14 Factory Pattern Time Clock Abstract Time Time
14. Factory Pattern
Time Clock • Abstract. Time
Time Known as a static Clock factory method • Abstract. Time if else } "time" return new , , ; Introduces dependency (coupling) on subclass! ;
// create some behaviors Swim. Behavior csb = new Circular. Swimming(); Quack. Behavior sqb = new Standard. Quacking(); Swim. Behavior rsb = new Random. Floating(); // daffy has circular swimming, std quacking Waterfowl daffy = new Duck(“daffy”, csb, sqb); // donald has random floating, std quacking Waterfowl donald = new Duck(“donald”, rsb, sqb); daffy. swim(); donald. quack();
Swim. Behavior circ_swimmer = new Circular. Swimming(); Swim. Behavior rand_floater = new Random. Floating(); Quack. Behavior std_quacker = new Standard. Quacking(); Quack. Behavior silent = new No. Quacking(); List<Duck> ducks = new Linked. List<>(); String swim_type = in. next(); String quack_type = in. next(); String name = in. next(); while ( !swim_type. equals(“done”) ) { Swim. Behavior swim_style = swim_type. equals(“circular”) ? circ_swimmer : rand_floater; Quack. Behavior quack_style = quack_type. equals(“standard”) ? std_quacker : silent; ducks. add(new Duck(name, swim_style, quack_style); swim_type = in. next(); quack_type = in. next(); name = in. next(); }
Swim. Behavior circ_swimmer = new Circular. Swimming(); Swim. Behavior rand_floater = new Random. Floating(); Quack. Behavior std_quacker = new Standard. Quacking(); Quack. Behavior silent = new No. Quacking(); List<Duck> ducks = new Linked. List<>(); String swim_type = in. next(); String quack_type = in. next(); String name = in. next(); while ( !swim_type. equals(“done”) ) { Swim. Behavior swim_style = swim_type. equals(“circular”) ? circ_swimmer : rand_floater; Quack. Behavior quack_style = quack_type. equals(“standard”) ? std_quacker : silent; ducks. add(new Duck(name, swim_style, quack_style)); }
Swim. Behavior circ_swimmer = new Circular. Swimming(); Swim. Behavior rand_floater = new Random. Floating(); Quack. Behavior std_quacker = new Standard. Quacking(); Quack. Behavior silent = new No. Quacking(); But what happens when List<Duck> ducks = new Linked. List<>(); we have more behaviors? String swim_type = in. next(); String quack_type = in. next(); String name = in. next(); while ( !swim_type. equals(“done”) ) { Swim. Behavior swim_style = swim_type. equals(“circular”) ? circ_swimmer : rand_floater; Quack. Behavior quack_style = quack_type. equals(“standard”) ? std_quacker : silent; ducks. add(new Duck(name, swim_style, quack_style)); }
Swim. Behavior circ_swimmer = new Circular. Swimming(); Swim. Behavior rand_floater = new Random. Floating(); Quack. Behavior std_quacker = new Standard. Quacking(); Quack. Behavior silent = new No. Quacking(); But what happens when List<Duck> ducks = new Linked. List<>(); we have more behaviors? String swim_type = in. next(); String quack_type = in. next(); • Operation? String name = in. next(); • Class? while ( !swim_type. equals(“done”) ) { Swim. Behavior swim_style = swim_type. equals(“circular”) ? circ_swimmer : rand_floater; Quack. Behavior quack_style = quack_type. equals(“standard”) ? std_quacker : silent; ducks. add(new Duck(name, swim_style, quack_style)); }
method solution
static method solution public static Duck create. Duck(Scanner in) { String swim_type = in. next(), quack_type = in. next(), name = in. next(); Swim. Behavior swim_style = swim_type. equals(“circular”) ? circ_swimmer : rand_floater; Quack. Behavior quack_style = quack_type. equals(“standard”) ? std_quacker : silent; return new Duck(name, swim_style, quack_style); }
method solution Alternative: create a class Duck. Factory public class Duck. Factory { public Duck grow(String swim_type, String quack_type) { … } }
method solution • Yes, just pushed problem into another object • But, have one place to maintain duck creation • Alternative: static methods – but can’t subclass Alternative: create a class Duck. Factory public class Duck. Factory { public Duck grow(String swim_type, String quack_type) { … } }
• Client may need to incorporate intelligence such as “thread awareness” in order to create the objects on the correct thread • Object creation may need to be a multi-step procedure • Hard to maintain – may require a lot of different “new’s” • And generally, we want to program to interfaces or abstract classes
• The interface consists of methods known as Factory Methods • This delegates the decision of what/how to create to the concrete subclasses
The interface consists of Factory Methods Creation is not done via constructor methods because constructor methods cannot be overridden
The concrete factory (USMoney. Mint) implements a single Factory Method (create. Currency. Maker), which instantiates concrete Products (Dollar. Bill. Maker or Dollar. Coin. Maker)
The concrete factory (USMoney. Mint) implements a single Factory Method (create. Currency. Maker), which instantiates concrete Products (Dollar. Bill. Maker or Dollar. Coin. Maker) But the client still has to create the Factory (USMoney. Mint) that creates the Currency. Makers There are two Products being built here by the USMoney. Mint Factory – Dollar. Coin. Maker and Dollar. Bill. Maker
Why not move the if statement to the constructor? The concrete factory (USMoney. Mint) implements a single Factory Method (create. Currency. Maker), which instantiates concrete Products (Dollar. Bill. Maker or Dollar. Coin. Maker) But the client still has to create the Factory (USMoney. Mint) that creates the Currency. Makers There are two Products being built here by the USMoney. Mint Factory – Dollar. Coin. Maker and Dollar. Bill. Maker
Why not move the if statement to the constructor? • Constructor doesn’t “return” an object! • It just initializes the current object The concrete factory (USMoney. Mint) implements a single Factory Method (create. Currency. Maker), which instantiates concrete Products (Dollar. Bill. Maker or Dollar. Coin. Maker) But the client still has to create the Factory (USMoney. Mint) that creates the Currency. Makers There are two Products being built here by the USMoney. Mint Factory – Dollar. Coin. Maker and Dollar. Bill. Maker
• Whose “factory methods” are similar
The abstract factory (Money. Mint) defines the Factory Method implemented by the concrete factories, which is used by the client to create Currency. Makers But the client still has to create the concrete Factories (USMoney. Mint), but can refer to them abstractly (via Money. Mint references) The products created by are sufficiently different to warrant separate Factories (USMoney. Mint and Canadian. Money. Mint), each of which “knows” which products to make
- Slides: 25