design patterns english

42
Design Patterns By: Sari Meriem 1 UNIVERSITY of MOHAMED CHERIF MESAADIA SOUK-AHRAS Departement of Math & Computer Science Presentation About:

Upload: meriemsari

Post on 16-Aug-2015

129 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: Design patterns english

1

Design Patterns

By: Sari Meriem

UNIVERSITY of MOHAMED CHERIF MESAADIASOUK-AHRAS

Departement of Math & Computer Science

Presentation About:

Page 2: Design patterns english

Motivation

2Meriem Sari GL à Objet 2 2 eme AM GL

Designing object-oriented software is hard. Designing a reusable object-oriented software is even harder.

Hard to find objects and collect them at the right granularity. Hard to define class interfaces and inheritance hierarchies. Hard to establish key relationships among classes.

The design should be specific to one problem but general enough to address future problems.

Page 3: Design patterns english

Motivation

3

Expert designers reuse existing solutions to solve new problems.

Recurring patterns of classes and communicating objects in many object-oriented systems.

Applying these patterns will make the software design flexible, elegant, and ultimately reusable.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 4: Design patterns english

History

4

In this presentation we will consider the patterns published in the book: «Design Patterns - Elements of Reusable Object-Oriented Software» by the GOF.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 5: Design patterns english

History

5

Erich Gamma Ralph Johnson John Vlissides Richard Helm

Meriem Sari GL à Objet 2 2 eme AM GL

Page 6: Design patterns english

Outline

6Meriem Sari GL à Objet 2 2 eme AM GL

Definition.

Classification.

State Design Pattern.

Strategy Design Pattern.

Visitor Design Pattern.

Page 7: Design patterns english

Definition

7

According to Christopher Alexander:

A pattern describes a problem which occurs over and over.

It describes the core of the solution to that problem.

This solution can be used a million times over, without ever doing it the same way twice.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 8: Design patterns english

Definition

8

Each pattern has four basic elements:

The pattern name: each pattern name describes the problem that this pattern solves to ease the documentation and the communication between designers and developers.

The problem: describes when to apply this pattern.

The solution: describes the general arrangement of elements (classes and objects ) that solves problem.

The consequences: describe the results of applying the pattern in order to evaluate design alternatives and understanding the costs and benefits of applying the pattern.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 9: Design patterns english

Classification

9

Design patterns vary in their granularity and level of abstraction.

They are classified by two criteria:

By purpose i.e. what a pattern does.

Creational patterns: concern the process of object creation. Structural patterns: deal with the composition of classes or objects. Behavioral patterns: characterize the ways in which classes or objects interact .

By scope i.e. whether the pattern applies primarily to classes or to objects.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 10: Design patterns english

Classification

10Meriem Sari GL à Objet 2 2 eme AM GL

Page 11: Design patterns english

Design Patterns

11

In this presentation we’re gonna abord three behavioral patterns which are:

State design pattern.

Strategy design pattern.

Visitor design pattern.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 12: Design patterns english

State Pattern

12

Definition.

Purpose.

Structure.

Consequences.

Example.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 13: Design patterns english

Definition

13

Known as: Objects for States.

An object's behavior depends on its state.

Allows the object to alter its behavior when its internal state changes.

This change happens in run-time.

Encapsulates varying behavior of the same object based on its states.

Creates objects for various states and a context object whose behavior depends on those objects’ state change.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 14: Design patterns english

Purpose

14

Minimizes the use of conditional statements that leads to changing the state of an object.

Puts each branch of the conditional statement in a separate class. It allows us to treat the object's state as an object in its own that varies independently from other objects.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 15: Design patterns english

Structure (1)

15Meriem Sari GL à Objet 2 2 eme AM GL

Page 16: Design patterns english

Structure (2)

16

Context: defines the interface of interest to clients and maintain the instance of a Concrete State subclass that defines the current state.

State: defines an interface for encapsulating the behavior associated with a particular state of the Context.

Concrete State Subclass: each subclass implements a behavior associated with a state of the Context.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 17: Design patterns english

Consequences

17

The State pattern puts all behavior associated with a particular state into one object.

When an object defines its current state internally, its state transitions have no explicit representation i.e. they only show up as assignments to some variables.

State objects can be shared between Contexts (Flyweight).

Meriem Sari GL à Objet 2 2 eme AM GL

Page 18: Design patterns english

Example (1)

18Meriem Sari GL à Objet 2 2 eme AM GL

Page 19: Design patterns english

Example (Implementation)

19Meriem Sari GL à Objet 2 2 eme AM GL

Page 20: Design patterns english

Example (Implementation)

20Meriem Sari GL à Objet 2 2 eme AM GL

Page 21: Design patterns english

