Author: Yang Ning
First chapter introduction
1. This article is not suitable ... This article is not suitable for renovating the house by this article;
This article is not suitable for object-oriented programming masters, will waste your time. If you are willing to draw a time to read this article, and make a valuable suggestion, thank you very much! what? Have you heard of design mode? Then do you dare to say a master?
2. This article is suitable for ... If you are interested in object-oriented programming, there is no time to read the "Design Patterns Elements of Reusable Object-Oriented Software" (hereinafter referred to as "Design Mode"). Then, this article will help you understand 23 design patterns.
I first read this book for every night, I fell asleep every time. "Design mode" is discussed in a rigorous and systematic style, and the original book can be said to be a basic tutorial for object-oriented programming, but it is necessary to understand its essence. The purpose of this article is to help you understand each design pattern more convenient, and do not want to be an alternative reading of the original book.
This article is not intended to introduce the basic knowledge of object-oriented. Therefore, it is assumed that the readers of this article have sufficient understanding and understanding of the encapsulation, inheritance and polymorphism of the face-to-objective object. And can recognize two principles for object-oriented object-oriented design:
● Programming for the interface, not for implementation;
● Prioritize the object combination, not the class inheritance.
3. What is the design pattern? The design pattern concept is proposed by architect Christopher Alexander: "Every pattern describes a problem that has been repeated around us, as well as the core of the solution. In this way, you can use the scheme again and again. You don't have to do repetitive labor. "The definition of the above is a broad definition of design patterns. We apply it to the domain-oriented software, which forms a narrow definition of design patterns.
We can simply think that design mode is to solve a particular method of a particular object-oriented software problem. But strictly, the above understanding is inaccurate, is there only 23 problems in the area of the object software? of course not.
In order to be able to understand this concept more accurately, we introduce another term: Framework. The framework has a variety of applications and meanings today. In the design mode: Framework is a class that forms a class of collaborative classes that make each other software that can be used.
The framework can be considered a package suitable for a certain area. This package provides a solution for various problems in the field. So, what is the difference between it and the design model?
● The design mode and the framework for the framework are different:
Design mode for object-oriented problem domains; framework for specific services;
● The design pattern is more abstract than the frame:
The design pattern can generate code after encountering specific problems; the framework can be represented by code.
● The design pattern is a smaller architecture element than the frame:
Multiple design patterns can be included in the framework.
TIPS: Design mode is like basic moves in martial arts. We combine these moves to form a routine (framework).
4. Why use the design mode? As a programmer knows a basic standard for a good program: high polymerization, low coupling. Object-oriented language is more complicated than structured languages, and there is no designs that have not been fully considered will result in software redesign. However, during the actual design process, designers more considerations how to solve business problems, accounting less in internal structure; design model adds this defect, which mainly considers how to reduce the dependence between objects, reduce coupling degree, Make the system easier to expand and improve object reuse. Therefore, the correct use of design modes can optimize the structure inside the system. Chapter II Summary Introduction
In the "Design Mode" book, 23 modes have been included. Depending on the purpose, divide them into three categories:
● Creating: Solve how to create an object.
● Structural: Solve the problem of how to make a correct combination or object.
● Behavioral: Solve how to interact between class or objects and how to assign responsibilities.
TIPS: Abstract (Abstract) and concrete are often used in design patterns. Abstract meaning refers to the class (method) it described is an interface class (method), and the specific meaning refers to the class (method) it described to implement the corresponding abstraction class (method).
Chapter 3 Abstract Factory (Abstract Factory)
1. It is intended to provide an interface to create a series of associated or interdependent objects without specifying their specific classes.
2. Category creation mode.
3. What is the problem? People who don't familiarize with this model will be strange to the factories (Factory). Why use this word? After that, we will also encounter another model - factory method (Factory method), so explain the meanings of the factory:
The house is made up of walls, doors, windows, floors, ceilings, pillars. If we write a software built a house for our customers, we will see the walls, doors, windows, floors, ceilings, pillars as different classes: Wallclass, Doorclass, Windowclass, CEILINGCLASS, PILLARCLASS.
Now we create a new class A, which has CreateWall (), createDoor (), createfloor (), five methods, and the functionality of each method is to create and return the corresponding object. If WallClass, Doorclass, Windowclass, CEILINGCLASS, FLOORCLASS, PILLARCLASS, as a product, then class A is like a factory that produces these products. This is why use the word used in the factory.
Tips: a This name is too bad if you use HouseFactory. Under normal circumstances, in your system uses a mode, it is best to use the keyword according to the mode as part of the name or method, so your companion or system code maintainer will understand your intention.
Our software is complete, and customers are very satisfied. However, our customers want to export this software, he found a problem, this software is too local, and it is a Chinese-style house. So he wants our software to build a different regional house.
This is our problem! We need to redesign the original system, and it is impossible to complete different architectural style around the world. We will first complete some style (customer first to put software countries), then add other ... 4. Solution 1) Establish an abstract factory (Abstract Factory) HouseFactory, declare in this class:
CreateWall ()
Createdoor ()
Createfloor ()
CreateceIling ()
Createpillar ()
2) Establish a corresponding abstract product (Abstract Product) class:
Wall, Door, Floor, Ceiling, Pillar
3) Establish corresponding specific factories for different styles (don't forget to achieve relationships), for example:
ChinaHouseFactory: HouseFactory
GreecehouseFactory: HouseFactory
...
4) Establish a corresponding specific product class for different styles (implement corresponding abstract products), for example:
ChinaWall: Wall
Chinadoor: door
...
GreeceWall: Wall
Greecedoor: Door
...
5. Talks, I think you must understand how to flexibly create and use a lot of columns above:
● Repeat the last two steps, you can add new styles.
● Use the abstraction class declared in the previous two steps to implement the operation.
Abstract factory mode is not the abstract factory class that is declared, but it is declared a series of abstract product classes, we can operate the specific product classes we have implemented or not implemented by using these abstract product classes, and guaranteed them consistency.
You may have discovered that this software can't build your two-story villa because it doesn't have stairs. To do this, we have to define the Stair abstraction, but also increase the CreateStair abstraction method; the most important thing is that we have to increase the corresponding Stair class in the 76 style has been completed, which is a big trouble. Indeed, abstract factory model is relatively weak in adapting to new products. This is a shortcoming it.
Chapter 4 Generator (Builder)
1. It is intended to separate the construction of a complex object with its representation, so that the same build process can create different representations.
2. Category creation mode.
3. What is the problem? In an abstract factory chapter, we learned how to support a variety of houses in the global building system. The new question is: The user wants to see the same structure, the appearance of different styles of houses. For example: this house has a floor, a ceiling, four walls, a door, a window. He wants to see the corresponding style of Chinese-style housing and Greek-style housing.
4. Solution
1) Create a generator class:
Class Housebuider
{
Buildhouse () {}
Buildwall (int) {}
BuildDoor (int) {}
BuildWindow (int) {}
Buildfloor () {}
Buildceiling () {}
Buildpillar () {}
Gethouse () {return null;}
}
In this class, we define a Build method for each housing composition element. And define a method of returning the constructor gethouse.
2) Define a specific generator (Concrete Builder) for each style:
ChinaHousebuilder: HousebuildergreecehouseBuilder: Housebuilder
...
And the method of heavyizing the parent class in these classes.
3) Also, various types of housing classes:
Chinahouse
Greecehouse
...
5. How to use the following methods to solve our problem with the following methods.
Class HosueSystem
{
Object Create (Builder Housebuilder)
{
Builder.buildhouse ();
Builder.buildfloor ();
Builder.buildceiling ();
Builder.buildwall (1);
Builder.Buildwall (2);
Builder.buildwall (3);
Builder.Buildwall (4);
Builder.door (1); // Built a door on Wall1
Builder.Window (2); // Built a window on Wall2
Return builder.gethouse ();
}
}
As long as you get different generators through the HouseSystem.create method, you can get the results of different styles.
In fact, HOUSESYSTEM is also a participant in the generator mode called the Director. Note the question:
● The generator (HouseBuilder class "to include the generation method of each component element. For example: the elements of the stairs are not in some architectures. In other styles, it is also necessary to add the Builderstair method in Housebuilder.
● The HouseBuilder class is not an abstract class, and each generation method is generally nothing. Thus, the specific generator does not have to consider the generation method of the element independent.
6. Talk
1) Maybe you have this idea: generator mode seems to be too complex, you can define the Createhouse method directly in the specific House class, for example:
Class Chinahouse
{
ChinaHouse Createhouse ()
{ChinaHouse House;
House = new chinahouse ();
House.Add (New floor ());
House.Add (New CEILING ());
House.Add (New Wall (1));
House.Add (New Wall (2));
House.Add (New Wall (3));
House.Add (New Wall (4));
House.add (New Door (1)); // Building a door on Wall1
House.add (new window); // built a window on Wall2
Return House;
}
}
The generator mode uses at least two classes to solve this problem: the guideline class and the specific generator class.
So, where is the generator mode? The answer is the assignment of responsibilities. Generator mode has a good allocation of the build of a complex object. It puts the construction process into the guideline, put the assembly process in the specific generator class. Let's take a look at the description below.
2) The HouseSystem class (guideder) can construct House very fine. This generation process is not necessary to care for the product class (ChinaHouse, Greecehouse ...) and generator class (GreecehouseBuilder). Specific generator classes consider the problem of assembling elements.
7. The live generator mode can be applied to the following issues:
● Convert the system's document format to other formats (all documents in each format are equivalent to one product). ● Compiling the problem (the syntax analyzer is the guide, the compile results are the product).