Sei sulla pagina 1di 28

1

Behavioral Patterns
Momento
Mediator

Behavioral pattern
2

A behavioral pattern explains how objects interact.


It describes how different objects and classes
send messages to each other to make things
happen and how the steps of a task are divided
among different objects.
Where Creational patterns mostly describe a
moment of time (the instant of creation), and
Structural patterns describe a more or less static
structure,
Behavioral patterns describe a process or a flow.

Behavioral Patterns
3

Chain of Responsibility:
Request delegated to the responsible service provider
Command:
Request or Action is first-class object, hence re-storable
Iterator:
Aggregate and access elements sequentially
Interpreter:
Language interpreter for a small grammar
Mediator:
Coordinates interactions between its associates
Memento:
Snapshot captures and restores object states privately

Behavioral Patterns (cont.)


4

Observer:
Dependents update automatically when subject
changes
State:
Object whose behavior depends on its state
Strategy:
Abstraction for selecting one of many algorithms
Template Method:
Algorithm with some steps supplied by a derived class
Visitor:
Operations applied to elements of a heterogeneous
object structure

Memento Pattern

Memento pattern
6

Memento pattern is used to restore state


of an object to a previous state.
The memento pattern is a design pattern
that permits the current state of an
object to be stored without breaking the
rules of encapsulation.
The originating object can be modified
as required but can be restored to the
saved state at any time.

Intent
7

Without violating encapsulation, capture


and externalize an object's internal state
so that the object can be returned to this
state later.
A magic cookie that encapsulates a
"check point" capability.
Promote undo or rollback to full object
status.

Problem
8

Need to restore an object back to its


previous state (e.g. "undo" or "rollback"
operations).

Discussion
9

The client requests a Memento from the source


object when it needs to checkpoint the source
object's state.
The source object initializes the Memento with a
characterization of its state.
The client is the "care-taker" of the Memento, but
only the source object can store and retrieve
information from the Memento (the Memento is
"opaque" to the client and all other objects).
If the client subsequently needs to "rollback" the
source object's state, it hands the Memento back to
the source object for reinstatement.

10

The Memento design pattern defines three


distinct roles:
Originator - the object that knows how to
save itself.
Caretaker - the object that knows why and
when the Originator needs to save and
restore itself.
Memento - the lock box that is written and
read by the Originator, and shepherded by
the Caretaker.

Structure
11

Implementation
12

Memento pattern uses three actor classes.


Memento contains state of an object to be
restored.
Originator creates and stores states in Memento
objects and Caretaker object is responsible to
restore object state from Memento.
We have created classes Memento, Originator
and CareTaker.
MementoPatternDemo, our demo class, will use
CareTaker and Originator objects to show
restoration of object states.

Consequences
13

Memento protects encapsulation and avoids


exposing originator's internal state and
implementation.
It also simplifies originator code such that the
originator does not need to keep track of its
previous state since this is the responsibility of the
CareTaker.
Using the memento pattern can be expensive
depending on the amount of state information that
has to be stored inside the memento object.
In addition the caretaker must contain additional
logic to be able to manage mementos.

Example
14

Sample Co
de

15

Memento (Software
counterpart)

16

Mediator Pattern

Mediator pattern
17

Mediator pattern is used to reduce


communication complexity between
multiple objects or classes.
This pattern provides a mediator class
which normally handles all the
communications between different
classes and supports easy maintenance
of the code by loose coupling.

Intent
18

Define an object that encapsulates how a set of


objects interact.
Mediator promotes loose coupling by keeping
objects from referring to each other explicitly,
and it lets you vary their interaction
independently.
Design an intermediary to decouple many
peers.
Promote the many-to-many relationships
between interacting peers to "full object status".

Mediator Pattern Structure


19

Participants
20

Mediator - defines an interface for


communicating with Colleague objects.
ConcreteMediator

knows the colleague classes and keep a reference to


the colleague objects.
implements the communication and transfer the
messages between the colleague classes

Colleague classes

keep a reference to its Mediator object


communicates with the Mediator whenever it would
have otherwise communicated with another Colleague.

Applicability
21

According to Gamma et al, the Mediator pattern


should be used when:
a set of objects communicate in well-defined but
complex ways. The resulting interdependencies
are unstructured and difficult to understand.
reusing an object is difficult because it refers to
and communicates with many other objects.
a behavior that's distributed between several
classes should be customizable without a lot of
subclassing.

22

Consequences Advantages
Comprehension - The mediator encapsulate the logic of

mediation between the colleagues. From this reason it' more


easier to understand this logic since it is kept in only one class.

Decoupled Colleagues - The colleague classes are totally


decoupled. Adding a new colleague class is very easy due to
this decoupling level.

Simplified object protocols - The colleague objects need to


communicate only with the mediator objects. Practically the
mediator pattern reduce the required communication
channels(protocols) from many to many to one to many and
many to one.

Limits Subclassing - Because the entire communication logic


is encapsulated by the mediator class, when this logic need to
be extended only the mediator class need to be extended.

23

Consequences Disadvantages

Complexity

in practice the mediators tends to become


more complex and complex.
A good practice is to take care to make the
mediator classes responsible only for the
communication part.
For example when implementing different
screens the the screen class should not contain
code which is not a part of the screen
operations.
It should be put in some other classes.

24

Mediator (Non software


example)
Loose coupling
between colleague
objects is achieved
by
having colleagues
communicate with
the
Mediator, rather than
one another.

Example 1
25

A Dialog window is a collection of graphic and nongraphic controls.


The Dialog class provides the mechanism to facilitate
the interaction between controls.
For example, when a new value is selected from a
ComboBox object a Label has to display a new value.
Both the ComboBox and the Label are not aware of
each other structure and all the interaction is
managed by the Dialog object.
Each control is not aware of the existence of other
controls.

Example 2
26

A chat room where multiple users can send


message to chat room and it is the responsibility
of chat room to show the messages to all users.
We have created two classes ChatRoom and
User.
User objects will use ChatRoom method to share
their messages.
MediatorPatternDemo, our demo class, will use
User objects to show communication between
them.

Example
27

Sample Co
de

28

Mediator (Software
counterpart)
Gtalk
Server

Potrebbero piacerti anche