Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
More Interfaces, Dynamic Binding, and Polymorphism Kirk Scott.
 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 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Design Patterns.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
CS 210 Introduction to Design Patterns September 28 th, 2006.
Case Studies on Design Patterns Design Refinements Examples.
CSSE 374: Introduction to Gang of Four Design Patterns
Creational Patterns (1) CS350, SE310, Fall, 2010.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Beware of bugs in the above code; I have only proved it correct, not tried it.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
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.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
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.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
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.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Go4 Template Method, Strategy, State Patterns Presented By: Matt Wilson.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
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.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
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
Abstract Factory Pattern
Factory Method Pattern
Design Patterns Spring 2017.
Low Budget Productions, LLC
Factory Patterns 1.
Software Design and Architecture
More Interfaces, Dynamic Binding, and Polymorphism
Factory Method Pattern
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Abstract Factory Pattern
Object Oriented Design Patterns - Creational Patterns
Object Oriented Design Patterns - Structural Patterns
Factory Pattern.
Dependency Inversion principle
Software Design Lecture : 28.
Presentation transcript:

Tech Talk Go4 Factory Patterns Presented By: Matt Wilson

Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct program) developers who wanted to continue to get together on occasion to discuss technical topics.  Sources used here included the Gang of 4 Design Patterns Book, Head First Design Patterns book, and many online articles and discussion boards on the topic.

What-is/Why-use a Factory pattern? 3  Encapsulates creation in one place.  Decouples clients from having to know about Subtypes.  When you are making a library and want to provide a creational interface for your Clients.  When you want to have the ability for Random types to be created at runtime. Example: A game that generates random types of enemies from an enemy “factory”.

No Factory Example 4 Hot dog store! White board it – No Factory. Any OO design problems seen here? (psudocode) Hotdog HotDogStore::orderHotDog(type) { Hotdog * dog = null; If (type == corndog) { dog = new CornDog(); } else if (type == PolishDog) { dog = new PolishDog(); } return dog; }

No Factory - Problems 5 Violates two of the five fundamental Principals of SOLID OO Design:SOLID 1) Violates Open Closed Principle Open for extension, but closed for modification Or as I like to put it: Writing new code shouldn’t require a change to existing code New HotDog types would require Client Code to change to support them. Violation makes code fragile and breaks existing code. 2) Violates Dependency Inversion Principle: Classes shouldn’t have references to Concrete Classes Violation creates dependency problems, longer compile times, and prohibits Unit Testing/dependency injection.

Factory attempt 1 6 White board it… what do you think? Factory encapsulates the logic of creating types. Client no longer depends on Concrete Products. Hotdog HotDogStore::orderHotDog(type) { Hotdog * dog = null; dog = HotDogSimpleFactory::CreateDog(type); return dog; }

Simple Factory 7 Better, now the Client doesn’t depend upon Concrete products, and Creation is encapsulated, but this is not yet Factory Method Commonly used, but not a Design Pattern. Called “Simple Factory” or “Static Factory” if Static.

Factory Method 8  Why Factory Method?  Simple Factory may be the correct choice if you have a simple program.  The intent of Factory Method is "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.“  Why would you want to do this? Because it creates an extensible framework for adding future factories and products.  Use when you are going to have one or more families of classes

Factory Method UML 9  Creator may be  pure interface,  abstract or even  concrete  Enables you to extend  your factory with  new products  and creators

Factory Method – Multiple Concrete Types 10 Client can now chose one or more factories to instantiate to produce different products.

Factory Method - Example 11  Extend HotDogStore (Whiteboard) and Show how you can have multiple concrete creators and products.  This extensible Framework can support many different clients that may be using your classes in different ways. Example: Hotdog ChicagoHotDogStore::orderHotDog(type) { Hotdog * dog = null; HotDogFactory chicagoDogs = new ChicagoHotDogFactory (); dog = chicagoDogs.createHotDog(type); return dog; } or Hotdog NewYorkHotDogStore::orderHotDog(type) { Hotdog * dog = null; HotDogFactory newYorkDogs = new NewYorkHotDogFactory (); dog = newYorkDogs.createHotDog(type); return dog; }

Factory Method - Example 12  Clearly the Factory pattern is a Framework for extending the software with new types in the future.  Enables you to effectively extend your system with new Products and Factories, without effecting Client Code.  Note: Passing in “Type” is optional, and often not used as part of Factory Method.

Factory Method Questions ? 13

Abstract Factory 14  The intent of Abstract Factory is "Provide an interface for creating families of related or dependent objects without specifying their concrete classes."  Or put another way: "The intent of an Abstract Factory is to provide an interface for a creating a set of related objects.“  Biggest difference between Abstract Factory and Factory Method, Abstract Factory defines an interface with multiple create methods, whereas Factory Method has a single method.  Factory method API: “CreateHotDog”  Abstract Factory API: “CreateLettuce, CreateBun, CreatePickle, etc.”

Abstract Factory 15 *Compare to Factory Method. Very similar, but with multiple Create methods.

Abstract Factory 16  Abstract Factory provides an API to create not only 1, but n number of related objects.  In once sense,  Factory Method is an Abstract Factory with only one create method, or...  Abstract Factory is a Factory Method with multiple create methods.  See diagram below for a good Abstract Factory example for abstracting away the type of Database a Client is communicating with.

Abstract Factory - Example 17

Abstract Factory 18  So why not just call Abstract Factory, “Factory Method N- number”? Why have two patterns that are so similar?  Because, the Go4 book defines Abstract Factory as simply an interface for creating products. It's up to Concrete subclass’ implementation to create them. The most common way to do this is to define a factory method for each product as shown above.  Although an Abstract Factory “most commonly” uses Factory Methods they are not limited to them.  Alternatives include (but are not limited to) Abstract Factories that use:  BuilderPattern (builds objects in a series of steps)  PrototypePattern (builds objects by copying and customizing a prototype of the object)

Questions ? 19