beware of bugs in the above code; i have only proved it correct, not tried it

Post on 02-Jan-2016

224 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Beware of bugs in the above code; I have only

proved it correct, not tried it.

Religious Studies 313 – Advanced Programming Topics

Why Use Factories?

Pizza pie = new DeepDish();

pie = new Garlic(pie);

pie = new Garlic(pie);

pie = new Onion(pie);

pie

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); else ret = new Cheese(ret);}return ret;

}

Simple Factory Overview

Single method to perform instantiation Method often defined in class of its own Could also make method static*

Replace new with calls to simple factory Simple factory contains all new

commands Update with each new class of same

type

* Do not make this method static

Simple Factory Overview

Really, really helpful pseudo-pattern Easy to write Isolates code that often needs to change Remaining code relies on abstractions Simplifies using new subclasses in

program Simple Factory pattern needs little

design Needs only factory class & type(s) it

instantiates

Simple Factory UML

Clients call method in SimpleFactory AbstractProduct is returned to client

Does not know which ConcreteProduct it is

May not know ConcreteProducts exist Can invisibly change ConcreteProducts

Speaking of Pizza ToppingsOtto von Bismarck [Classes] are like

sausages. It's better not to see

them being made.

In The Beginning…

Creator

Product

Problem with Simple Factory

Design like you code; Start with the abstractions

Design like I code; Start with the abstractions

Design like someone good codes; Start with the abstractions

Dependency Inversion

Bottoms-Up!

Design to Concept, Too

Simple factory created as need arises Developer notices many like classes

created Methods becoming bloated with options Prescient engineer creates future

options These all start and end with the

classes Simple factory used as means to an end No work conceptualizing concrete

classes No design work going into Simple

Factory

Problem with Simple Factory

What if…

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;

}

}

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();

}

Factory Method Pattern

public class DeepDishFactory extends PizzaFactory {

Pizza createPizzaType() { return new DeepDish();}

}

public class ThinCrustFactory extends PizzaFactory {

Pizza createPizzaType() { return new Cracker();}

}

Factory Method UML

Clients call method in AbstractFactory May not know ConcreteFactory they have

Factory Method Process

AbstractCreator defines factory Can be interface or abstract class Does not actually instantiate any Products

ConcreteCreators create instances ConcreteCreator for each type of Product Use multiple creators for parallel hierarchies

Creates two sets of parallel hierarchies DeepDish & Cracker product lines DeepDishFactory & ThinCrustFactory

creator

Factory Method Intent

Only use interfaces & abstract classes for: Variables Fields Parameters Statics

Banish required concreteness to perimeters Factory methods Where performance is critical Code passing the “What would _______ do?”

test

For Next Lecture

Lab #4 available on web/Angel Due before next lab (Tues. 2/26) I will be available during lab time next

week First set of pattern reports due on

8AM Wed. This time: Debbie, Jake, Katey, Matt S. &

Ryan Read pages 144 - 162 in the book

How can we easily enable skinnable applications?

Could I make even odder religious references?

top related