beware of bugs in the above code; i have only proved it correct, not tried it
TRANSCRIPT
![Page 1: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/1.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/2.jpg)
Religious Studies 313 – Advanced Programming Topics
![Page 3: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/9.jpg)
In The Beginning…
Creator
Product
![Page 10: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/10.jpg)
Problem with Simple Factory
![Page 11: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/12.jpg)
Bottoms-Up!
![Page 13: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/14.jpg)
Problem with Simple Factory
![Page 15: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/15.jpg)
What if…
![Page 16: Beware of bugs in the above code; I have only proved it correct, not tried it](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032607/56649ed15503460f94bdfb3d/html5/thumbnails/22.jpg)
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?