14. Factory Pattern SE2811 Software Component Design

Slides:



Advertisements
Similar presentations
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Inheritance issues SE-2811 Dr. Mark L. Hornick 1.
ITEC200 – Week03 Inheritance and Class Hierarchies.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Case Studies on Design Patterns Design Refinements Examples.
Abstract Factory Design Pattern making abstract things.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
The Adapter Pattern SE-2811 Dr. Mark L. Hornick 1.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Systems Requirements SE 3821 Design? Algorithms CS 2852.
SE-2811 Software Component Design Week 1, Day 2 Making teams Warm-up exercise Design pattern defined SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
Slide design: Dr. Mark L. Hornick
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
// create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating();
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Lecture 5:Interfaces and Abstract Classes
Design Patterns: MORE Examples
Factory Method Pattern
Chapter 5:Design Patterns
Low Budget Productions, LLC
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Inheritance and Polymorphism
SE-2811 Software Component Design
Software Design and Architecture
Strategy Design Pattern
Chapter 13 Abstract Classes and Interfaces
SE-2811 Software Component Design
Object Oriented Practices
7. Decorator, Façade Patterns
SE-2811 Software Component Design
SE-2811 Software Component Design
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Week 6 Object-Oriented Programming (2): Polymorphism
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
SE-2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
Structural Patterns: Adapter and Bridge
7. Decorator SE2811 Software Component Design
8. Observer Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
13. Composite Pattern SE2811 Software Component Design
SE-2811 Software Component Design
10. Façade Pattern SE2811 Software Component Design
13. Composite Pattern SE2811 Software Component Design
5. Strategy, Singleton Patterns
Software Design Lecture : 28.
Message Passing Systems
Presentation transcript:

14. Factory Pattern SE2811 Software Component Design Dr. Rob Hasker (based on slides by Dr. Mark Hornick) 14. Factory Pattern

Issue: multiple types of clocks See time example in sample code folder Class Time: hours + minutes, where hours can grow forever Class Clock: hours + minutes as captured on 24-hour clock Review test code for each AbstractTime: abstracts the two clocks makeTime(int hours, int minutes, String type)

Issue: multiple types of clocks See time example in sample code folder Class Time: hours + minutes, where hours can grow forever Class Clock: hours + minutes as captured on 24-hour clock Review test code for each AbstractTime: abstracts the two clocks makeTime(int hours, int minutes, String type) { if (type.equalsIgnoreCase("time")) return new Time(hours, minutes); else return new Clock(hours, minutes); } Known as a static factory method Discuss dangers of depending on subclasses in general Introduces dependency (coupling) on subclass!

Static factory methods Also: Clock.fromString, Time.fromString Generally: method to parse objects from inputs General form: public class X { … public static X makeX(someType someData) { return new X(…); }

Ducks a-swimming Problem: modeling ducks in a pond Solution? Each type of duck: particular swimming pattern Eg: straight lines vs. circular Each type of duck also has a particular quack Mallard, canvasback, black-bellied whistler Solution? Strategy: abstract move, abstract quack

Applying the strategy pattern… // create some behaviors SwimBehavior csb = new CircularSwimming(); QuackBehavior sqb = new StandardQuacking(); SwimBehavior rsb = new RandomFloating(); // 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();

How this looks in the application… SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List<Duck> ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style); }

How this looks in the application… SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List<Duck> ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); }

How this looks in the application… SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List<Duck> ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); } But what happens when we have more behaviors?

How this looks in the application… SwimBehavior circ_swimmer = new CircularSwimming(); SwimBehavior rand_floater = new RandomFloating(); QuackBehavior std_quacker = new StandardQuacking(); QuackBehavior silent = new NoQuacking(); List<Duck> ducks = new LinkedList<>(); String swim_type = in.next(); String quack_type = in.next(); String name = in.next(); while ( !swim_type.equals(“done”) ) { SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; ducks.add(new Duck(name, swim_style, quack_style)); } But what happens when we have more behaviors? Operation? Class?

Duck class, Simple Factory Idiom method solution

Duck class, Simple Factory Idiom method solution public static Duck createDuck(Scanner in) { String swim_type = in.next(), quack_type = in.next(), name = in.next(); SwimBehavior swim_style = swim_type.equals(“circular”) ? circ_swimmer : rand_floater; QuackBehavior quack_style = quack_type.equals(“standard”) ? std_quacker : silent; return new Duck(name, swim_style, quack_style); }

Duck class, Simple Factory Idiom Alternative: create a class DuckFactory method solution public class DuckFactory { public Duck grow(String swim_type, String quack_type) { … }

Duck class, Simple Factory Idiom Alternative: create a class DuckFactory method solution public class DuckFactory { public Duck grow(String swim_type, String quack_type) { … } Yes, just pushed problem into another object But, have one place to maintain duck creation Alternative: static methods – but can’t subclass

Issue: Direct instantiation problem The issue/problem/context: A client needs to create one of several (or many) types of (similar) objects Creation of objects may need to be happen within various differing locations within the app (distributed creation) Client doesn’t want to know specifically what kind of object to create 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

Scenario: Client directly creates class instances

Factory Pattern Solution Define an interface for object creation methods in an abstract Factory class that can be used by the Client whenever concrete objects (aka Products) need to be created The interface consists of methods known as Factory Methods Let some concrete subclass decide specifics By providing an implementation of the Factory Methods This delegates the decision of what/how to create to the concrete subclasses

Factory Pattern The interface consists of Factory Methods Creation is not done via constructor methods because constructor methods cannot be overridden

Factory pattern The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker)

Factory pattern The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker

Factory pattern Why not move the if statement to the constructor? The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker

Factory pattern Why not move the if statement to the constructor? Constructor doesn’t “return” an object! It just initializes the current object Factory pattern The concrete factory (USMoneyMint) implements a single Factory Method (createCurrencyMaker), which instantiates concrete Products (DollarBillMaker or DollarCoinMaker) But the client still has to create the Factory (USMoneyMint) that creates the CurrencyMakers There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker

Abstract Factory Pattern The next level of extension of the Factory concept The products created by are sufficiently different to warrant separate Factories Whose “factory methods” are similar Students will not be responsible for this pattern unless it’s covered during the week 10 presentations

Abstract Factory The abstract factory (MoneyMint) defines the Factory Method implemented by the concrete factories, which is used by the client to create CurrencyMakers But the client still has to create the concrete Factories (USMoneyMint), but can refer to them abstractly (via MoneyMint references) The products created by are sufficiently different to warrant separate Factories (USMoneyMint and CanadianMoneyMint), each of which “knows” which products to make

Review Goal: decouple object creation from client code Simple solution: static factory method – not extensible But is a good model for parsing inputs into objects Factory class: supports multiple generation methods Abstract Factory Class: multiple product lines Example: apply factory pattern to encrypting strings