Pattern Factory (Factory)
by Jose Jorge Marquez Gomez on ago.27, 2010, under DoNet
A factory pattern serves to channel all creations of objects in a single point. This is useful for robust design of our system and responsive to change.
The factory pattern can be combined with the singleton pattern as it is more comfortable to use and useful form to use. In this example we will put a factory without singleton.
To say the factory pattern is of the creational patterns, this type of pattern that abstracts the way to create concrete objects, thus giving support to create objects in the abstract.
To start a factory pattern is to perform:
- A function or method which is responsible for creating objects of a given type.
- It is not mandatory but recommended that the function or method returns an abstraction (interface or abstract class).
Imagine a pizza shop requires a software you need to have a number of products (pizzas varied). As designers we are good, the pizza object creation can be done from anywhere in the project, but the problem is that there are many types of pizzas. To create each pizza for each type in different parts of the program would be the first thing you would think, but ... if instead we use common sense and apply the factory?, This would allow us as a new product or change in any of them that only change in one place, taking more controlled this code. Even if you realize the speed of change is much more to go changing all possible appearances of our pizzas by the code.
Well, the factory would be responsible for creating all kinds of pizzas and offer all possible points of the program.
Our example would be:
- The interface IPizza:
IPizza interface
{
WhoAmI void ();
}
- The individual pizzas:
public class PizzaBarbacoa: IPizza
{
# Region Members IPizza
public void WhoAmI ()
{
Console.WriteLine ("I am a BBQ pizza");
}
# Endregion
}
public class PizzaMargarita: IPizza {# region Members public void IPizza WhoAmI () {Console.WriteLine ("I am a pizza Margarita");} # endregion}
public class PizzaPepperoni: IPizza
{
# Region Members IPizza
public void WhoAmI ()
{
Console.WriteLine ("I am a pizza Pepperoni");
}
# Endregion
}
- The plant in question:
public class PizzaFactory
{
public enum tipoPizza
{
Pepperoni, Margherita, Barbeque
}
IPizza public getPizza (tipoPizza type)
{
switch (type)
{
tipoPizza.Barbacoa case:
return new PizzaBarbacoa ();
break;
tipoPizza.Margarita case:
return new PizzaMargarita ();
break;
tipoPizza.Pepperoni case:
return new PizzaPepperoni ();
break;
default:
return new PizzaBarbacoa ();
}
}
}
- A little test:
public class Test {public void testPizza () {/ / example not using factory PizzaBarbacoa IPizza pizza1 = new (); pizza1.quienSoy (); IPizza PizzaMargarita pizza2 = new (); pizza2.quienSoy (); IPizza PizzaPepperoni pizza3 = new ( ) pizza3.quienSoy (); IPizza PizzaBarbacoa pizza4 = new (); pizza4.quienSoy (); IPizza PizzaMargarita pizza5 = new (); pizza5.quienSoy () / / example using factory PizzaFactory PizzaFactory factory = new (); IPizza pizza6 = factory.getPizza (PizzaFactory.tipoPizza.Barbacoa); pizza6.quienSoy (); IPizza pizza7 = factory.getPizza (PizzaFactory.tipoPizza.Margarita); pizza7.quienSoy (); IPizza pizza8 = factory.getPizza (PizzaFactory.tipoPizza. Pepperoni) pizza8.quienSoy ();}} As you can see how to use and its advantages are better. I hope you like!. See you later!






