Download presentation
Presentation is loading. Please wait.
Published byEstella George Modified over 9 years ago
1
Religious Studies 313 – Advanced Programming Topics
2
Today’s Goals Going to hell/lightning striking me down avoided Need for factories with other patterns shown What is problem creating pattern instances? Simple factory trashed after its discussed First, we will be reviewing how these work Situation it helps will be examined in detail And then highlight limitations of this approach Solve the problem with Factory Method pattern Show how it provides better, more general, approach
3
Why Use Factories? Pizza pie = new DeepDish(); pie = new Garlic(pie); pie = new Onion(pie); pie
4
Speaking of Pizza Toppings Otto von Bismarck _____ are like sausages. It's better not to see them being made.
5
Simple Factory Pattern Pizza pie = PizzaFactory.createPizza(type, toppings); Pizza createPizza(String type, String[] toppings) { Pizza ret; if (type.equals(“DeepDish")) ret = new DeepDish(); else ret = new Cracker(); for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); } return ret; }
6
Simple Factory Overview
9
Little design required to use Simple Factory Within program, much easier to add & use classes Easy to write since need factory & classes to use Code that most often needs to change is isolated Client relies on abstraction simple factory provides
10
Simple Factory UML Client code calls method in SimpleFactory AbstractProduct returned by this method Specific type unknown, really a ConcreteProduct Client need not know about ConcreteProduct s
11
In The Beginning…
12
Creator Product
13
Problem with Simple Factory Approach requires single way to view world Assimilates everything that comes into contact with it Pizza createPizza(String type, String[] toppings) { Pizza ret; if (type.equals(“DeepDish")) ret = new DeepDish(); else ret = new Cracker(); for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); } return ret; }
14
Dependency Inversion Design like you code; Start with the abstractions
15
Dependency Inversion
17
Bottoms-Up!
18
Design to Concept, Not Class Simple factory created as need arises Many related classes created for use later Methods becoming bloated with options Room for growth wanted even if not used classes These cases are all about classes Means to an end is only reason for simple factory Concrete classes not conceptualized or planned No design work No design work going into Simple Factory
19
Problem with Simple Factory createPizza entirely dependent on 6 classes Must change whenever these constructors modified Pizza createPizza(String type, String[] toppings) { Pizza ret; if (type.equals(“DeepDish")) ret = new DeepDish(); else ret = new Cracker(); for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); } return ret; }
20
What if…
21
Or We Had…
22
Simple Factory public class PizzaFactory { Pizza createPizza(String type, String[] toppings) { Pizza ret; if (type.equals(“DeepDish")) ret = new DeepDish(); else ret = new Cracker(); for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); else ret = new Cheese(ret); } return ret; }
23
Factory Method Pattern public abstract class PizzaFactory { Pizza createPizza(String type, String[] toppings) { Pizza ret = createPizzaType(); for (String s : toppings) { if (s.equals(“Garlic")) ret = new Garlic(ret); else if (s.equals(“Onion")) ret = new Onion(ret); else if (s.equals(“Fish")) ret = new Anchovy(ret); else ret = new Cheese(ret); } return ret; } abstract Pizza createPizzaType(); }
24
Factory Method Pattern public class DeepDishFactory extends PizzaFactory { Pizza createPizzaType() { return new DeepDish(); } } public class ThinCrustFactory extends PizzaFactory { Pizza createPizzaType() { return new Cracker(); } }
25
Factory Method UML Clients call method in AbstractFactory ConcreteFactory unknown by the clients
26
Factory Method UML Clients call method in AbstractFactory ConcreteFactory unknown by the clients
27
Factory Method Process AbstractCreator defines factory Usually either an interface or abstract class Does not instantiate any actual Product s Instances allocated by ConcreteCreator s Each Product has own ConcreteCreator Develop lines using many ConcreteCreator s DeepDishFactory & ThinCrustFactory needed To create product lines for DeepDish & Cracker
28
Factory Method Intent Interface & abstract class types always used for: Variables Fields Parameters Statics Any & all required concreteness left for: Factory methods Code on critical path (when performance is critical)
29
Factory Method Intent Interface & abstract class types always used for: Variables Fields Parameters Statics Any & all required concreteness left for: Factory methods Code on critical path (when performance is critical) Code passing “What would _______ code?” test
30
Factory Method Intent Interface & abstract class types always used for: Variables Fields Parameters Statics Any & all required concreteness left for: Factory methods Code on critical path (when performance is critical) Code passing “What would _______ code?” test
31
For Next Lecture Lab #3 still available on Angel Use Assignment Submitter before Fri. lab (Feb. 24) Read pages 144 - 162 in the book How can we easily enable skinnable applications? Cars made in factories, but what pattern do they use?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.