Wednesday, September 24, 2014

Simple Factory

If you have code that makes use of lots of concrete classes, you're looking for trouble because that code may have to be changed as new concrete classes are added.

So, your code will not be closed for modification.

Remember: Designs should be open for extension but closed for modification. 


class PizzaStore 

Pizza class



What if you want to add/delete pizza types


Encapsulating Object Creation


into a Factory class

Reworking the PizzaStore class


SimplePizzaFactory simpleFactory = new SimplePizzaFactory();
PizzaStore pizzaStore = new PizzaStore(simpleFactory);
pizzaStore.order("Veggie");

Class diagram


No comments:

Post a Comment