|Lesson 6||Three Types of Design Patterns:|
||Distinguish between Behavioral, Creational, and Structural Design Patterns.|
Three Types of Design Patterns
Design patterns are divided into three fundamental groups:
- Creational, and
Behavioral patterns describe interactions between objects and focus on how objects communicate with each other.
They can reduce complex flow charts to mere interconnections between objects of various classes.
Behavioral patterns are also used to make the algorithm that a class uses simply another parameter that is adjustable at runtime.
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them.
These patterns characterize complex control flow that is difficult to follow at run-time.
They shift your focus away from the flow of control to let you concentrate just on the way objects are interconnected.
Behavioral class patterns use inheritance to distribute behavior between classes.
The Template Method is the simpler and more common of the two.
A template method is an abstract definition of an algorithm. It defines the algorithm step by step. Each step invokes either an abstract operation or a primitive operation.
A subclass fleshes out the algorithm by defining the abstract operations.
The other behavioral class pattern is Interpreter pattern, which represents a grammar as a class hierarchy and implements an interpreter as an operation on instances of these classes.
Creational patterns are used to create objects for a suitable class that serves as a solution for a problem. Generally when instances of several different classes are available.
They are particularly useful when you are taking advantage of polymorphism
and need to choose between different classes at runtime rather than compile time.
Creational patterns support the creation of objects in a system.
Creational patterns allow objects to be created in a system without having to identify a specific class type in the code, so you do not have to write large, complex code to instantiate an object.
It does this by having the subclass of the class create the objects. However, this can limit the type or number of objects that can be created within a system.
Structural patterns form larger structures from individual parts, generally of different classes.
Structural patterns vary a great deal depending on what sort of structure is being created for what purpose.
Structural patterns are concerned with how classes and objects are composed to form larger structures.
Structural class patterns
use inheritance to compose interfaces or implementations.
As a simple example, consider how multiple inheritance mixes two or more classes into one.
The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together.
Another example is the class form of the Adapter Pattern
In general, an adapter makes one interface (the adaptee's) conform to another, thereby providing a uniform abstraction of different interfaces. A class adapter accomplishes this by inheriting privately from an adaptee class.
The adapter then expresses its interface in terms of the adaptee's.