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

Slides:



Advertisements
Similar presentations
Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
OOMPA Lecture 10 Design Patterns Singleton Factory Method
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
1 Creational Patterns CS : Software Design Winter /T8.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Software Components Creational Patterns.
Abstract Factory Abstract Factory using Factory Method.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
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.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
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.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Design Patterns Introduction
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
07 - Creational PatternsCSC4071 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
1 DesignPattern-3 Creational Patterns Suntae Kim Kangwon National University
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Pattern.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Patterns: Brief Examples
Design Pattern Catalogues
Low Budget Productions, LLC
Factory Patterns 1.
Software Design and Architecture
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
UNIT-III Creational Patterns UNIT-III.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
CS 350 – Software Design Singleton – Chapter 21
Creational Patterns.
Software Design Lecture 9.
Presentation transcript:

Creational Design Patterns

Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented. Two types 1. Class creational Uses inheritance to vary the class to be instantiated 2. Object creational Delegates instantiation to another object

Creational Patterns Creational Patterns are important as systems depend more on object composition Creational Patterns Encapsulate concrete classes system uses Hide how instances of these classes are created

Example: To build a Maze

Maze example interface Site { void enter() ; } class Room implements Site { public Room(int number) { roomNumber = number ; } public Site getSide(int direction) { return sides[direction] ; } public void setSide(int direction, Site m) { sides[direction] = m ; } public void enter() { } private Site[] sides = new Site[4] ; private int roomNumber ; } class Wall implements Site { public Wall() { } public void enter() { } } class Door implements Site { public Door(Room r1, Room r2) { room1 = r1 ; room2 = r2 ; } public void enter() { } private Room room1, room2 ; private boolean isOpen ; }

Maze Example class Maze { public static int NORTH = 0 ; public static int SOUTH = 1 ; public static int EAST = 2 ; public static int WEST = 3 ; public Maze() { } public void addRoom(Room r) { rooms.add(r) ; } public Room roomNo(int idx) { return (Room) rooms.get(idx) ; } List rooms = new ArrayList() ; }

