factory pattern
DESCRIPTION
Factory Pattern. Building Complex Objects. New is an implementation. Calling “new” is certainly coding to an implementation In fact, it’s always related to a concrete class (Open for Modification) That’s fine when things are simple, but. Look Out For Change. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/1.jpg)
Factory Pattern
Building Complex ObjectsBuilding Complex Objects
![Page 2: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/2.jpg)
New is an implementationNew is an implementation
Calling “new” is certainly coding to an implementation
In fact, it’s always related to a concrete class
(Open for Modification) That’s fine when things are simple,
but . . .
Calling “new” is certainly coding to an implementation
In fact, it’s always related to a concrete class
(Open for Modification) That’s fine when things are simple,
but . . .
![Page 3: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/3.jpg)
Look Out For ChangeLook Out For Change
When you have several related classes, that’s probably a good sign that they might change in the future
Duck duck;if (picnic) {
duck = new MallardDuck();} else if (hunting) {
duck = new DecoyDuck();} else if (inBathTub) {
duck = new RubberDucky();}
![Page 4: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/4.jpg)
Design PrincipleDesign Principle
What should you try to do with code that changes?
What should you try to do with code that changes?
![Page 5: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/5.jpg)
When Building Objects is Complex
When Building Objects is Complex
Book example - a variety of types of pizzas
Think about constructing the weapons in your decorator lab
Book example - a variety of types of pizzas
Think about constructing the weapons in your decorator lab
![Page 6: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/6.jpg)
Pizza orderPizza() {
Pizza pizza = new Pizza();
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
![Page 7: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/7.jpg)
Pizza orderPizza(String type) {Pizza pizza;
if (type.equals(“cheese”)) {pizza = new CheesePizza();
} else if (type.equals(“greek”)) {pizza = new GreekPizza();
} else if (type.equals(“pepperoni”)) {pizza = new PepperoniPizza();
}
pizza.prepare();pizza.bake();pizza.cut();pizza.box();
return pizza;}
![Page 8: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/8.jpg)
Pizza orderPizza(String type) {Pizza pizza;
if (type.equals(“cheese”)) {pizza = new CheesePizza();
} else if (type.equals(“greek”)) {pizza = new GreekPizza();
} else if (type.equals(“pepperoni”)) {pizza = new PepperoniPizza();
} else if (type.equals(“sausage”)) {pizza = new SausagePizza();
} else if (type.equals(“veggie”)) {pizza = new VeggiePizza();
}
pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;
}
![Page 9: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/9.jpg)
Pizza orderPizza(String type) {Pizza pizza;
if (type.equals(“cheese”)) {pizza = new CheesePizza();
} else if (type.equals(“greek”)) {pizza = new GreekPizza();
} else if (type.equals(“pepperoni”)) {pizza = new PepperoniPizza();
} else if (type.equals(“sausage”)) {pizza = new SausagePizza();
} else if (type.equals(“veggie”)) {pizza = new VeggiePizza();
}
pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;
}
Encapsulate!
![Page 10: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/10.jpg)
public class SimplePizzaFactory {public Pizza createPizza(String type) {
Pizza pizza;
if (type.equals(“cheese”)) {pizza = new CheesePizza();
} else if (type.equals(“pepperoni”)) {pizza = new PepperoniPizza();
} else if (type.equals(“sausage”)) {pizza = new SausagePizza();
} else if (type.equals(“veggie”)) {pizza = new VeggiePizza();
}
return pizza;}
}
Now orderPizza() is tidy
![Page 11: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/11.jpg)
public class PizzaStore {SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory factory) {this.factory = factory;
}
public Pizza orderPizza(String type) {Pizza pizza;pizza = factory.createPizza(type);
pizza.prepare();pizza.bake();pizza.cute();pizza.box();
return pizza;}
}
No new operator
![Page 12: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/12.jpg)
Simple FactorySimple Factory
Pull the code that builds the instances out and put it into a separate class Identify the aspects of your
application that vary and separate them from what stays the same
Pull the code that builds the instances out and put it into a separate class Identify the aspects of your
application that vary and separate them from what stays the same
![Page 13: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/13.jpg)
Simple Factory PatternSimple Factory Pattern
ClientorderProduct()
SimpleFactorycreateProduct()
<<abstract>>Product
productMethod()
ConcreteProductB
ConcreteProductA
ConcreteProductC
![Page 14: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/14.jpg)
Why would we do this?Why would we do this?
Multiple clients needing same types of object
Ensure consistent object initialization
Multiple clients needing same types of object
Ensure consistent object initialization
![Page 15: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/15.jpg)
The Factory MethodThe Factory Method
Definition: The factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Definition: The factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
![Page 16: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/16.jpg)
Factory Method PatternFactory Method Pattern
<<abstract>>Creator
factoryMethod()operation()
<<abstract>>Product
ConcreteCreatorfactoryMethod()
ConcreteProduct
abstract!
IS-A
HAS-A
IS-A
No more SimpleFactory classObject creation is back in our class, but …
delegated to concrete classes
![Page 17: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/17.jpg)
Dependency Inversion PrincipleDependency Inversion Principle
Depend upon abstractions -- not concrete classes
Depend upon abstractions -- not concrete classes
![Page 18: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/18.jpg)
Impossible GuidelinesImpossible Guidelines
No variable should hold a reference to a concrete class
No class should derive from a concrete class
No method should override an implemented method of an of its base classes.
No variable should hold a reference to a concrete class
No class should derive from a concrete class
No method should override an implemented method of an of its base classes.
![Page 19: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/19.jpg)
Impossible GuidelinesImpossible Guidelines
The idea here is to stay clear of classes that are likely to change
String studentCheckingSystem businessCheckingInteger FederalTaxCode
The idea here is to stay clear of classes that are likely to change
String studentCheckingSystem businessCheckingInteger FederalTaxCode
![Page 20: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/20.jpg)
What if there are categories of Products?
What if there are categories of Products?
<<abstract>>Player
HeroGunnerHeroStealth
HeroTankHeroPilot
EnemyLevelBossEnemyCaptain
EnemyThug
![Page 21: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/21.jpg)
When Subclasses get out of hand…
When Subclasses get out of hand…
• make the factory an abstract class as well
• Instead of subclasses for the products, subclasses for the components of the products
• The next diagram is overwhelming… breathe in, breath out
![Page 22: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/22.jpg)
Abstract Factory PatternAbstract Factory Pattern<<interface>>
AbstractFactorycreateProductA()createProductB()
<<interface>>AbstractProductA
<<interface>>AbstractProductB
Client
ConcreteFactory1createProductA()createProductB()
ConcreteFactory2createProductA()createProductB()
ConcreteProductA1
ConcreteProductA2
ConcreteProductB1
ConcreteProductB2
![Page 23: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/23.jpg)
Abstract Factory PatternAbstract Factory Pattern
This design favors composition
handy when you need to insure the quality of many similar, but slightly different versions of a
class
![Page 24: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/24.jpg)
More DiagramsMore Diagrams
pp.156/157
160/161
pp.156/157
160/161
![Page 25: Factory Pattern](https://reader036.vdocuments.us/reader036/viewer/2022062802/56814611550346895db31f0c/html5/thumbnails/25.jpg)
SummarySummary Simple Factory
Use when you have only one family of objects
Factory Method Pattern Use when you have multiple families of objects
Abstract Factory Use when you have multiple families of object
components
Simple Factory Use when you have only one family of objects
Factory Method Pattern Use when you have multiple families of objects
Abstract Factory Use when you have multiple families of object
components