(Observe the Iterator Visit, Strategize, and act as Mediator to create Memories)
Observer Design Pattern (Behavioral):
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Define an object that is the “keeper” of the data model and/or business logic (the Subject). Delegate all “view” functionality to decoupled and distinct Observer objects. Observers register themselves with the Subject as they are created. Whenever the Subject changes, it broadcasts to all registered Observers that it has changed.
Example:
1) The observer pattern is used in the model view controller (MVC) architectural pattern. In MVC the this pattern is used to decouple the model from the view. View represents the Observer and the model is the Observable object.
2) News Publishing:
http://www.oodesign.com/observer-pattern.html
3) In Yahoo, whenever state of user changes, all concerned properties are notified and action taken accordingly.
Iterator Design Pattern (Behavioral) :
A collection is just a grouping of some objects. They can have the same type or they can be all cast to a base type like object. A collection can be a list, an array, a tree and the examples can continue. But what is more important is that a collection should provide a way to access its elements without exposing its internal structure. We should have a mechanism to traverse in the same way a list or an array. It doesn’t matter how they are internally represented.
The idea of the iterator pattern is to take the responsibility of accessing and passing trough the objects of the collection and put it in the iterator object. The iterator object will maintain the state of the iteration, keeping track of the current item and having a way of identifying what elements are next to be iterated.
“An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you need to accomplish. But you probably don’t want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you’ll require. You might also need to have more than one traversal pending on the same list.” And, providing a uniform interface for traversing many types of aggregate objects (i.e. polymorphic iteration) might be valuable.
The Iterator pattern lets you do all this. The key idea is to take the responsibility for access and traversal out of the aggregate object and put it into an Iterator object that defines a standard traversal protocol.
Example:
1) Book Collection: (uses “nested class”)
http://www.oodesign.com/iterator-pattern.html
Visitor Design Pattern (Behavioral) :
Represents an operation to be performed on a set of objects in a collection. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Example:
1) taxi example, where the customer calls orders a taxi, which arrives at his door. Once the person sits in, the visiting taxi is in control of the transport for that person.
2) Shopping in the supermarket is another common example, where the shopping cart is your set of elements. When you get to the checkout, the cashier acts as a visitor, taking the disparate set of elements (your shopping), some with prices and others that need to be weighed, in order to provide you with a total.
3) Postage Visitor applied to elements in a shopping cart:
http://java.dzone.com/articles/design-patterns-visitor
Main Class will implement a accept() method. example Book class will implement accept() method which will accept a visitor. PostageVisitor class will implement a visit method which will do the calculation.
public void accept(Visitor vistor) {
visitor.visit(this);
}
Strategy Design Pattern:
The Strategy pattern provides a way to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. A good use of the Strategy pattern would be saving files in different formats, running various sorting algorithms, or file compression
Memento Design Pattern:
Memento pattern which is used in undo frameworks to bring an object back to a previous state
Mediator:
An airport control tower is an excellent example of the mediator pattern. The tower looks after who can take off and land – all communications are done from the airplane to control tower, rather than having plane-to-plane communication. This idea of a central controller is one of the key aspects to the mediator pattern.