Maze Example class MazeGame { public Maze createMaze() { Maze maze = new Maze() ; Room room1 = new Room(1) ; Room room2 = new Room(2) ; Door door = new Door(room1,room2) ; maze.addRoom(room1) ; maze.addRoom(room2) ; room1.setSide(Maze.NORTH,new Wall()) ; room1.setSide(Maze.EAST,door) ; room1.setSide(Maze.SOUTH,new Wall()) ; room1.setSide(Maze.WEST,new Wall()) ; room2.setSide(Maze.NORTH,new Wall()) ; room2.setSide(Maze.EAST,new Wall()) ; room2.setSide(Maze.SOUTH,new Wall()) ; room2.setSide(Maze.WEST,door) ; return maze ; }

Factory Method Intent: Define an interface for creating an object, but let subclasses decide which cass to instantiate. Motivation: Example: Framework of Abstract classes Abstract classes: Document, Application Application has Open, New, etc. to create new documents Application cannot know which concrete document to instant Concrete classes: DrawingDocument, DrawingApplication

Complex Constructors class Complex { 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); // fromCartesian(-1, 0)

Factory Method Solution CreateDocument() = Factory Method

Factory Method Applicability: Use the Factory Method when A class can t anticipate the class of objects it must create A class wants its subclasses to specify the objects it creates

Factory Method

Factory Method - Participants Product (Document) The interface of objects the Factory Method creates ConcreteProduct (MyDocument) Implements the product interface Creator (Application) Declares the factory method which returns an object of type Product ConcreteCreator (MyApplication) Defines the Factory method to returnn an instance of ConcreteProduct

Factory Method Implementation Abstract Creator Class v.s. Concrete Creator Class Parameterized Factory Method Creator can keep the Class Info to instantiate (Can avoid sub classing) To use naming conventions

Factory Methods in Maze Example public class MazeGame { Maze newMaze() { return new Maze() ; } Room newRoom(int n) { return new Room(n) ; } Wall newWall() { return new Wall() ; } Door newDoor(Room r1, Room r2) { return new Door(r1,r2) ; } public Maze createMaze() { Maze maze = newMaze() ; Room room1 = newRoom(1) ; Room room2 = newRoom(2) ; Door door = newDoor(room1,room2) ; ……… return maze ; }

Customized Maze Components class BombedWall extends Wall { } class RoomWithABomb extends Room { RoomWithABomb(int n) { super(n) ; } } class BombedMazeGame extends MazeGame { Wall newWall() { return new BombedWall() ; } Room newRoom(int n) { return new RoomWithABomb(n) ; } }

Abstract Factory Intent: Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Motivation: User interface Toolkit supporting multiple look- and- feel standards. (Widgets like Scroll Bars, Windows, Buttons etc.) Not to hard code these widgets for a particular look-and-feel otherwise hard to change it We can define a WidgetFactory interface for creating each basic entity

Abstract Factory Example

Abstract Factory - Applicability Use Abstract Factory if A system must be independent of how its products are created A system should be configured with one of multiple families of products A family of related objects must be used together You want to reveal only interfaces of a family of products and not their implementations

Abstract Factory - Structure

Abstract Factory - Participants AbstractFactory (WidgetFactory) Declares an interface of methods to create abstract product objects ConcreteFactory (MotifWidgetFactory, … ) Implements the methods to create concrete product objects AbstractProduct (Window, ScrollBar) Declares an interface for a product type ConcreteProduct (MotifWindow, MotifScrollBar) Defines a product object Implements the AbstractProduct interface Client Uses only interfaces declared by AbstractFactory and AbstractProduct

Abstract Factory Implementation Factory better to be a Singleton A new Concrete Factory for each Platform. Or alternatively a single Concrete Factory keeping its Classes of Products. Extending the Factories. (Adding a new Product)

Abstract Factory – Maze Example class MazeFactory { Maze newMaze() { return new Maze() ; } Room newRoom(int n) { return new Room(n) ; } Wall newWall() { return new Wall() ; } Door newDoor(Room r1, Room r2) { return new Door(r1,r2) ; } } public class MazeGame { public Maze createMaze(MazeFactory factory) { Maze maze = factory.newMaze() ; Room room1 = factory.newRoom(1) ; Room room2 = factory.newRoom(2) ; Door door = factory.newDoor(room1,room2) ; ……… return maze ; }

Customizing Maze Factory class BombedWall extends Wall { } class RoomWithABomb extends Room { RoomWithABomb(int n) { super(n) ; } } class BombedMazeFactory extends MazeFactory { Wall newWall() { return new BombedWall() ; } Room newRoom(int n) { return new RoomWithABomb(n) ; } }

Singleton Intent: Ensure that a class has only one instance, and provide a global point of access to it. Use Singleton There must be exactly one instance of a class, and it must be accessible to clients from a well known access point. When this instance should be extensible by sub- classing

Singleton Define an Instance operation to access its unique instance. It must be a static method. Must create its own unique instance.

Singleton - benefits Controlled access to sole instance Reduced namespace May be sub-classed to refine operations Can Permit a variable number of instances More flexible than static methods

Singleton – Implementation Ensure a unique instance class Singleton { private static Singleton inst = null ; public static Singleton getInstance() { if (inst==null) inst = new Singleton() ; return inst ; } protected Singleton() { } } Subclassing the singleton class Put instance() method in each subclass

Singleton – Maze example class MazeFactory { protected MazeFactory() { } private static MazeFactory inst = null ; public static MazeFactory getInst() { if (inst==null) inst = new MazeFactory() ; return inst ; } Maze newMaze() { return new Maze() ; } Room newRoom(int n) { return new Room(n) ; } Wall newWall() { return new Wall() ; } Door newDoor(Room r1, Room r2) { return new Door(r1,r2) ; } }

Singleton – Maze example public class MazeGame { public Maze createMaze() { Maze maze = MazeFactory.getInst().newMaze() ; Room room1 = MazeFactory.getInst().newRoom(1) ; Room room2 = MazeFactory.getInst().newRoom(2) ; Door door = MazeFactory.getInst().newDoor(room1,room2) ; maze.addRoom(room1) ; maze.addRoom(room2) ; room1.setSide(Maze.NORTH,new Wall()) ; room1.setSide(Maze.EAST,door) ; room1.setSide(Maze.SOUTH,new Wall()) ; room1.setSide(Maze.WEST,new Wall()) ; room2.setSide(Maze.NORTH,new Wall()) ; room2.setSide(Maze.EAST,new Wall()) ; room2.setSide(Maze.SOUTH,new Wall()) ; room2.setSide(Maze.WEST,door) ; return maze ; }

Singleton – Alternative Maze Factory class MazeFactory { protected MazeFactory() { } private static final String name = "BOMBED" ; private static MazeFactory inst = null ; public static MazeFactory getInst() { if (inst==null) { if (name.equals("BOMBED")) inst = new BombedMazeFactory() ; else inst = new MazeFactory() ; } return inst ; } Maze newMaze() { return new Maze() ; } Room newRoom(int n) { return new Room(n) ; } Wall newWall() { return new Wall() ; } Door newDoor(Room r1, Room r2) { return new Door(r1,r2) ; } }