Low Budget Productions, LLC

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Creational Patterns (1) CS350, SE310, Fall, 2010.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Object Oriented Programming
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
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.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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.
Copyright © Jim Fawcett Spring 2017
Design Patterns: MORE Examples
Sections Inheritance and Abstract Classes
Factory Method Pattern
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Factory Patterns 1.
Inheritance and Polymorphism
Design Patterns C++ Java C#.
Software Design and Architecture
Factory Method Pattern
Interfaces and Inheritance
Presented by Igor Ivković
Refactoring II 21-Sep-18.
Informatics 122 Software Design II
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Refactoring II 5-Feb-19.
Lesson 5: More on Creational Patterns
Informatics 122 Software Design II
Object-Oriented PHP (1)
Presented by Igor Ivković
Extending Classes Through Inheritance
Presentation transcript:

Low Budget Productions, LLC Presents “The Factory Method Pattern”

Co-Executive Producers Trisha Rogers Gamma, Helm, Johnson and Vlissides Martin Fowler Wikipedia A host of internet writers Creative Consultant Mark Shacklette

Act I: Scene 1: Design for Change Open for extension but closed for modification Design system to evolve while maximizing code reuse and eliminating changes to existing classes and client classes Factory Method Pattern: Take the code that is likely to change over time and put it in its own class Create objects without specifying the exact class of the object that will be created Commit to the interface not the implementation Factory Method Pattern: defers instantiation to subclasses

Act I: Scene 2: Design pattern space Purpose: A creational pattern, concerned with the process of object creation Scope: Class scope Factory Method Pattern applies primarily to classes and their subclasses These relationships are established through inheritance and so are static - fixed at compile time. Factory Method Pattern deals with the dilemma of not knowing what the client will want until run-time.

Act I: Scene 3: Factory Method Purpose Problems: What if it is the nature of the application to have classes that change on a regular basis (new products, e.g.) and code re-writing will have to be done frequently? What if the classes are abstract and they do not know how the subclasses will be implemented? Solution: Factory Method Pattern can defer instantiation to subclasses, encapsulating the knowledge of which subclass to create and move this knowledge to a separate class. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate. It is called a Factory Pattern since it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code. Continue with slide 11

Act I: Scene 3: Factory Method Purpose Variation on the theme: Parameterized factory methods Problem: What if there is ambiguity in the meaning of the constructor parameters? Only one constructor is allowed per class, and while overloading will solve the problem of passing different types of parameters, it will not solve problem of the same type with different meanings. Solution: Create a factory class that will privatize the constructor and pull all the logic into one class. Provide different static methods to return the appropriate object depending on the meaning. Example to follow. Problem: You want a simple concrete creator class to handle object creation depending on a passed parameter? Solution: Create a factory class that will privatize the constructor and pull all the logic into one class. Write a switch statement to handle various parameters. If there is an addition of a sub-class, fix code in the factory class only. The interface does not change. Example to follow.

Act I: Scene 4: Where to use it Three basic situations Frameworks High level software written for an industry Abstract classes used to define and maintain relationships between objects A company in the industry will purchase the framework and implement the subclasses to its own specifications So if there aren’t many/any subclasses in the framework, how does the framework developer create objects? Encapsulates the knowledge of which subclass to create and moves this knowledge out of the framework Parallel hierarchies Application class cannot anticipate the subclass to be instantiated at run-time; it knows when, not what kind. “Little Factories” with logic in one place

Act I: Scene 5: UML per GoF

Act II: Scene 1: Old Approach

Act II: Scene 2: Factory Method

Act II: Scene 2: Factory Method Scenario: A bank has multiple types of accounts. These are subclasses of the class Account. There is a saving account class and a checking account class. The client uses a factory method class as an interface. When a new type of account is added – an IRA account – no changes need to be made to the abstract account class, the savings account class or the checking account class. More importantly, no changes need to be made to the client class. Only changes in the factory class need to be made to add the new product – IRA account. See the example in Eclipse.

Act II: Scene 3: Disambiguation Example How to use a factory method pattern to solve problem of multiple meanings for parameters: Make the constructor private Add one or more factory methods that are static and accessible from the public class. Example is from Wikipedia. class Complex { private Complex(double a, double b) { //... } public static Complex fromCartesian(double real, double imag) { return new Complex(real, imag); } public static Complex fromPolar(double modulus, double angle) { return new Complex(modulus * cos(angle), modulus * sin(angle)); Complex c = Complex.fromPolar(1, pi); // Same as fromCartesian(-1, 0)

Act III: Scene 1: Limitations The first limitation is that refactoring an existing class to use factories breaks existing clients. For example, if class Complex was a standard class, it might have numerous clients with code like: Complex c = new Complex(-1, 0); Once we realize that two different factories are needed, we change the class (to the code shown earlier). But since the constructor is now private, the existing client code no longer compiles. The second limitation is that, since the pattern relies on using a private constructor, the class cannot be extended. Any subclass must invoke the inherited constructor, but this cannot be done if that constructor is private. The third limitation is that, if we do extend the class (e.g., by making the constructor protected -- this is risky but possible), the subclass must provide its own re-implementation of all factory methods with exactly the same signatures. For example, if class StrangeComplex extends Complex, then unless StrangeComplex provides its own version of all factory methods, the call StrangeComplex.fromPolar(1, pi) will yield an instance of Complex (the superclass) rather than the expected instance of the subclass. Continue with slide 11

Coda: Never, ever follow student presenter Will Rodgers (the state pattern with the Pepsi machine guy) in any presentations. Ever.