31
Creational Design Patterns

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

Embed Size (px)

Citation preview

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

Creational Design Patterns

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

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 creationalUses inheritance to vary the class to be instantiated

2. Object creationalDelegates instantiation to another object

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

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

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

Example: To build a Maze

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

Maze exampleinterface 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 ; }

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

Maze Exampleclass 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() ;}

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

Maze Exampleclass 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 ;}

}

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

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

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

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)

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

Factory Method Solution

CreateDocument() = Factory Method

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

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

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

Factory Method

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

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

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

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

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

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 ;

}}

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

Customized Maze Componentsclass 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) ; }}

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

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

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

Abstract Factory Example

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

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

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

Abstract Factory - Structure

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

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

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

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)

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

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 ;

}}

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

Customizing Maze Factoryclass 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) ; }}

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

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

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

Singleton

Singleton Define an Instance operation to access its

unique instance. It must be a static method. Must create its own unique instance.

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

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

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

Singleton – Implementation

Ensure a unique instanceclass 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

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

Singleton – Maze exampleclass 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) ; }}

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

Singleton – Maze examplepublic 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 ;} }

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

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) ; }

}