1 Chapter 5:Design Patterns. 2 What are design pattern? Schematic description of design solution to...

34
1 Chapter 5:Design Patterns

Transcript of 1 Chapter 5:Design Patterns. 2 What are design pattern? Schematic description of design solution to...

Page 1: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

1

Chapter 5:Design Patterns

Page 2: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

2

What are design pattern? Schematic description of design solution to

recurring problems in software design and,

Reusable (i.e. generic) but don’t have to be implemented in the same way

Describe problems that occur repeatedly and core solutions to those problems

Page 3: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

3

Why design pattern? To capture & document software design knowledge

To support reuse in design and boost confidence in software systems

To provide common vocabulary for software designers to communicate their designers

Page 4: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

4

Useful of design pattern Reuse existing, high quality solutions to recurring

design patterns

Improve future design: solution to new problems can be obtained with higher quality

Improve documentation: smaller & simpler

Use a right design, not just one that works

Page 5: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

5

Pattern category Divided into three:-

Creational: creation of object

Structural: concern about the composition of objects into larger structures

Behavioral: define how object interact and how responsibility is distributed among them (i.e. use inheritance, aggregation & composition).

Page 6: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

6

Pattern category

Creational Structural Behavioral

Abstract factory Adapter Chain of responsibility

Builder Bridge Command

Factory method Composite Interpreter

Prototype Decorator Iterator

Singleton Facade Mediator

Flyweight Memento

Proxy Observer

State

Strategy

Template method

Visitor

Page 7: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

7

Pattern description Divided into seven:-

Content: General situation in which the pattern applies

Problem: A short sentence or two string the main difficulty

Forces: The issues or concerns to consider when solving the problem

Solution: The recommended way to solve the problem in the given context

Antipatterns: Solutions that are inferior or do not work in this context

Related patterns: Patterns that are similar to this pattern

References: Who developed or inspired the pattern

Page 8: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

8

The abstraction – occurrence pattern Context

Often in a domain model you find a set of related objects (occurrence)

Problem What is the way to represent such set of occurrences

in a class diagram?

Forces You want to represent the members of each set of

occurrences without duplicating the common information

Page 9: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

9

Abstraction – Occurrence

Solution:

Page 10: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

10

Abstraction – Occurrence Antipatterns:

Page 11: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

11

The general hierarchy pattern Context

Often in a hierarchy can have one or more objects above them (superiors)

Problem How to you represent a hierarchy of objects, in which

some objects cannot have subordinates?

Forces You want a flexible way of representing the hierarchy

All the objects have many common properties & operations

Page 12: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

12

General hierarchy

Solution:

Page 13: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

13

General hierarchy Solution:

Page 14: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

14

General hierarchy Antipattern:

Page 15: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

15

The Player – Role Pattern Context

A role is a particular set of properties associated with an object in a particular context

An object may play different roles in different contexts

Problem How do you best model players and roles so that a

player can change roles or process multiple roles?

Forces It is desirable to improve encapsulation by capturing

the information associated with each separate role in a class

You want to avoid multiple inheritance

You cannot allow an instance to change data

Page 16: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

16

The Player – Role Pattern

Solution:

Page 17: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

17

The Player – Role Pattern Example 1:

Page 18: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

18

The Player – Role Pattern Example 2:

Page 19: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

19

The singleton pattern Context

It is very common to find classes for which only one instance should exist (singleton)

Problem How do you ensure that it is never possible to create

more than one instance of singleton class?

Forces The use of a public constructor cannot guarantee that

no more than one instance will be created

The singleton instance must also be accessible to all classes that require it

Page 20: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

20

The singleton pattern Solution:

Page 21: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

21

The observer pattern Context

When an association is created between two classes, the code for the classes becomes inseparable

If you want to reuse one class, then you also have to reuse the other

Problem How do you reduce the interconnection between

classes, especially between classes that belong to different modules or subsystems?

Forces You want to minimize the flexibility of the system to the

greatest extent possible

Page 22: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

22

The observer pattern Solution:

Page 23: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

23

Observer Antipatterns

Connect an observer directly to an observable so that they both have references to each other

Make the observers subclasses of the observable

Page 24: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

24

The delegation pattern Context

When an association is created between two classes, the code for the classes becomes inseparable

If you want to reuse one class, then you also have to reuse the other

Problem How do you reduce the interconnection between

classes, especially between classes that belong to different modules or subsystems?

Forces You want to minimize the flexibility of the system to the

greatest extent possible

Page 25: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

25

The delegation pattern Solution:

Page 26: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

26

The delegation patternExample:

Page 27: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

27

The adapter pattern Context

You are building an inheritance hierarchy and want to incorporate it into an existing class

The reused class is also often already part of its own inheritance hierarchy

Problem How to obtain the power of polymorphism when

reusing a class whose method

Have the same function

But not the same signature

Forces You do not have access to multiple inheritance or you

do not want to use it

Page 28: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

28

The adapter pattern Solution:

Page 29: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

29

The adapter pattern Example:

Page 30: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

30

The façade pattern Context

Often, an application contains several complex packages

A programmer working with such packages has to manipulate many different classes

Problem How do you simplify the view that programmers have

of a complex package

Forces It is hard for a programmer to understand and use an

entire subsystem

If several different application classes call method of the complex package, then any modifications made to the package will necessitate a complex review of all these classes

Page 31: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

31

The façade pattern Solution:

Page 32: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

32

The factory pattern Context

A reusable framework needs to create objects, however the class the created objects depends on the application

Problem How do you enable a programmer to add new

application specific class into a system built on such as framework?

Forces We want to have the framework create and work with

application-specific classes that the framework does not yet know about

Page 33: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

33

The factory pattern Solution

Page 34: 1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.

34

The factory pattern Example