Design Patterns - behavioral

mediator - def

mediator - behavioral
-Define an obj. that encapsulates how a set of objs interact.
-promotes loose coupling (by designing an intermediary)
-Promote the many-to-many relationships between interacting peers

mediator - components

-Mediator: Defines an interface for communicating with Colleague objs.
-Concrete mediator: knows colleagues objs (has a ref of each) + coordinates communication among them
-Colleague: objs in the set; each has a reference (knows) its mediator
-ConcreteCol

mediator - example

-Airport control tower -> it looks after who can take off and land
-all communications done from the airplane to control tower
(NO plane-to-plane communication)

mediator - advantages

-colleagues send msgs to mediator, not to other colleagues directly
-separation: makes reusability easier (Allows loose coupling)

memento

-provides the ability to restore an object to its previous state (undo via rollback)
-when a snapshot of (some portion of) an object's state/fields must be saved to be restored to that state later

memento - participants (1)

Participants:
i) Memento (SolverState)
..stores/define internal state/fields of the Originator(source) object.
..state/fields to save/restore are defined by originator
ii) Caretaker (container of mementos)
..pass (manage) the memento to other objects.
..p

memento - participants (2)

iii) Originator (ConstraintSolver)
..creates a memento containing a snapshot of its current internal state.
..uses the memento to restore its internal state.

memento - consequences

..mementos might be expensive (copy large ammounts of info)
..Hidden costs in caring for mementos: caretaker deletes mementos it cares for, but it has has no idea how much state is in the memento (may incur large storage costs)