The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.

Slides:



Advertisements
Similar presentations
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Generator Design Patterns: The Factory Patterns.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Design Patterns.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Creational Patterns (1) CS350, SE310, Fall, 2010.
Abstract Factory Design Pattern making abstract things.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
The Adapter Pattern SE-2811 Dr. Mark L. Hornick 1.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
The Façade Pattern SE-2811 Dr. Mark L. Hornick 1.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Object Oriented Software Development
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
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.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Object Oriented Programming
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Introduction to Object-Oriented Programming Lesson 2.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
// 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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Design Patterns: MORE Examples
Factory Method Pattern
Low Budget Productions, LLC
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Inheritance and Polymorphism
Software Design and Architecture
Factory Method Pattern
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
SE-2811 Software Component Design
Lesson 5: More on Creational Patterns
Fundaments of Game Design
14. Factory Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
Presentation transcript:

The Factory Patterns SE-2811 Dr. Mark L. Hornick 1

Direct instantiation is a disadvantage 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 SE-2811 Dr. Mark L. Hornick 3

Factory Design 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 SE-2811 Dr. Mark L. Hornick 5 Creation is not done via constructor methods because constructor methods cannot be overridden The interface consists of Factory Methods

Factory pattern SE-2811 Dr. Mark L. Hornick 6 There are two Products being built here by the USMoneyMint Factory – DollarCoinMaker and DollarBillMaker 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

Factory Summary Defines an interface (Factory Methods), implemented in a concrete Factory, for creating Product objects The interface specifies a special method (Factory Method) that is invoked by clients Creation is not done via constructor methods because constructor methods cannot be overridden in classes that extend the abstract class SE-2811 Dr. Mark L. Hornick 7

Abstract Factory classes 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 SE-2811 Dr. Mark L. Hornick 8

Abstract Factory 9 The products created by are sufficiently different to warrant separate Factories (USMoneyMint and CanadianMoneyMint), each of which “knows” which products to make 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)

Abstract Factory Abstract classes MoneyMint (abstract factory) CurrencyMaker (abstract product) USMoneyMint (concrete factory) creates instances of CurrencyMakers (concrete Product) But only concrete USMoneyMint class knows the concrete type of CurrencyMaker (DollarBillMaker, DollarCoinMaker) to create The framework (client) used by the client app only references abstract CurrencyMaker classes The client application should only access the framework’s abstract classes (whenever possible)

Abstract Factory Pattern (generic) SE-2811 Dr. Mark L. Hornick 11

Extending the abstraction further The Concrete Factories (USMoneyMint and CanadianMoneyMint) still have to be created by the client app So we still have the client app dealing with non- abstract classes and using “new” But we can even abstract this away by using static methods in the Abstract Factory class…

Abstract Factory method 13 Here, the Abstract Factory (MoneyMint) exposes a static factory method (createMint) that can be called to create the concrete factories (the US and Canadian Mints). In addition, the client is forced to use the createMint method, since the concrete classes have protected constructors. The client app only has to reference the abstract classes in this configuration.

Abstract Factory Implementation Abstract Factory is a true abstraction The client only deals with the Abstract Factory class The client never creates concrete Factories Subclasses of the abstract factory class “decide” what concrete object to create, based on the subclasses actual implementation of the factory method This allows the abstract (Factory) class defer instantiation (of the Product) to subclasses at runtime Parameterized abstract factory methods Multiple product variants Choice selected by parameter

Abstract Factory Advantages Provides “hooks” for additional subclasses Permits subclass to modify product creation by overriding the abstract factory method But base class can provide a default if needed!

Abstract Factory - Disadvantages Supporting new products is not easy The factory interface has to be extended for each new product Changing the Abstract Factory class interface changes all its subclasses

Overriding design principles motivating the Factory patterns No variable should hold a reference to a concrete class No class should derive from a concrete class No method should override an implemented method of any of its base classes These are guidelines that we strive for, but cannot always follow 100% SE-2811 Dr. Mark L. Hornick 17