Top Banner
Creational Design Patterns Creational Design Patterns Structural Design Patterns Behavioral Design Patterns
37

Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Jul 28, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational Design Patterns

Creational Design Patterns

Structural Design Patterns

Behavioral Design Patterns

Page 2: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

GoF Design Pattern Categories

Purpose

Creational Structural Behavioral

Scope Class Factory Method Adapter Interpreter

Template Method

Object Abstract Factory

Builder

Prototype

Singleton

Adapter

Bridge

Composite

Decorator

Facade

Proxy

Flyweight

Chain of Responsibility

Command

Iterator

Mediator

Memento

Observer

State

Strategy

Visitor

Page 3: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational Design Patterns

● Abstracts instantiation process

● Makes system independent of how its objects are

o created

o composed

o represented

● Encapsulates knowledge about which concrete classes the

system uses

● Hides how instances of these classes are created and put

together

Page 4: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational Design Patterns

● Design patterns that deal with object creation mechanisms,

trying to create objects in a manner suitable to the situation

● Make a system independent of the way in which objects are

created, composed and represented

● A class creational pattern uses inheritance to vary the class that's

instantiated, whereas an object creational pattern will delegate

instantiation to another object.

Page 5: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational Design Patterns

●Recurring themes:

oEncapsulate knowledge about which concrete

classes the system uses (so we can change them

easily later)

oHide how instances of these classes are created and

put together (so we can change it easily later)

Page 6: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Benefits of creational patterns● Creational patterns let you program to an interface defined

by an abstract class

● That lets you configure a system with “product” objects that

vary widely in structure and functionality

● Example: GUI systems

o InterViews GUI class library

oMultiple look-and-feels

o Abstract Factories for different screen components

Page 7: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Benefits of creational patterns

●Generic instantiation – Objects are instantiated

without having to identify a specific class type in

client code (Abstract Factory, Factory)

●Simplicity – Make instantiation easier: callers do

not have to write long complex code to instantiate

and set up an object (Builder, Prototype pattern)

●Creation constraints – Creational patterns can put

bounds on who can create objects, how they are

created, and when they are created

Page 8: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

Because the creational patterns are closely related, we'll study all five of

them together to highlight their similarities and differences.

We'll also use a common example building a maze for a computer game

to illustrate their implementations.

The maze and the game will vary slightly from pattern to pattern.

We'll ignore many details of what can be in a maze and whether a maze

game has a single or multiple players.

Instead, we'll just focus o n how mazes get created.

We define a maze as a set of rooms.

A room knows its neighbors; possible neighbors are another room, a wall,

or a door to another room.

Page 9: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

The classes Room, Door, and Wall define the components of the maze

used in all our examples.

We define only the parts of these classes that are important for creating

a maze.

We'll ignore players, operations for displaying and wandering around in a

maze, and other important functionality that isn't relevant to building

the maze.

Page 10: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

Page 11: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

The class MapSite is the common abstract class for all the components

of a maze

class MapSite {

public:

virtual void Enter()=0

}

Page 12: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

Room is the concrete subclass of MapSite that defines the key

relationships between components in the maze.

It maintains references to other MapSite objects and stores a room

number. The number will identify rooms in the maze.

Enter provides a simple basis for more sophisticated game operations.

For example, if you are in a room and say "GoEast," the game can simply

determine which MapSite is immediately to the east and then call Enter

on it.

Page 13: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

class Room : public MapSite {

public:

Room(int roomNo);

MapSite* GetSide(Direction) const;

void SetSide(Direction, MapSite*);

virtual void Enter();

private:

MapSite* _sides[4];

int _roomNumber;

};

Page 14: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

The following classes represent the wall or door that occurs on each side of a room.

class Wall : public MapSite {

public:

Wall();

virtual void Enter();

};

class Door : public MapSite {

public:

Door(Room* = 0, Room* = 0);

virtual void Enter();

Room* OtherSideFrom(Room*);

private:

Room* _rooml;

Room* _room2;

bool _isOpen;

};

Page 15: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

We need to know about more than just the parts of a maze.

We'll also define a Maze class to represent a collection of rooms.

Maze can also find a particular room given a room number using its

RoomNo operation.

class Maze {

public:

Maze();

void AddRoom(Room* ) ;

Room* RoomNo(int) const;

private:

// . . .

};

Page 16: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

Another class is MazeGame, which creates the maze. One

straightforward way to create a maze is with a series of

operations that add components to a maze and then

interconnect them.

For example, the following member function will create a

maze consisting of two rooms with a door between them:

Page 17: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

Maze* MazeGame::CreateMaze () {

Maze* aMaze = new Maze;

Room* rl = new Room(l);

Room* r2 = new Ro om (2);

Door* theDoor = new Door(rl, r2);

aMaze->AddRoom(rl);

aMaze->AddRoom(r2);

rl->SetSide(North, new Wall);

rl->SetSide(East, theDoor);

rl->SetSide(South, new Wall);

rl->SetSide(West, new Wall);

r2->SetSide(North, new Wall);

r2->SetSide(East, new Wall);

r2->SetSide(South, new Wall);

r2->SetSide(West, theDoor);

return aMaze;

Page 18: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Creational patterns

The real problem with this member function isn't

its size but its inflexibility.

It hard-codes the maze layout.

Changing the layout means changing this member

function, either by overriding it—which means

reimplementing the whole thing—or by changing

parts of it—which is error-pronaend doesn't

promote reuse.

The creational patterns show how to make this

design more flexible, not necessarily smaller.

Page 19: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory (Object Creational)

IntentProvide an interface for creating families of related or

dependent objects without specifying their concrete classes

Pattern summaryCapture family creation in a class containing a factory

method for each class in the family

Also Known as

Kit

Page 20: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory - Motivation

1) Implement a user interface toolkit that supports multiple looks

and feel standards such as Motif, Windows 95 or the finder in

MacOS.

w How can you write a single user interface and make it portable

across the different look and feel standards for these window

managers?

2) Implement a facility management system for an intelligent

house that supports different control systems such as Siemens’

Instabus, Johnson & Control Metasys or Zumtobe’s proprietary

standard.

w How can you write a single control system that is independent

from the manufacturer?

Page 21: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory - Motivation

Different look-and-feels define different appearances and behaviors for user

interface "widgets" like scroll bars, windows, and buttons.

To be portable across look-and-feel standards, an application should not

hard-code its widgets for a particular look and feel.

Instantiating look-and feel specific classes of widgets throughout the

application makes it hard to change the look and feel later.

Page 22: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory - structure

Page 23: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory - structure

Page 24: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory The Client remains blissfully unaware of the various concrete classes in this

example. Client code deals with the simpler, abstract, general case.

Page 25: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory

Page 26: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory applicability

Use the Abstract Factory pattern when

• a system should be independent of how its products are created,

composed, and represented.

• a system should be configured with one of multiple families of

products.

• a family of related product objects is designed to be used together, and

you need to enforce this constraint.

• you want to provide a class library of products, and you want to reveal

just their interfaces not, their implementations.

Page 27: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Participants

AbstractFactory (WidgetFactory)

- declares an interface for operations that create abstract product objects.

• ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)

- implements the operations to create concrete product objects.

• AbstractProduct (Window, ScrollBar)

- declares an interface for a type o f product object.

• ConcreteProduct (MotifWindow, MotifScrollBar)

- defines a product object to be created by the corresponding concrete factory.

- implements the AbstractProduct interface.

• Client

- uses only interfaces declared by AbstractFactory and AbstractProductclasses.

Page 28: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Collaborations

Normally a single instance of a Concrete Factory class is

created at run-time.

To create different product objects, clients should use a

different concrete factory.

Abstract Factory defers creation of product objects to its

Concrete Factory subclass.

Page 29: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Consequences

●The Abstract Factory Pattern has the following benefits:

oIt isolates concrete classes from the client.

You use the Abstract Factory to control the classes

of objects the client creates.

Product names are isolated in the implementation of

the ConcreteFactory, clients use the instances

through their abstract interfaces.

Page 30: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Consequences

oExchanging product families is easy.

None of the client code breaks because the abstract

interfaces don’t change.

Because the abstract factory creates a complete

family of products, the whole product family

changes when the concrete factory is changed.

oIt promotes consistency among products.

It is the concrete factory’s job to make sure that the

right products are used together.

Page 31: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Consequences

● The Abstract Factory pattern has the following liability:

oAdding new kinds of products to existing factory is

difficult.

Adding a new product requires extending the

abstract interface which implies that all of its

derived concrete classes also must change.

Page 32: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Consequences

Essentially everything must change to support and

use the new product family

abstract factory interface is extended

derived concrete factories must implement the

extensions

a new abstract product class is added

a new product implementation is added

client has to be extended to use the new product

Page 33: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Implementation

Factories as singletons.

Creating the products.

Defining extensible factories.

Page 34: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Implementation

● Concrete factories are often implemented as singletons.

● Creating the products

oConcrete factory usually use the factory method.

simple

new concrete factory is required for each product family

oalternately concrete factory can be implemented using

prototype.

only one is needed for all families of products

product classes now have special requirements - they

participate in the creation

Page 35: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

Abstract Factory Implementation

● Defining extensible factories by using create function

with an argument

oonly one virtual create function is needed for the

AbstractFactory interface

oall products created by a factory must have the same

base class or be able to be safely coerced to a given

type

o it is difficult to implement subclass specific operations

Page 36: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

UNIT-III

Know Uses

●Interviews

oused to generate “look and feel” for specific user

interface objects

ouses the Kit suffix to denote AbstractFactory classes,

e.g., WidgetKit and DialogKit.

oalso includes a layoutKit that generates different

composite objects depending on the needs of the

current context

ET++

oanother windowing library that uses the AbstractFactory

to achieve portability across different window systems

(X Windows and SunView).

L2

Page 37: Creational Design Patterns Structural Design Patterns ...emadilms.ir/emadi/wp-content/uploads/2015/02/... · Creational patterns Because the creational patterns are closely related,

UNIT-III

Related Patterns

●Factory Method -- a “virtual” constructor

●Prototype -- asks products to clone

themselves

●Singleton -- allows creation of only a

single instance

L2