factory pattern

25
Factory Pattern Building Complex Objects

Upload: les

Post on 13-Jan-2016

26 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: Factory Pattern

Factory Pattern

Building Complex ObjectsBuilding Complex Objects

Page 2: Factory Pattern

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

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

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

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

Pizza orderPizza() {

Pizza pizza = new Pizza();

pizza.prepare();

pizza.bake();

pizza.cut();

pizza.box();

return pizza;

}

Page 7: Factory Pattern

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

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

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

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

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

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

Simple Factory PatternSimple Factory Pattern

ClientorderProduct()

SimpleFactorycreateProduct()

<<abstract>>Product

productMethod()

ConcreteProductB

ConcreteProductA

ConcreteProductC

Page 14: Factory Pattern

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

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

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

Dependency Inversion PrincipleDependency Inversion Principle

Depend upon abstractions -- not concrete classes

Depend upon abstractions -- not concrete classes

Page 18: Factory Pattern

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

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

What if there are categories of Products?

What if there are categories of Products?

<<abstract>>Player

HeroGunnerHeroStealth

HeroTankHeroPilot

EnemyLevelBossEnemyCaptain

EnemyThug

Page 21: Factory Pattern

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

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

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

More DiagramsMore Diagrams

pp.156/157

160/161

pp.156/157

160/161

Page 25: Factory Pattern

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