design patterns english
TRANSCRIPT
1
Design Patterns
By: Sari Meriem
UNIVERSITY of MOHAMED CHERIF MESAADIASOUK-AHRAS
Departement of Math & Computer Science
Presentation About:
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.
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
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
History
5
Erich Gamma Ralph Johnson John Vlissides Richard Helm
Meriem Sari GL à Objet 2 2 eme AM GL
Outline
6Meriem Sari GL à Objet 2 2 eme AM GL
Definition.
Classification.
State Design Pattern.
Strategy Design Pattern.
Visitor Design Pattern.
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
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
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
Classification
10Meriem Sari GL à Objet 2 2 eme AM GL
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
State Pattern
12
Definition.
Purpose.
Structure.
Consequences.
Example.
Meriem Sari GL à Objet 2 2 eme AM GL
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
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
Structure (1)
15Meriem Sari GL à Objet 2 2 eme AM GL
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
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
Example (1)
18Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
19Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
20Meriem Sari GL à Objet 2 2 eme AM GL
Strategy Pattern
21Meriem Sari GL à Objet 2 2 eme AM GL
Definition.
Purpose.
Structure.
Consequences.
Example.
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.
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.
Structure (1)
24Meriem Sari GL à Objet 2 2 eme AM GL
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.
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.
Example (1)
27Meriem Sari GL à Objet 2 2 eme AM GL
Calculator
Example (Implementation)
28Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
29Meriem Sari GL à Objet 2 2 eme AM GL
Visitor Pattern
30Meriem Sari GL à Objet 2 2 eme AM GL
Definition.
Purpose.
Structure.
Consequences.
Example.
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.
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.
Structure (1)
33Meriem Sari GL à Objet 2 2 eme AM GL
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.
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.
Example (1)
36Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
37Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
38Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
39Meriem Sari GL à Objet 2 2 eme AM GL
Example (Implementation)
40Meriem Sari GL à Objet 2 2 eme AM GL
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
Acknowledgment
42
Thank You For Paying Attention
Meriem Sari GL à Objet 2 2 eme AM GL