Lesson 6 | Three Types of Design Patterns: |
Objective | Distinguish between Behavioral, Creational, and Structural Design Patterns. |
Three Types of Design Patterns
GofPatterns
Design patterns are divided into three fundamental groups:
- Behavioral,
- Creational, and
- Structural
Behavioral Patterns
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
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. vThey are particularly useful when you are taking advantage of polymorphism and need to choose between different classes at runtime rather than compile time.
Three Types of Design Patterns
Polymorphism allows instances of different classes to be used as an instance of a common
superclass[1]. For example, an
EncryptionStream
object might only have a reference to an abstract
Cipher
object. The type of encryption the
EncryptionStream
performed could be adjusted by changing from a
DESCipher
to an
RSACipher
. In cryptology, RSA is an algorithm for public-key cryptography. It was the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptography. RSA is widely used in electronic commerce protocols, and is believed to be secure given sufficiently long keys and the use of up-to-date implementations.
Real World Example of Polymorphism:
Example 1: A Teacher interacts with student. A Teacher interacts with his or her seniors.
Here teacher is an object but the attitude is different based on the situation.
Example 2: Person interacts with genetic offspring in house at the same time that person interacts with an empoloyee in the office.
Example 3: Your mobile phone, has one name of many forms:
- As phone
- As camera
- As mp3 player
- As radio
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
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.
[1]superclass: A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors.