CS 210 Introduction to Design Patterns September 19 th, 2006.

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

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
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.
NJIT More GRASP Patterns Chapter 22 Applying UML and Patterns Craig Larman Prepared By: Krishnendu Banerjee.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
UML Class Diagram: class Rectangle
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 210 Introduction to Design Patterns September 28 th, 2006.
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.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Beware of bugs in the above code; I have only proved it correct, not tried it.
Abstract Factory Abstract Factory using Factory Method.
Factory Patterns. RHS – SOC 2 Being less concrete One important OO principle is: ”Program to an interface, not an implementation” Interfaces reduces the.
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.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
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.
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.
CSC 313 – Advanced Programming Topics. Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior;
Builder An Object Creational Pattern Tim Rice CSPP51023 March 2, 2010.
CS 415 N-Tier Application Development By Umair Ashraf June 22,2013 National University of Computer and Emerging Sciences Lecture # 3 Design Patterns (Observer,Factory,Singleton)
Religious Studies 313 – Advanced Programming Topics.
CS 210 Introduction to Design Patterns August 29, 2006.
The Factory Pattern Sanjay Yadav (ISE ).
CSC 480 Software Engineering Design With Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Pat’s Pizza creation Pizza orderPizza(String type){ Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if type.equals(“greek”))
Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
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.
SE 461 Software Patterns. ABSTRACT FACTORY PATTERN.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
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.
SE 461 Software Patterns Welcome to Design Patterns.
Factory Method Pattern
Chapter 10 Design Patterns.
Low Budget Productions, LLC
Factory Patterns 1.
Software Design and Architecture
More Interfaces, Dynamic Binding, and Polymorphism
Factory Method Pattern
Strategy Design Pattern
UML Class Diagram: class Rectangle
Programming Design Patterns
Abstract Factory Pattern
Object Oriented Design Patterns - Creational Patterns
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Factory Pattern.
ADAPTER FAÇADE FACTORY OBSERVER SINGLETON STRATEGY
14. Factory Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
Design by Abstraction (Continuation) CS 3331 Spring 2005
CSC 480 Software Engineering
Software Design Lecture : 28.
Presentation transcript:

CS 210 Introduction to Design Patterns September 19 th, 2006

Head First Design Patterns Chapter 4 Factory Pattern

Calling the constructor Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new DecoyDuck(); } else if (inBathTub) { duck = new RubberDuck(); } This type of code will often lead to problems when new types have to be added.

Another example in similar vein Pizza orderPizza(String type){ if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if type.equals(“greek”)) { pizza = new GreekPizza(); } else if type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box() }

Identifying the aspects that vary If the pizza shop decides to change the types of pizza it offers, the orderPizza method has to be changed.

Another example in similar vein Pizza orderPizza(String type){ if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if type.equals(“greek”)) { pizza = new GreekPizza(); } else if type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box() } Part that varies. Part that remains constant

Encapsulating object creation if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if type.equals(“greek”)) { pizza = new GreekPizza(); } else if type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } SimplePizzaFactory

Building a simple pizza factory public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("cheese")) { pizza = new CheesePizza(); } else if (type.equals("pepperoni")) { pizza = new PepperoniPizza(); } else if (type.equals("clam")) { pizza = new ClamPizza(); } else if (type.equals("veggie")) { pizza = new VeggiePizza(); } return pizza; }

Reworking the PizzaStore Class public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

Complete example for Simple Factory SimplePizzaFactory factory = new SimplePizzaFactory(); PizzaStore store = new PizzaStore(factory); Pizza pizza = store.orderPizza("cheese"); Look at Eclipse

Simple Factory Defined PizzaStore orderPizza() SimplePizzaFactory createPizza() Pizza prepare() bake() cut() box() CheesePizza VeggiePizza ClamPizza

Creating multiple factories PizzaStore NYPizzaFactory ChicagoPizzaFactory

Creating multiple instances NYPizzaFactory nyFactory = new NYPizzaFactory(); PizzaStore nyStore = new PizzaStore(nyFactory); Pizza pizza = nyStore.orderPizza("cheese"); ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory(); PizzaStore chicagoStore = new PizzaStore(chicagoFactory); Pizza pizza = chicagoStore.orderPizza("cheese");

Alternate approach – Abstract method – a framework for the pizza store public abstract class PizzaStore { abstract Pizza createPizza(String item); public Pizza orderPizza(String type) { Pizza pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

Allowing the subclasses to decide PizzaStore createPizza() orderPizza() NYStylePizzaStore createPizza() ChicagoStylePizzaStore createPizza() A factory method handles object creation and encapsulates it in the subclass. This decouples the client code in the super class from the object creation that happens in the subclass.

Factory Method Pattern PizzaStore createPizza() orderPizza() NYStylePizzaStore createPizza() ChicagoStylePizzaStore createPizza() PizzaNYStyleCheesePizza ChStyleCheesePizza Creator Classes Product Classes

Factory Method Pattern defined The factory method pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclass.