In software engineering, a creational design pattern is a design pattern that deals with the process of object creation in a way that is both flexible and efficient. Creational design patterns provide general solutions for creating objects suitable for various situations without being tied to specific classes or configurations. These patterns help encapsulate the object creation process, making the code more flexible and easier to maintain. They can also improve the performance of an application by minimizing the number of object instances created. Examples of creational design patterns include Singleton, Factory Method, Abstract Factory, Builder, and Prototype patterns. Each pattern addresses a specific type of object creation problem and provides a standardized solution that can be adapted to various scenarios.
In this article, we will start with the Factory Method pattern.
The Factory Design Pattern is a popular creational design pattern in software engineering that allows us to create objects without exposing the object creation logic to the client. This pattern is widely used in C# programming and helps create an object of a similar type based on the input parameter passed.
According to the Gang of Four, the Factory Design Pattern is defined as "A factory is an object which is used for creating other objects". In technical terms, a factory is a class with a method that creates and returns different types of objects based on the input parameter it receives. The Factory Design Pattern is particularly useful when we have a superclass and multiple subclasses and need to create and return an object of one of the subclasses based on the data provided. With the help of this pattern, we can encapsulate the object creation process and provide a common interface to the client to refer to the newly created object. The basic principle behind the Factory Design Pattern is to provide a flexible and efficient solution for creating objects that can be adapted to various situations. By utilizing this pattern, we can improve the performance of our application by minimizing the number of object instances created. Factory Design Pattern is a widely used creational design pattern that provides a way to create objects without exposing the object creation logic to the client.
Factory Design Pattern example scenario 1
Suppose you own a pizza restaurant that offers different types of pizza to your customers, such as Cheese Pizza, Pepperoni Pizza, and Veggie Pizza. To implement this in your software system, you can create a hierarchy of pizza classes where a superclass called "Pizza" represents the common features of all pizzas, and each subclass represents a specific type of pizza.
For example, the Pizza superclass can have common properties like "name", "description", and "price", and methods like "prepare", "bake", "cut", and "box". Each subclass can then override these methods and add its own implementation for the specific pizza type. Now, when a customer orders a specific pizza, you need to create an object of that particular pizza type and return it to the customer. Here's where the Factory Design Pattern comes into play. You can create a Factory class called "PizzaFactory" with a static method called "CreatePizza" that takes a string parameter representing the type of pizza the customer wants to order. In the "CreatePizza" method, you can use a switch statement or some other mechanism to create an object of the appropriate pizza subclass based on the input parameter. For example, if the input parameter is "cheese", you can create an object of the "Cheese Pizza" subclass. If the input parameter is "pepperoni", you can create an object of the "Pepperoni Pizza" subclass, and so on. Once the object of the specific pizza type is created, you can return it to the customer. The customer can then call the methods on the returned pizza object to prepare, bake, cut, and box the pizza.
Using the Factory Design Pattern in this example, you can encapsulate the object creation process and provide a common interface for the client (in this case, the customer) to refer to the newly created object. This approach also allows you to add more pizza types in the future without modifying the client code, making your system more modular and extensible.
Here's the complete code for the Pizza Restaurant example of the Factory Design Pattern in C#:
using System;
using System.Collections.Generic;
namespace FactoryMethodPattern_1 {
public abstract class Pizza {
public string Name {
get;
set;
}
public string Description {
get;
set;
}
public double Price {
get;
set;
}
public List < string > Toppings {
get;
set;
}
public abstract void Prepare();
public abstract void Bake();
public abstract void Cut();
public abstract void Box();
}
public class CheesePizza: Pizza {
public CheesePizza() {
Name = "Cheese Pizza";
Description = "Pizza with cheese and tomato sauce";
Price = 8.99;
Toppings = new List < string > {
"Cheese",
"Tomato Sauce"
};
}
public override void Prepare() {
Console.WriteLine("Preparing " + Name);
}
public override void Bake() {
Console.WriteLine("Baking " + Name);
}
public override void Cut() {
Console.WriteLine("Cutting " + Name);
}
public override void Box() {
Console.WriteLine("Boxing " + Name);
}
}
public class PepperoniPizza: Pizza {
public PepperoniPizza() {
Name = "Pepperoni Pizza";
Description = "Pizza with pepperoni, cheese, and tomato sauce";
Price = 9.99;
Toppings = new List < string > {
"Pepperoni",
"Cheese",
"Tomato Sauce"
};
}
public override void Prepare() {
Console.WriteLine("Preparing " + Name);
}
public override void Bake() {
Console.WriteLine("Baking " + Name);
}
public override void Cut() {
Console.WriteLine("Cutting " + Name);
}
public override void Box() {
Console.WriteLine("Boxing " + Name);
}
}
public class VeggiePizza: Pizza {
public VeggiePizza() {
Name = "Veggie Pizza";
Description = "Pizza with veggies, cheese, and tomato sauce";
Price = 10.99;
Toppings = new List < string > {
"Mushrooms",
"Bell Peppers",
"Onions",
"Cheese",
"Tomato Sauce"
};
}
public override void Prepare() {
Console.WriteLine("Preparing " + Name);
}
public override void Bake() {
Console.WriteLine("Baking " + Name);
}
public override void Cut() {
Console.WriteLine("Cutting " + Name);
}
public override void Box() {
Console.WriteLine("Boxing " + Name);
}
}
Next, let's define the PizzaFactory class that will create objects of the appropriate pizza subclass based on the input parameter,

Join the conversation! Your thoughts help the community grow.