Applying the Factory Method Design Pattern

Similar documents
Using Design Patterns in Java Application Development

Applying the Observer Design Pattern

Applying the Decorator Design Pattern

g Baking with OO Goodness

Design Patterns. CSCE Lecture 17-10/28/2015 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

TDDB84. Lecture 2. fredag 6 september 13

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

SDC Design patterns GoF

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

If we aren t supposed to program to an implementation then how can we actually create new things? Reptile reptile = new Turtle(); Software Engineering

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design Patterns. An introduction

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

The Strategy Pattern Design Principle: Design Principle: Design Principle:

COMP 6471 Software Design Methodologies

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Object-Oriented Design

DESIGN PATTERN - INTERVIEW QUESTIONS

Factories, Builders and Singletons. Steven R. Bagley

Object-Oriented Oriented Programming

What is Design Patterns?

Object-Oriented Design

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Object-oriented Software Design Patterns

Design patterns. Jef De Smedt Beta VZW

The Factory Method Design Pattern

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Application Architectures, Design Patterns

1 Software Architecture

The GoF Design Patterns Reference

Design Patterns. Gunnar Gotshalks A4-1

Introduction to Software Engineering: Object Design I Reuse & Patterns

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Software Design COSC 4353/6353 D R. R A J S I N G H

Review Software Engineering October, 7, Adrian Iftene

Tuesday, October 4. Announcements

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Topics in Object-Oriented Design Patterns

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

What is Design Patterns?

Object Oriented Paradigm

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

CS251 Software Engineering Lectures 18: Intro to DP

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Recognizing And Understanding AntiPatterns In Java Application Development

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Design Patterns Cont. CSE 110 Discussion - Week 9

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

SOLID DESIGN PATTERNS FOR MERE MORTALS

Brief Note on Design Pattern

3 Product Management Anti-Patterns by Thomas Schranz

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Design Patterns. CSC207 Fall 2017

CS/CE 2336 Computer Science II

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

What is Design Patterns?

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued)

An Introduction to Patterns

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Extensibility Design Patterns From The Initial Stage of Application Life-Cycle

A Rapid Overview of UML

Summary of the course lectures

Design Patterns: Structural and Behavioural

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Design patterns. OOD Lecture 6

A Reconnaissance on Design Patterns

An Introduction to Patterns

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Introduction and History

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

A Primer on Design Patterns

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Design Patterns. CSC207 Fall 2017

Design Patterns. CSC207 Winter 2017

UNIT I Introduction to Design Patterns

Analysis of Design Patterns in Java with Rational Rhapsody as the New Building Blocks of Software Engineering

DESIGN PATTERNS FOR MERE MORTALS

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Applying Design Patterns to SCA Implementations

Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series

A Metric for Measuring the Abstraction Level of Design Patterns

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

DESIGN PATTERNS MOCK TEST DESIGN PATTERNS MOCK TEST II

A Metric of the Relative Abstraction Level of Software Patterns

Lecture 13: Design Patterns

UNIT I Introduction to Design Patterns

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Transcription:

Applying the Factory Method Design Pattern Trenton Computer Festival Professional Seminars Michael P. Redlich (908) 730-3416 michael.p.redlich@exxonmobil.com

About Myself Degree B.S. in Computer Science Rutgers University (go Scarlet Knights!) ExxonMobil Research & Engineering Clinton, New Jersey Senior Research Technician (1988-1998, 2004-present) Systems Analyst (1998-2002) Ai-Logix, Inc. Somerset, New Jersey Technical Support Engineer (2003-2004) 1

About Myself (continued) ACGNJ Java Users Group Leader Secretary Publications Avoid Excessive Subclassing with the Decorator Design Pattern + Barry Burd and Michael Redlich + Java Boutique, January 27, 2006 James: The Java Apache Mail Enterprise Server + Barry Burd and Michael Redlich + Java Boutique, September 30, 2005 2

Example Source Code The example source code was adapted from: Head First Design Patterns + Eric & Elisabeth Freeman (with Kathy Sierra & Bert Bates) Download example source code from: http://tcf.redlich.net/ 3

Gang of Four (GoF) Erich Gamma Richard Helm Ralph Johnson John Vlissides Design Patterns Elements of Reusable Object- Oriented Software ISBN 0-201-63361-2 1995 4

Gang of Four (GoF) Next Generation? Eric Freeman Elisabeth Freeman Kathy Sierra Bert Bates Head First Design Patterns ISBN 0-596-00712-4 2004 5

What are Design Patterns? A pattern is a solution to a problem in a context The context is the situation in which the pattern applies The problem refers to the desired goal in the context, but also refers to any constraints that may occur The solution is a general design that anyone can apply If you find yourself in a context with a problem that has a goal that is affected by a set of constraints, then you can apply a design that resolves the goal and constraints and leads to a solution. 6

What are Design Patterns? (continued) Recurring solutions to software design problems that are repeatedly found in real-world application development All about the design and interaction of objects Four essential elements: The pattern name The problem The solution The consequences 7

How Design Patterns Solve Design Problems Find appropriate objects Helps identify less obvious abstractions Design for change Avoid creating objects directly Avoid dependencies on specific operations Avoid algorithmic dependencies Avoid tight coupling 8

