Problem
When a user interacts with a program, one of the important features is sometimes the ability to undo what was just done. This is common in many applications from graphics to word processing. In fact I just used it now in Microsoft Word. Often it is the case that the operation just executed cannot be reversed appropriately, there is a loss of original information or some other reason. How can we maintain a system that CAN reverse all operations?
Solution
The answer is the memento. A memento is simply a class that contains all information needed to recreate a specific state of an object. When an operation is performed the originator object performs the operation passes back a memento as a side effect. That memento is then stored, and then in the event that the system wishes to return the originator to the old state, then memento is simply passed back to the originator, who adjusts its internal state accordingly. At no time does anyone but the originator know the contents of the memento.
Consequences
Since only the originator know the contents of the memento, no one has to know the implementation details of the originator.
A caretaker object is required to create, store and delete mementos.
Mementos could be very expensive in terms of overhead if the internal state of the originator is complex. So, in some cases mementos might not be a valid solution.
Problem
“If a particular kind of problem occurs often enough, then in might be worthwhile to express [the] instances of a problem as sentences in a simple language.” The example is pattern matching using regular expressions. There is a simple language defined, regular expressions have a well defined syntax.
Solution
An interpreter simply implements the hierarchy of the BNF specification of a language. An interpreter defines an abstract expression and concrete subclasses that represent explicit types of expressions. Each expression has some parameters, which can be expressions, and the current expression contains some list of other expressions. By having the nesting ability of containing any other expression you can represent a BNF specification very well.
Consequences
It is easy to create and extend simple grammars, but complex grammars are hard to maintain
Observer Design Pattern
Problem
In a complex system you want “maintain consistency between related objects”. This is not a trivial task, since coupling the objects together reduces reuse of each component. How can you maintain consistency, yet also maintain the loose coupling?
Solution
If you make the related classes observers, who look at a common repository of data for their state, you can maintain consistency and no observer knows about the others. For example, in a spreadsheet application, the observers could be the numerical cells, and the graphs, and the pie charts. They all look to the data (subject) to determine how they should look. The subject has methods to attach, detach, and update the observers in the event that its state changes.
Consequences
Since all observers are derived off of a abstract base class, then all the subject knows is that it is being observed, not by whom.
When a subject changes, it tells all the observers, thus it can broadcast changes.
One problem is that in this system the no one can know the cost of changing the subject. It could be as simple as incrementing a pointer, or as complex as drawing the entire screen over again.
November 24th, 2009
Vinay
1. Intercepting Filter:
Problem : Preprocessing and post-processing of a client Web request and response are required.When a request enters a Web application, it often must pass several entrance tests prior to the main processing stage. For example,
Has the client been authenticated?
Does the client have a valid session?
Is the client’s IP address from a trusted network?
Does the request path violate any constraints?
What encoding does the client use to send the data?
Do we support the browser type of the client? Read more…