History: Objectives: Design Patterns
History: Objectives: Design Patterns
History: Objectives: Design Patterns
History
The origin of design patterns lies in work done by an architect named
Christopher Alexander during the late 1970s. He began by writing two
books, A Pattern Language[Alex77] and A Timeless Way of Building
[Alex79] which, in addition to giving examples, described his rationalle
for documenting patterns.
The pattern movement became very quiet until 1987 when patterns
appeared again at an OOPSLA conference. Since then, many papers
and presentations have appeared, authored by people such as Grady
Booch, Richard Helm, and Erich Gamma, and Kent Beck. From then
until 1995, many periodicals, featured articles directly or indirectly
relating to patterns. In 1995, Erich Gamma, Richard Helm, Ralph
Johnson, and John Vlissides published Design Patterns: Elements of
Reusable Object-Oriented Software [Gamma95], which has been
followed by more articles in trade journals, and an additional book by
<To be filled in later>.
Patterns Defined
Patterns are devices that allow programs to share knowledge about
their design. In our daily programming, we encounter many problems
that have occured, and will occur again. The question we must ask
ourself is how we are going to solve it this time. Documenting patterns
is one way that you can reuse and possibly share the infomation that
you have learned about how it is best to solve a specific program
design problem.
Essay writting is usually done in a fairly well defined form, and so is
documenting design patterns. The general form for documenting
patterns is to define items such as:
1. The motivation or context that this pattern applies to.
2. Prerequisites that should be satisfied before deciding to use a pattern.
3. A description of the program structure that the pattern will define.
4. A list of the participants needed to complete a pattern.
5. Consequences of using the pattern...both positive and negative.
6. Examples!
Example 1 : Singleton
[Gamma95]
Advantages
What are the advantages to using this pattern?
Single instance is controlled absolutely. When implemented as the
pattern recommends, the class will have direct control over how many
instances can be created. This is in contrast to making the programmer
responsible for insuring that there is only one instance.
This pattern is easily extensible to allow a controlled number of
"singleton" objects to be created. The most important modification
needed to accomplish this change is in the operator that has control
over access to the instances. In this case, the Instance() function would
need to be changed.
Examples Here is how a generic singleton might be written in C++. If
you want to try it, here is the source code: singleton_ex.tar
class Singleton {
public:
static Singleton* Instance(); // gives back a real object!
static proof(void);
// proof that the object was made
protected:
Singleton();
// constructor
private:
static Singleton* _singleton;
};
return _singleton;
// end Instance()
Example 2 : Facade
Rationalle and Motivation
The facade pattern can make the task of accessing a large number of
modules much simpler by providing an additional interface layer. When
designing good programs, programmers usually attempt to avoid
excess coupling between module/classes. Using this pattern helps to
simplify much of the interfacing that makes large amounts of coupling
complex to use and difficult to understand. In a nutshell, this is
accomplished by creating a small collection of classes that have a
single class that is used to access them, the facade.
Classes
There can be any number of classes involved in this "facaded" system,
but I would think that the minimum is four classes. One client, the
facade, and the classes underneath the facade. In a typical situation,
the facade would have a limited amount of actual code, making calls to
lower layers most of the time.
Advantages/Disadvantages
As stated before, the primary advantage to using the facade is the
make the interfacing between many modules or classes more
managable. One possible disadvantage to this pattern is that you may
lose some functionality contained in the lower level of classes, but this
depends on how the facade was designed.
Examples
Imagine that you need to write some program that needs to represent
a building as rooms that can be manipulated. Manipulated as in
interacting with objects in the room to change their state. The client
that has ordered this program has determined that there will only be a
need for a finite number of objects possible in each room, and a finite
number of operations that can be performed on each of them.
You, as the program architect, have decided that the facade pattern
will be an excelent way to keep the amount of interfacing low,
considering the number of possible objects in each room, and the
actions that the client has specified.
Consider the sheer simplicity from the client's side of the problem. A
less thought out design may have looked like this, making lots of
interaction by the client necessary.