Let’s say you have a pizza shop, and as a cutting-edge pizza store owner, you might end up writing some code like this:
But you need more than one type of pizza…
So then you will add some code that determines the appropriate type of pizza and then goes about making the pizza:
But the pressure is on to add more pizza types
This will only mess up the OrderPizza() method if so many if else constructs. It is time to encapsulate it.
But How?
Take the creation code and move it out into another object that is only going to be concerned with creating pizzas.
Building a simple pizza factory
Here it is:
And here is the revised PizzaStore client class, which uses SimplePizzaFactory to encapsulate pizza creation process:
Please note that Simple Factory isn’t actually a design pattern. It’s more of a programming idiom.
Hope this helps.
eda22024-d611-4a50-8459-a9b533263bd3|1|4.0
factory method, c#, design pattern