Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the."— Presentation transcript:

1 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.

2 CSC 313 – Advanced Programming Topics

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

4 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

5 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

6 Programming to a Concept

7 Strategy Pattern Usage Should we need all the different duck classes just for their constructors? public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); }

8 Decorator Pattern Problem Need DoubleGarlicOnionDeepDish class for this? Pizza pie = new Garlic(DeepDish()); pie = new Onion(Garlic(pie));

9 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

10 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

11 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; } } }

12 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; }

13 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

14 It’s 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

15 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; }

16 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

17 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”);

18 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”);

19 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

20 For Next Lecture  Lab #4 available on web/Angel  Asks you to implement remove recursion  Due before next lab (Tues. 2/26) ‏  Read pages 123 - 143 in the book  How do we write these factories?  Is there a good way to do this?  How does the actual design pattern work?


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

Similar presentations


Ads by Google