In This Chapter
Understanding what design patterns are and how you can use them
Implementing an Observer pattern
Building a Mediator pattern
When you work as a software designer, eventually you start to notice that you do certain things over and over. For example, you may get mad and shut off the computer. But that's not directly related to software design.
For an example related to software design, whenever we need to keep track of how many instances of a certain class get created, we always create a static member variable called something like
int InstanceCount; and in the constructor, we include a line that increments
InstanceCount; and in the destructor, we put a line that decrements
InstanceCount. Further, we typically make
InstanceCount private and include a static method that retrieves the value, such as
We have used this design so many times we know that it works. The first time we used it, we had to think about it and how we would design and implement it. Now, we barely have to think about it; we just do it, sometimes even when we're driving down the road. Well, maybe not then, but we do use it when we're designing software. Thus it's a design pattern that we use.
Way back in 1995, a book came out that became an instant best seller in the computer programming world. It was called Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. The four ...