The Object-Oriented Thought ProcessChapter15
One of the most important books on object-oriented software development isDesignPatterns: Elementsof Reusable Object-Oriented SoftwarebyErich Gamma, Richard Helm, RalphJohnson,andJohnVlissides.Thebook’s authors havebecome knownas the Gang ofFour (GoF).
Why Design Patterns?
The concept of design patterns did not necessarily start with the need for reusable software.In fact, the seminal work on design patterns is about constructing buildings and cities.As ChristopherAlexander noted inA Pattern Language: Towns, Buildings, Construction,“Eachpatterndescribesa problem which occurs over and over again in our environment, and thendescribes thecore of the solution tothat problem, in such a way that you can use the solution amillion timesover, without ever doing it the same way twice.”
The Four Elements of a Pattern
TheGoFdescribe a pattern as having four essentialelements:Thepattern name is a handle we can use to describe a design problem, itssolutions,andconsequences in a word or two.Theproblem describes when to apply the pattern.Thesolution describes the elements that make up the design, their relationships,responsibilities,andcollaborations.Theconsequences are the results and trade-offs of applying the pattern.
For historical perspective, we need to consider the Model/View/Controller (MVC)introduced inSmalltalk (and used in other object-oriented languages).MVCis often used to illustratethe originsof design patterns.TheModel/View/Controller paradigm was used to create userinterfaces inSmalltalk. Smalltalk was perhaps the firstpopular object-oriented language.
Smalltalk is the result of several great ideas that emerged from Xerox PARC.These ideas includedthe mouse and using a windowing environment, among others.Smalltalkis awonderful languagethat provided the foundation for all the object-oriented languages that followed.One of the complaints about C++ is that it’s not really object-oriented, whereas Smalltalk is.
The MVC wasone ofthe early pioneers in this separation of interfaces.TheMVC explicitly defines theinterfaces betweenspecific components pertaining to a very common and basic programmingproblem:thecreation of user interfaces and their connection to the business logic and data behind them.
Although the MVC is a great design, it can be somewhat complex in that a lot of attentionmust bepaid to the upfront design.Thisis a problem with object-oriented design in general—thereis afine line between a good design and a cumbersome design.Thequestion remains: Howmuch complexityshould you build into the system with regard to a complete design?
Types of Design Patterns
Design Patterns is inherently a designbook,andthe patterns can be implemented in any number of languages.Theauthors of thebook dividedthe patterns into three categories:Creationalpatterns create objects for you, rather than having you instantiateobjectsdirectly.Structuralpatterns help you compose groups of objects into larger structures, suchascomplexuser interfaces or accounting data.Behavioralpatterns help you define the communication between objects in yoursystemandhow the flow is controlled in a complex program.
The creational patterns consist of the following categories:AbstractfactoryBuilderFactorymethodPrototypeSingleton
The Singleton Design Pattern
The singletonpattern isa creational pattern used to regulatethe creationof objects from a class to a single object.Forexample, if you have a website that hasa counterobject to keep track of the hits on your site, you certainly do not want a newcounter tobe instantiated each time your web page is hit.
More Than One Reference
There may well be more than one reference to the singleton.Ifyou create references inthe application andeach reference is referring to the singleton, you will have to manage themultiple references.
TwoReferences to a Single Counter
Be aware that in this example, two separate references are pointing to the counter.Thus,when thecounter changes, both references will reflect the update.
Structural patternsare used to create larger structures from groups of objects.The following sevendesign patterns are members of the structural category:AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy
The Adapter Design Pattern
The adapter pattern is a way for you to create a different interface for a class that alreadyexists.Theadapter pattern basically provides a class wrapper.Inother words, you create a newclass thatincorporates (wraps) the functionality of an existing class with a newand—ideally—better interface.
The behavioral patterns consist of the following categories:ChainofresponseCommandInterpreterIteratorMediatorMementoObserverState
Iteratorsprovide a standard mechanism for traversing a collection, such as avector.Functionalitymust be provided so that each item of the collection can be accessed one ata time.Theiteratorpattern provides information hiding, keeping the internal structure ofthe collectionsecure.
The termantipatternderives from the fact that design patterns are created to proactivelysolveaspecific type of problem.Anantipattern, on the other hand, is a reaction to a problem andis gleanedfrom bad experiences.Inshort, whereas design patterns are based on solid designpractices,antipatternscan be thought of as practices to avoid.