A Design Pattern is a solution to a recurring design problem.
Pattern Essentials
- Pattern Name - Provides a method to communicate about design and formalizes
an idea thus allowing user to say "I've seen this before."
- Problem - When do you apply this pattern.
- Solution - An abstract description of problem design that can be used on many
problems.
- Consequences - Costs and benefits of applying a pattern.
Catalog Organization
There are 23 patterns in the Catalog. These are grouped using two criteria. The first is purpose. All of the patterns
are divided into three main purposes.
- Creation patterns used to create classes or objects of from a collection of objects.
- Structural patterns deal with the composition of classes and objects
- Behavioral patterns deal with ways in which objects or classes interact and
distribute responsibility.
A second way to classify patterns is their scope. This classification is
based on whether a pattern applies to classes or objects.
How Design Patterns Solve Design Problems
Finding Appropriate Objects
Hard part of ood is decomposition of a system into objects. The following factors
influence decompostion, often in conflicting ways.
- encapsulation
- granularity - size and number of objects in a class
- dependency
- flexibility
- performance
- evolution
- reusability
Some of the classes are based on concrete (real-world) objects while others
are problem solving artifacts such as structures (arrays etc.) and abstract base
classes.
Some Important Patterns
- Abstract Factory (87)
- Factory (107)
- Observer (293)
- Singleton (127)
- Strategy (315)
- Adapter (139)
Inheritance vs. Composition (Aggregation)
Inheritance (is-a) versus Composition (has-a) is an important decision in class design. Some important points of the section:
Weaknesses of Inheritence
- Inheritence breaks encapsulation since children often have to work with parents internal values.
- Can't change the implementations inherited from a parent at run-time because inheritence relations
are defined at compile-time.
- You may inherit functions that are not appropriate in the new class.
Advantages of Composition
- Object composition is defined dynamically at runt-time through objects acquiring refernces to other objects.
- Composed objects don't violate encapsulation.
- Fewer interface dependencies.
- Class hierarchies remain small.