Download - Lecture 14: Factory Pattern Basics
![Page 1: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/1.jpg)
LECTURE 14:FACTORY PATTERN BASICS
CSC 313 – Advanced Programming Topics
![Page 2: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/2.jpg)
Strategy Pattern Usage
public class RubberDuck extends Duck {
FlightBehavior flyBehavior;QuackBehavior quackBehavior;
public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay();}
![Page 3: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/3.jpg)
Strategy Pattern Usage
public class RubberDuck extends Duck {
FlightBehavior flyBehavior;QuackBehavior quackBehavior;
public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay();}
flyBehaviorquackBehavior Squeak
FlyNoWayRubberDuck
![Page 4: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/4.jpg)
Decorator Pattern Usage
Pizza pie = new DeepDish();pie = new Garlic(pie);pie = new Garlic(pie);pie = new Onion(pie);
OnionGarlic
GarlicDDish
pie
![Page 5: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/5.jpg)
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
![Page 6: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/6.jpg)
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
![Page 7: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/7.jpg)
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
![Page 8: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/8.jpg)
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
![Page 9: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/9.jpg)
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
![Page 10: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/10.jpg)
Programming to a Concept
Can we even make instantiation conceptual? Hard-coded class name required by new
command Tricks impossible – no interfaces or
abstract classes Cannot rely on polymorphism
![Page 11: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/11.jpg)
Programming to a Concept
Can we even make instantiation conceptual? Hard-coded class name required by new
command Tricks impossible – no interfaces or
abstract classes Cannot rely on polymorphism
Must instantiate a class, but can LOOK fancy:
new Squeak();new Garlic(pie);
new Monkey(Oohhh, Look);
![Page 12: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/12.jpg)
Decorator Pattern Problem
Need DoubleGarlicOnionDeepDish class for this?
Pizza pie = new Garlic(DeepDish());pie = new Onion(Garlic(pie));
![Page 13: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/13.jpg)
Strategy Pattern Usage
Why write a class that just a constructor?
public RubberDuck() {quackBehavior = new Squeak();flyBehavior = new FlyNoWay();
}
![Page 14: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/14.jpg)
Relations Between Patterns Design patterns can be used alone
Intent & purpose differs for each pattern “Official” patterns in at least 3 industrial
projects Often travel together in code
Many strong relationships between patterns
Combination stronger than using by itself
![Page 15: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/15.jpg)
Improving Constructor
May want to limit creating objects Control instantiation and when or how it
occurs May want to enforce limit on number
instantiated Boolean instances silly, for example
Wastes time & memory, only have 2: true & false
Constructors limited in their actions, however Before constructor starts, instance already
allocated Exception only option to disrupt allocation
process
![Page 16: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/16.jpg)
Smarter Move
public class Bool {static Bool TRUE = new Bool(true);static Bool FALSE = new Bool(false);
private boolean value;
private Bool(boolean b) { value = b; }
static Bool fromBoolean(boolean b) { if (b) return TRUE; else return FALSE; }
![Page 17: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/17.jpg)
Cache Data To Save Time
public class Int {static Map<String,Int> map; 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;}
![Page 18: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/18.jpg)
Smart Instantiation
To limit construction what should your code do Make sure you declare constructors
protected/private Get instances via method declared public static
From outside class, instantiation is prohibited Get protection error on lines with new
command For instance, must use public static
method
![Page 19: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/19.jpg)
Better Instantiation
Methods like these called Factory Methods Method creates objects just like it is a
factory Factory methods provide additional
features Meaningful name can be given to
method Using set of methods, creation options
clarified Readable code easy & no jumping
through hoops
![Page 20: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/20.jpg)
Factory Method Example
public class Int {static Map<String,Int> map; 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;}
![Page 21: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/21.jpg)
Simple Factory Pattern
Also known as Static Factory Pattern (Only if method static & not instance
based) Pattern uses single factory method
as base Multiple types instantiated in factory
method Contains specific, hard-coded “new”
commands Other classes use method to skip
instantiations Changing or adding types is much
easier Only need to modify factory method Client code may not know other types
exist!
![Page 22: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/22.jpg)
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 { throw Exception(“No Pizza for
you!”); }
}Pizza pie = StaticFactory.createPizza(“fish”);
![Page 23: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/23.jpg)
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 { throw Exception(“No Pizza for
you!”); }
}SimpleFactory simple = ...Pizza pie = simple.createPizza(“fish”);
![Page 24: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/24.jpg)
Creating Simple Factory
Factory method instantiates many types protected constructors prevents unlimited
alloc. Within method, parameter(s) specify what
to allocate Pluses & minuses to static factory
method use Factory method much easier to find and
call Client tied to factory class via hard-coded
call Keeping factory method non-static
means More generic client code calling factory
method Exactly which method called not clear
![Page 25: Lecture 14: Factory Pattern Basics](https://reader035.vdocuments.mx/reader035/viewer/2022062520/568161e5550346895dd20500/html5/thumbnails/25.jpg)
For Next Lecture
Lab #3 available on Angel Asks you to implement Decorator
Pattern Have time today, but may want help
profiling In your book, read pages 123 - 143
How do we write these factories? Is there a good way to do this? What design pattern could be used in
these cases?