Design pattern overview
Overview
This particular article introduce the prior knowledge of design pattern. For more detail explanation, please read later posts on design pattern. It is important to learn those design pattern to become a good software engineer. So, lets get started.
In the first section, we focus on the 7 principles of software designs. Then, in the second section, we would briefly introduce the three categories of the design pattern. We would gain overall control of the design pattern in a certain level. Finally in the third section, UML diagram are introduced to help us understand the design pattern code.
1. Seven principles of design pattern
Whether in our learning of the design pattern, or the daily development period, we better follow the same design principles.
There are 7 design principles in common design principles, they are open and close principle, Richter substitution principle, Dependency inversion (inversion) principle, single responsibility principle, interface isolation principle, Dimitt’s law, and synthetic reuse principle.
There are only ONE goal of those principles: “Low in coupling and high in cohesion. High reusability, extendability and maintainability”
Design Principle | Description | Goal |
---|---|---|
Open and close principle | Open for extension, close for modification | Lower the risks from maintenance |
Dependency inversion principle | High level should not depends on low level, it need to have interface oriented programming | Better for upgrading and extending the code architecture |
Single responsibility principle | One class/method only do one thing, Implementation class need to be single. | Help us understand and make the code more readable. |
Interface isolation principle | One interface do one thing, interface need to be simple | Decouple the functions, low in coupling and high in cohesion. |
Dimitt’s law | Do not know what it should not need to know. A class need to have the least understanding of its objects. It lowers the coupling. | Only talk to friends, do not take with strangers, decrease the entropy (Chaotic) of the code. |
Richter substitution principle | Do not break the inheritance. The overwrite or rewrite of the child class functions/methods should not affect the parent class’s functions/methods | Prevent the inheritance flood |
Synthetic reuse principle | Use grouping or Aggregate relationships to achieve code reuse, and use less inheritance | Lower the code coupling |
Those principles will be shown during our development phase. In most of the time our development is just copying without knowing why we need to do so. With those design principles we can think about WHY we need to do such in our codes.
There is another classic use case, we define the variable in an entity using private
, and use get
& set
method to access those variables, but why we do not define those variables as public
and access them directly?
Design principles are everywhere in our development. Think more on the coding habits so that you know why you do so.
2. Design pattern categories
Check this section while reading the design pattern notes later. It really helps : )
I. Creational
The focus of creational pattern is “how to create an object?” Its main point is to “separate the creational and the use”.
- Singleton pattern: One class can only create one instance, this class provide a global access point for outside to get this instance, Multiton is its extended version
- Prototype pattern: Make one object as a prototype, create similar new instances from the prototype.
- Factory Method Pattern: Define an interface to create the product, and its child class decides on what product are being produced.
- Abstract Factory Pattern: Provide a interface to create product family, and every child class can create a series of related product
- Builder Pattern: Divide a complex object into multiple simple parts, create them base on different requirements, and build the complex object. (Similar to divide and conquer)
II. Structural
Structural pattern describe how to make a larger structure base on certain layout. It has like-structural method and object structural pattern. The former use inheritance to operate interface and class, and the latter use grouping or coupling to group the objects.
- Proxy pattern: Provide a proxy for access control of an object. That is, access the object implicitly through proxy to limit, increase or change the features of the object
- Adapter Pattern: Replace one interface with another interface that the client wants, so that those incompatible class of the original interface can work together now.
- Bridge pattern: Isolate the abstraction and implementation, so that they can change on their own. That is through the combination relationship in replacing of the inheritance relationship. It lower the coupling of the abstract and implementation.
- Decorator pattern: Add some responsibility of an object dynamically, so that it can have extra features/functionality.
- Facade pattern: Provide a consistent interface for a complex sub-system, so that those sub-systems are easy to access
- Flyweight pattern: Use shared technology to support the reusability of large amount of Fine-grained objects
- Composite pattern: Combine the objects into tree like structure, so that the user can have consistent access of single object and grouped object.
III. Behavioral
Behavioral pattern is used to describe the complex control flow when running a program. i.e. describe how multiple class or objects work together as a whole to accomplish a task that a single object cannot do. It involves the distribution of algorithm and object.
Behavioral pattern has class behavioral pattern and object behavioral pattern. The former used inheritance to distribute behavior between classes, and the later uses combination or cohesion to distribute behavior within objects. Because the combination or cohesion relationship has lower coupling in inheritance, it satisfy composite reuse principle. Hence the object behavioral pattern is more flexible than class behavior pattern.
- Template Method Pattern: It defines the algorithm skeleton of an operation. It delay some algorithm steps into the subclasses, so that the subclasses can redefine some certain steps of the algorithm without changing the algorithm structure.
- Strategy Pattern: It defines a series of algorithm, and encapsulate each algorithm so that they are interchangeable. The change of the algorithm does not affect the client that using the algorithm.
- Command Pattern: Encapsulate a request as an object so that it separates
Note:
This is an English translation of This blog article
Hope you enjoyed : )