Docsity

GoF Design Patterns-Patterns in Software Engineering-Lecture 2 Slides-Computer Engineering, Slides of Software Engineering

Toggle sidebar
bg1
Patterns in
Software Engineering
Software
Engineering
Lecturer: Raman Ramsin
Lecturer:
Raman
Ramsin
Lt 2
L
ec
t
ure
2
GoF Desi
g
n Patterns
–
Creational
Department of Computer Engineering 1Sharif University of Technology
g
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download GoF Design Patterns-Patterns in Software Engineering-Lecture 2 Slides-Computer Engineering and more Slides Software Engineering in PDF only on Docsity!

Patterns inSoftware EngineeringSoftware Engineering Lecturer: Raman RamsinLecturer:

Raman Ramsin

L

t

Lecture 2 GoF Design Patterns – Creational

1

g

Patterns in Software Engineering – Lecture 2

GoF Design Patterns – Principles Ā„

Emphasis on flexibility and reuse through decoupling ofclasses.

Ā„

T

he underlying principles:

y

g p

p

program to an interface, not to an implementation.

favor composition over class inheritance.

p

find what varies and encapsulate it.

2

Patterns in Software Engineering – Lecture 2

GoF Design Patterns: Purpose and Scope

4

Patterns in Software Engineering – Lecture 2

GoF Creational Patterns Ā„

Class

F

t

M th

d

D fi

i t

f

f

ti

bj

t

b t l t

F

actory Method:

D

efine an interface for creating an object, but let

subclasses decide which class to instantiate. Factory method lets aclass defer instantiation to subclasses.

Ā„

Object

Abstract Factory:

Provide an interface for creating families of

related or dependent objects without specifying their concrete class.

p

j^

p

y

g

Builder:

Separate the construction of a complex object from its

representation so that the same construction process can createdifferent representations.

Prototype:

Specify the kinds of objects to create using a prototypical

instance, and create new objects by copying this prototype.

Singleton:

Ensure a class only has one instance, and provide a global

point of access to it

5

Sharif University of Technology

point

of access to it.

Patterns in Software Engineering – Lecture 2

Factory Method: Applicability

U

th

F

t

M th d

tt

h

Ā„

U

se the Factory Method pattern when

l^

't

ti i

t

th

l^

f

bj

t

it

t

t

a class can't anticipate the class of objects it must create.

a class wants its subclasses to specify the objects it creates

a class wants its subclasses to specify the objects it creates.

classes delegate responsibility to one of several helper

classes delegate responsibility to one of several helpersubclasses, and you want to localize the knowledge of whichhelper subclass is the delegate.

7

Sharif University of Technology

Patterns in Software Engineering – Lecture 2

Factory Method: Structure

8

Patterns in Software Engineering – Lecture 2

Abstract Factory Ā„

Intent:

P

id

i t

f

f

ti

f

ili

f

l t d

P

rovide an interface for creating families of related or dependent objects without specifying their concrete classes.

10

Sharif University of Technology

Patterns in Software Engineering – Lecture 2

Abstract Factory: Applicability

U

th

Ab t

t F

t

tt

h

Ā„

U

se the Abstract Factory pattern when

a system should be independent of how its products arecreated, composed, and represented.created, composed, and represented.

a system should be configured with one of multiple families ofproducts.

a family of related product objects is designed to be usedtogether, and you need to enforce this constraint.

you want to provide a class library of products

and you want

you want to provide a class library of products, and you wantto reveal just their interfaces, not their implementations.

11

Sharif University of Technology

Patterns in Software Engineering – Lecture 2

Abstract Factory: Consequences 9

Concrete classes are isolated

Clients manipulate

Concrete

classes are isolated. Clients manipulate

instances through their abstract interfaces.

Exchanging product families is easy. Different product

g

g p

y

p

configurations can be used simply by changing theconcrete factory.

Consistency among products is promoted

Consistency among products is promoted.S

k

d

f

d

d ff

l^

h

S

upporting new kinds of products is difficult. The

AbstractFactory interface fixes the set of products thatcan be created.

13

Sharif University of Technology

Patterns in Software Engineering – Lecture 2

Builder Ā„

Intent:

Separate the construction of a complex object from its representation

Separate the construction of a complex object from its representationso that the same construction process can create differentrepresentations.

14

Patterns in Software Engineering – Lecture 2

Builder: Structure

16

Patterns in Software Engineering – Lecture 2

Builder: Collaborations

17

Patterns in Software Engineering – Lecture 2

Prototype Ā„

Intent:

Specify the kinds of objects to create using a prototypical

Specify the kinds of objects to create using a prototypicalinstance, and create new objects by copying this prototype.

19

Patterns in Software Engineering – Lecture 2

Prototype: Applicability Ā„

Use the Prototype pattern when

the classes to instantiate are specified at run-time, forexample, by dynamic loading.

building a class hierarchy of factories that parallels the classhierarchy of products should be avoidedhierarchy of products should be avoided.

instances of a class can have one of only a few different

instances of a class can have one of only a few differentcombinations of state.

Ā„

It may be more convenient to install a corresponding number ofprototypes and clone them rather than instantiating the class

ll

20

manually.