Strategy Pattern

21Meriem Sari GL à Objet 2 2 eme AM GL

Definition.

Purpose.

Structure.

Consequences.

Example.

Page 22: Design patterns english

Definition

22Meriem Sari GL à Objet 2 2 eme AM GL

Known as: Policy pattern.

Enables an algorithm's behavior to be selected at runtime.

Changes the executing algorithm of the context object.

Lets the algorithm vary independently from clients that use it.

Defines a family of algorithms and encapsulates each one.

Makes algorithms interchangeable between families.

Page 23: Design patterns english

Purpose

23Meriem Sari GL à Objet 2 2 eme AM GL

Configure many related classes which differ only in their behavior.

Very helpful in validation on in coming data based algorithms. Type of data. The source of the data. The user choice…

Used by other objects in different areas of the system or even different systems without duplicating the code.

Page 24: Design patterns english

Structure (1)

24Meriem Sari GL à Objet 2 2 eme AM GL

Page 25: Design patterns english

Structure (2)

25Meriem Sari GL à Objet 2 2 eme AM GL

Context : maintains a reference to a Strategy object and configured with a ConcreteStrategy object.

Strategy: declares an interface common to all supported algorithms.

ConcreteStrategy: implements the algorithm using the Strategy interface.

Page 26: Design patterns english

Consequences

26Meriem Sari GL à Objet 2 2 eme AM GL

Encapsulating all the behaviors in one class makes it hard to avoid conditional statements.

Eliminates the use of conditional statements.

Provides different implementations of the same behavior.

Increases the number of objects in an application.

Page 27: Design patterns english

Example (1)

27Meriem Sari GL à Objet 2 2 eme AM GL

Calculator

Page 28: Design patterns english

Example (Implementation)

28Meriem Sari GL à Objet 2 2 eme AM GL

Page 29: Design patterns english

Example (Implementation)

29Meriem Sari GL à Objet 2 2 eme AM GL

Page 30: Design patterns english

Visitor Pattern

30Meriem Sari GL à Objet 2 2 eme AM GL

Definition.

Purpose.

Structure.

Consequences.

Example.

Page 31: Design patterns english

Definition

31Meriem Sari GL à Objet 2 2 eme AM GL

Represents an operation to be performed on the elements of an object structure.

Allows the addition of new operations without changing the structure of the object.

Separates the algorithm from the object structure on which it operates.

Allows the addition of new virtual functions in a classes without modifying it.

It is one way to follow the open/closed principle.

Page 32: Design patterns english

Purpose

32Meriem Sari GL à Objet 2 2 eme AM GL

Consider a compiler that represents programs as abstract syntax trees.

We’ll need to perform operations on abstract syntax trees (configure the nodes).

Compiling + Configuring = a hard to understand system(complex).

Visitor design pattern allows us to configure the abstract syntax tree without polluting the source code of the original programe which is the compiler.

Page 33: Design patterns english

Structure (1)

33Meriem Sari GL à Objet 2 2 eme AM GL

Page 34: Design patterns english

Structure (2)

34Meriem Sari GL à Objet 2 2 eme AM GL

Visitor: declares a Visit operation for each class of ConcreteElement . The operation's name and signature identifies the class that sends the Visit request to the visitor.

ConcreteVisitor: implements each operation declared by Visitor.

Element: defines an Accept operation that takes a visitor as an argument.

ConcreteElement: implements an Accept operation that takes a visitor as an argument.

Page 35: Design patterns english

Structure (3)

35Meriem Sari GL à Objet 2 2 eme AM GL

Consider two objects: one is called the "element", the other is called the "visitor".

An element has an accept() method that can take the visitor as an argument.

The accept() method calls a visit() method of the visitor.

The element passes itself as an argument to the visit() method.

Page 36: Design patterns english

Example (1)

36Meriem Sari GL à Objet 2 2 eme AM GL

Page 37: Design patterns english

Example (Implementation)

37Meriem Sari GL à Objet 2 2 eme AM GL

Page 38: Design patterns english

Example (Implementation)

38Meriem Sari GL à Objet 2 2 eme AM GL

Page 39: Design patterns english

Example (Implementation)

39Meriem Sari GL à Objet 2 2 eme AM GL

Page 40: Design patterns english

Example (Implementation)

40Meriem Sari GL à Objet 2 2 eme AM GL

Page 41: Design patterns english

Conclusion

41

If we don't study design patterns in software, we won't be able to improve them, and it'll be harder to come up with new ones.

Meriem Sari GL à Objet 2 2 eme AM GL

Page 42: Design patterns english

Acknowledgment

42

Thank You For Paying Attention

Meriem Sari GL à Objet 2 2 eme AM GL