22
Beware of bugs in the above code; I have only proved it correct, not tried it.

Beware of bugs in the above code; I have only proved it correct, not tried it

Embed Size (px)

Citation preview

Page 1: Beware of bugs in the above code; I have only proved it correct, not tried it

Beware of bugs in the above code; I have only

proved it correct, not tried it.

Page 2: Beware of bugs in the above code; I have only proved it correct, not tried it

Religious Studies 313 – Advanced Programming Topics

Page 3: Beware of bugs in the above code; I have only proved it correct, not tried it

Why Use Factories?

Pizza pie = new DeepDish();

pie = new Garlic(pie);

pie = new Garlic(pie);

pie = new Onion(pie);

pie

Page 4: Beware of bugs in the above code; I have only proved it correct, not tried it

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;

}

Page 5: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 6: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 7: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 8: Beware of bugs in the above code; I have only proved it correct, not tried it

Speaking of Pizza ToppingsOtto von Bismarck [Classes] are like

sausages. It's better not to see

them being made.

Page 9: Beware of bugs in the above code; I have only proved it correct, not tried it

In The Beginning…

Creator

Product

Page 10: Beware of bugs in the above code; I have only proved it correct, not tried it

Problem with Simple Factory

Page 11: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 12: Beware of bugs in the above code; I have only proved it correct, not tried it

Bottoms-Up!

Page 13: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 14: Beware of bugs in the above code; I have only proved it correct, not tried it

Problem with Simple Factory

Page 15: Beware of bugs in the above code; I have only proved it correct, not tried it

What if…

Page 16: Beware of bugs in the above code; I have only proved it correct, not tried it

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;

}

}

Page 17: Beware of bugs in the above code; I have only proved it correct, not tried it

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

}

Page 18: Beware of bugs in the above code; I have only proved it correct, not tried it

Factory Method Pattern

public class DeepDishFactory extends PizzaFactory {

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

}

public class ThinCrustFactory extends PizzaFactory {

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

}

Page 19: Beware of bugs in the above code; I have only proved it correct, not tried it

Factory Method UML

Clients call method in AbstractFactory May not know ConcreteFactory they have

Page 20: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 21: Beware of bugs in the above code; I have only proved it correct, not tried it

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

Page 22: Beware of bugs in the above code; I have only proved it correct, not tried it

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?