• No results found

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

N/A
N/A
Protected

Academic year: 2021

Share "Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering"

Copied!
26
0
0

Loading.... (view fulltext now)

Full text

(1)

Software Engineering Software Engineering

Lecturer: Raman Ramsin Lecturer: Raman Ramsin

L t 2

Lecture 2

(2)

GoF Design Patterns – Principles

„

Emphasis on flexibility and reuse through decoupling of classes.

„

The 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.

Department of Computer Engineering

2 Sharif University of Technology

(3)

GoF Design Patterns: General Categories

23 patterns are divided into three separate categories:

„

23 patterns are divided into three separate categories:

… Creational patterns

l h l d f l d b

„ Deal with initializing and configuring classes and objects.

… Structural patterns

Deal with decoupling interface and implementation of classes

„ Deal with decoupling interface and implementation of classes and objects.

… Behavioral patterns

„ Deal with dynamic interactions among societies of classes and objects.

(4)

GoF Design Patterns: Purpose and Scope

Department of Computer Engineering

4 Sharif University of Technology

(5)

GoF Creational Patterns

„ Class

F t M th d D fi i t f f ti bj t b t l t

… Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class 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 create different representations.

… Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

(6)

Factory Method

„ Intent:

… Define an interface for creating an object, but let subclasses decide

… Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer

instantiation to subclasses.

Department of Computer Engineering

6 Sharif University of Technology

(7)

Factory Method: Applicability

U th F t M th d tt h

„

Use 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 helper

subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

(8)

Factory Method: Structure

Department of Computer Engineering

8 Sharif University of Technology

(9)

Factory Method: Consequences

9 It provides hooks for the subclasses 9 It provides hooks for the subclasses.

9 It connects parallel class hierarchies.

(10)

Abstract Factory

„

Intent:

P id i t f f ti f ili f l t d

… Provide an interface for creating families of related or

dependent objects without specifying their concrete classes.

Department of Computer Engineering

10 Sharif University of Technology

(11)

Abstract Factory: Applicability

U th Ab t t F t tt h

„

Use the Abstract Factory pattern when

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

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

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

(12)

Abstract Factory: Structure

Department of Computer Engineering

12 Sharif University of Technology

(13)

Abstract Factory: Consequences

9 Concrete classes are isolated Clients manipulate 9 Concrete classes are isolated. Clients manipulate

instances through their abstract interfaces.

9 Exchanging product families is easy. g g p y Different product p configurations can be used simply by changing the

concrete factory.

9 Consistency among products is promoted 9 Consistency among products is promoted.

S k d f d d ff l h

8 Supporting new kinds of products is difficult. The

AbstractFactory interface fixes the set of products that

(14)

Builder

„ Intent:

… Separate the construction of a complex object from its representation

… Separate the construction of a complex object from its representation so that the same construction process can create different

representations.

Department of Computer Engineering

14 Sharif University of Technology

(15)

Builder: Applicability

„

Use the Builder pattern when

…

the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.

and how they re assembled.

…

the construction process must allow different p

representations for the object that's constructed.

(16)

Builder: Structure

Department of Computer Engineering

16 Sharif University of Technology

(17)

Builder: Collaborations

(18)

Builder: Consequences

9 It lets you vary a product's internal representation.

9 It isolates code for construction and representation.

9 It gives you finer control over the construction process:

Since the Builder pattern constructs the product step by Since the Builder pattern constructs the product step by step under the director's control.

Department of Computer Engineering

18 Sharif University of Technology

(19)

Prototype

„

Intent:

… Specify the kinds of objects to create using a prototypical

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

(20)

Prototype: Applicability

„

Use the Prototype pattern when

… the classes to instantiate are specified at run-time, for example, by dynamic loading.

… building a class hierarchy of factories that parallels the class hierarchy of products should be avoided

hierarchy 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 different combinations of state.

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

ll

Department of Computer Engineering

20 Sharif University of Technology

manually.

(21)

Prototype: Structure

(22)

Prototype: Consequences

9 It hides the concrete product classes from the clients, thereby reducing the number of names clients know about.

9 It lets a client work with application-specific classes without modification.

9 It lets you add and remove products at run-time.

9 It lets you specify new objects by varying values.

Department of Computer Engineering

22 Sharif University of Technology

(23)

Singleton

„

Intent:

E l l h i t d id l b l

… Ensure a class only has one instance, and provide a global point of access to it.

(24)

Singleton: Applicability

„

Use the Singleton pattern when

…

there must be exactly one instance of a class, and it must be accessible to clients from a well known

access point.

access point.

…

when the sole instance should be extensible by subclassing and clients should be able to use an subclassing, and clients should be able to use an extended instance without modifying their code.

Department of Computer Engineering

24 Sharif University of Technology

(25)

Singleton: Consequences

9 It provides Controlled access to sole instance.

9 It reduces the name space by avoiding global variables. It reduces the name space by avoiding global variables.

9 It permits refinement of operations and representation through subclassing

through subclassing.

9 It permits a variable number of instances.

9 It is more flexible than class operations.

(26)

Reference Reference

G E H l R J h R d Vli id J D i

„

Gamma, E., Helm, R., Johnson, R., and Vlissides, J., Design Patterns: Elements of Reusable Object-oriented Software.

Addison-Wesley, 1995.

Department of Computer Engineering

26 Sharif University of Technology

References

Related documents

ًلاومعم رد تاعلاطم مس یسانش تارذ فلتخم زا نیا بیکرت ب ه لیلد تیللاح و تیمس نییاپ نآ ب ه ناونع لرتنک یفنم هدافتسا یم دوش ( 7 ، 9 .) ره دنچ Warheit و

This chapter describes the cables connected to an AAU, including the AU PGND cable, RU power cable, RF jumper, CPRI fiber optic cable, AISG multi-wire cable, and RU alarm

In an experiment involving foreign- and standard- accented speech, Yi, Phelps, Smiljanic, & Chandrasekaran (2013) collected native English speaker listeners’

We want to ensure that as a global team we focus our vision not only on internal business success, but also on making a positive impact on the external world and people around

In this chapter, the theoretical background to lean manufacturing and the debate surrounding the quality of working life for employees within this context was outlined. The

Wisdom and Knowledge Humility Forgiveness Humour Gratitude Hope Courage Temperance Values Self-Efficacy Optimal Experience Peak Experiences Flow Positive Ageing Psychology of

The survey data shows certain trends in the CubeSat environment, for example the pre- ferred CubeSat parameters for the following: satellite size, communication transmission

2.1 Definition of organic farming and organic products 13 2.1.1 International definition of organic farming and organic products 13 2.1.2 Definition of organic farming