fred brooks einstein argued that there must be simplified explanations of nature, because god is not...

Download Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the

If you can't read please download the document

Upload: leonard-obrien

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

Decorator Pattern Usage Pizza pie = new DeepDish(); pie = new Garlic(pie); pie = new Onion(pie); pie

TRANSCRIPT

Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. CSC 313 Advanced Programming Topics Decorator Pattern Usage Pizza pie = new DeepDish(); pie = new Garlic(pie); pie = new Onion(pie); pie Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior; public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); } RubberDuck flyBehavior quackBehavior Squeak FlyNoWay Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class Programming to a Concept Strategy Pattern Usage Should we need all the different duck classes just for their constructors? public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); } Decorator Pattern Problem Need DoubleGarlicOnionDeepDish class for this? Pizza pie = new Garlic(DeepDish()); pie = new Onion(Garlic(pie)); Relations Between Patterns Design patterns can be used alone Each has its own intent & purpose Must be in 3+ industrial projects to be accepted But often travel in groups Found in relationships with other patterns Often stronger in combination Improving Constructor May want to limit creating objects Could limit when instances created Limit the objects that actually get created For example, Boolean instances are silly Much easier if just have 2: true & false Constructor prevented from taking action Automatically creates a new instance Or throw an exception to disrupt process Smarter Move public class Bool { static Bool TRUE = new Bool(true); static Bool FALSE = new Bool(false); private boolean value; Bool(boolean b) { value = b; } Bool newBool(boolean b) { if (b) { return TRUE; } else { return FALSE; } } } Use to Add Caching public class Int { private static Map map; private int value; Int(int i) { value = i; } Int fromString(String s) { Int retVal = map.get(s); if (retVal == null) { retVal = new Int(Integer.parseInt(s)); map.put(s, retVal); } return retVal; } Smart Instantiation When limiting construction is good Can make all constructors protected/private Define public method instantiating object Other classes may not use constructor Could get protection error if they try writing it Use your public method to get instances Its A Pattern! Methods like these called Factory Methods Like a factory they just create objects Factory methods provide additional features Often have far more meaningful names Set of factory methods clarify different options Can be easier to follow code Factory Method Example public class Int { private static Map map; private int value; private Int(int i) { value = i; } public Int fromString(String s) { Int retVal = map.get(s); if (retVal == null) { retVal = new Int(Integer.parseInt(s)); map.put(s, retVal); } return retVal; } Simple Factory Pattern Also known as Static Factory Pattern But only when implemented using static method Uses single factory method Factory method constructs many types Place for all new commands found Others call this method to get new objects Limits methods that may need to change Static Factory Method public class StaticFactory { static Pizza createPizza(String type) { if (type.equals("cheese")) { return new CheesePizza(); } else if (type.equals(fish")) { return new AnchovyPizza(); } else { return null; } } Pizza pie = StaticFactory.createPizza(fish); Simple Factory Method public class SimpleFactory { Pizza createPizza(String type) { if (type.equals("cheese")) { return new CheesePizza(); } else if (type.equals(fish")) { return new AnchovyPizza(); } else { return null; } } SimpleFactory simple =... Pizza pie = simple.createPizza(fish); Creating Simple Factory Method instantiates many types Constructors better not be private Requires 1 or more parameters to determine type When making factory method static Easy to find factory method But back to coding to a class When keeping factory method non-static Classes calling factory method are more generic Finding the factory method is harder For Next Lecture Lab #4 available on web/Angel Asks you to implement remove recursion Due before next lab (Tues. 2/26) Read pages in the book How do we write these factories? Is there a good way to do this? How does the actual design pattern work?