Thinking in Design Patterns Keep it simple Goal should be simplicity Design patterns are not a magic bullet No plug and play Know when to apply a design pattern Ensure that a pattern fits the design Consider patterns during refactoring Goal is to improve structure, not behavior Don t be afraid to remove a design pattern Especially if design has become too complex 9

Design Pattern Categories Creational Abstracts the instantiation process Dynamically create objects so that they don t have to be instantiated directly Structural Composes groups of objects into larger structures Behavioral Defines communication among objects in a given system Provides better control of flow in a complex application 10

Creational Patterns Abstract Factory Provides an interface for creating related objects without specifying their concrete classes Builder Reuses the construction process of a complex object Factory Method Lets subclasses decide which class to instantiate from a defined interface Prototype Creates new objects by copying a prototype 11

Creational Patterns (continued) Singleton Ensures a class has only one instance with a global point of access to it 12

Structural Patterns Adapter Converts the interface of one class to an interface of another Bridge Decouples an abstraction from its implementation Composite Composes objects into tree structures to represent hierarchies Decorator Attaches responsibilities to an object dynamically 13

Structural Patterns (continued) Façade Provides a unified interface to a set of interfaces Flyweight Supports large numbers of fine-grained objects by sharing Proxy Provides a surrogate for another object to control access to it 14

Behavioral Patterns Chain of Responsibility Passes a request along a chain of objects until the appropriate one handles it Command Encapsulates a request as an object Interpreter Defines a representation and an interpreter for a language grammar Iterator Provides a way to access elements of an object sequentially without exposing its implementation 15

Behavioral Patterns (continued) Mediator Defines an object that encapsulates how a set of objects interact Memento Captures an object s internal state so that it can be later restored to that state if necessary Observer Defines a one-to-many dependency among objects State Allows an object to alter its behavior when its internal state changes 16

Behavioral Patterns (continued) Strategy Encapsulates a set of algorithms individually and makes them interchangeable Template Method Lets subclasses redefine certain steps of an algortithm Visitor Defines a new operation without changing the classes on which it operates 17

Pizza Store Application Objective: Develop a pizza shop application Plan for expansion 18

public class PizzaStore { IS THIS A GOOD APPROACH? public Pizza orderpizza(string type) { Pizza pizza = null; if(type.equals( cheese )) } } pizza = new CheesePizza(); Area of change else if(type.equals( pepperoni )) pizza = new PepperoniPizza(); pizza.prepare(); pizza.bake(); Area that we expect to remain unchanged pizza.cut(); pizza.box(); return pizza; 19

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(); return pizza; } } 20

public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderpizza(string type) { Pizza pizza = factory.createpizza(type); pizza.prepare(); pizza.bake(); } } pizza.cut(); pizza.box(); return pizza; Notice how the createpizza() method eliminates the need to use the new keyword 21

Simple Factory PizzaStore factory SimplePizzaFactory pizza Pizza orderpizza createpizza prepare bake cut box CheesePizza VeggiePizza PepperoniPizza ClamPizza 22

Expanding the Pizza Store PizzaStore All pizza store franchises should leverage the PizzaStore code so that pizzas are prepared the same way. NYPizzaFactory ChicagoPizzaFactory This franchise likes to make pizza with thin crust, tasty sauce, and just a little cheese. This franchise likes to make pizza with thick crust, rich sauce and lots of cheese. 23

public class PizzaTestDrive { // instance variables... IS THIS A BETTER APPROACH? NYPizzaFactory nyfactory = new NYPizzaFactory(); PizzaStore nystore = new PizzaStore(nyFactory); nystore.orderpizza( pepperoni ); Here we get NY style pizza ChicagoPizzaFactory chicagofactory = new ChicagoFactory(); PizzaStore chicagostore = new PizzaStore(chicagoFactory); chicagostore.orderpizza( pepperoni ); } Here we get Chicago style pizza 24

Factory Method Intent Defines an interface for creating an object, but lets subclasses decide which class to instantiate Lets a class defer instantiation to subclasses Also known as Virtual Constructor Motivation To solve the problem of one class knowing when to create a class of another type, but not knowing what kind of class to create 25

Factory Method (continued) Design Principle Depend upon abstractions; do not depend upon concrete classes Use this pattern when: A class can t anticipate the class of objects is must create A class would prefer for its subclasses to specify the objects it creates There is a need for a class to localize one of several helper classes that can be delegated a responsibility 26

Factory Method (continued) 27

Pizza Store Creator Classes PizzaStore createpizza orderpizza Product Classes Pizza prepare bake cut box NYPizzaStore createpizza ChicagoPizzaStore createpizza NYStyleCheesePizza ChicagoStyleCheesePizza NYStylePepperoniPizza ChicagoStylePepperoniPizza 28

And Now for the code review and demonstration! 29

Resources Design Patterns Elements of Reusable Object- Oriented Software Erich Gamma, et. al ISBN 0-201-63361-2 Head First Design Patterns Eric & Elisabeth Freeman (with Kathy Sierra & Bert Bates) ISBN 0-596-00712-4 http://www.wickedlysmart.com/ Java Design Patterns James W. Cooper ISBN 0-201-48539-7 http://www.patterndepot.com/put/8/javapatterns.ht m 30

Resources (continued) UML Distilled Martin Fowler (with Kendall Scott) ISBN 0-201-32563-2 Data & Object Factory http://www.dofactory.com/ 31