To: "simple" design pattern, then
Authors: Yang Ning Chapter Introduction
1. This article is not suitable ...
This article is not suitable to the decoration through the house in this article the reader;
This article is not suitable for object-oriented programming expert, will be a waste of your time. If you are willing to take time to read this article and made valuable suggestions, Thank you very much! What? You have not heard of design patterns? Then you said you dare?
2. In this paper, suitable for ...
If you are interested in object-oriented programming, and did not have time to read Gang of Four's "Design Patterns Elements of Reusable Object-Oriented Software" (hereinafter referred to as "design patterns"). Well, this article will help you understand the 23 kinds of design patterns.
I first read the book before eachalmost always fall asleep first. "Design pattern" in a rigorous, systematic exposition of the 23 kinds of style to the design mode, the original book can be said that object-oriented programming is a basis for curriculum, but to grasp its essence, we must have to spend a certain amount of energy. The purpose of this paper is to help you more easily understand the design of each model, does not want to be a substitute for reading the original book.
This article has no intention to introduce the basic knowledge of object-oriented. Therefore, the assumption that readers of this article has been object-oriented encapsulation, inheritance and polymorphism have sufficient knowledge and understanding. And be able to recognize the reusable object-oriented design of the two principles:
● interface for programming, rather than the realization of programming;
● priority target portfolio, rather than type of inheritance.
3. What is a Design Pattern?
The concept of design patterns is proposed by architect Christopher Alexander: "Each pattern describes a recurring constantly around us, as well as the solution to the problem of the heart. In this way, time and again you will be able to use the program and duplication of effort do not have to do. "definition of the above is a broad definition of design patterns. We will apply it to the field of object-oriented software, on the formation of a narrow definition of design patterns.
We can simply think: the design model is to solve a specific problem of object-oriented software-specific methods. However, for a strict understanding of the above is not accurate, are object-oriented software, only a mere 23 questions? Of course not.
In order to more accurately understand the concept, we introduce another term: framework (Framework). In today's framework of the term has a wide range of applications and meanings. In the design mode: Framework (Framework) is a particular type of software constitutes a reusable design of a type of mutual collaboration.
Framework can be regarded as applicable to a particular area of a package. This package provides all areas of the solution to the problem. Then it and What is the difference between design patterns?
● design patterns and frameworks for different domains of the problem:
Design patterns for object-oriented problem domain; framework for a specific business problem domain;
● design pattern is more abstract than frameworks:
Design Patterns in the specific problems encountered in order to generate code; framework code that can be used.
● framework design model is smaller than the structural elements of the system:
Framework may include a number of design patterns.
Tips: Just as design patterns in basic martial arts movements. We will reasonably These movements combined to form routines (framework).
4. Why use design patterns?
As good programmers are aware of a basic standard procedures: high polymerization and low coupling. The structure of object-oriented language than the language of the more complex, non-performing or not fully considered in the design will lead to re-design and development software. However, the actual design process, the more designers to consider how to solve business problems, to consider the internal structure of the software less; design model complements this defect, which is considering how to reduce dependence between objects, reducing the degree of coupling, makes it easier to expand the system to improve the reusability of the object. Therefore, the right designer can use design patterns to optimize the structure of the system.
Introduction Chapter Summary of "Design Patterns" book, containing a total of 23 models. According to different purposes, they are divided into three categories:
● the creation of type (Creational): to solve the problem of how to create objects.
● Structural (Structural): how the right combination of issues or objects.
● Behavioral (Behavioral): to solve or how the interaction between the object and how the issue of allocation of responsibilities.
Tips: design pattern often used in the abstract (Abstract) and concrete (Concrete) that the two words. Abstract description of the meaning it refers to the class (methods) is the interface type (method), the specific description of the meaning it refers to the class (methods) to achieve the corresponding abstract class (methods).
Chapter III of the abstract factory (Abstract factory)
1. Intentions to provide a series of related or interdependent to create the interface objects, and without specifying their specific type.
2. Classification model created.
3. The question is what?
Are not familiar with this model for those who will plant (Factory) surprised the word, why use the word? After, we will run into another mode - the factory method (Factory method), so to explain the significance of the factory:
House by the walls, doors, windows, floors, ceilings, columns composed. If we are prepared for customers to build a software house, we will walls, doors, windows, floors, ceilings, as different types of columns: WallClass, DoorClass, WindowClass, CeilingClass, PillarClass.
Now we create a new Class A, of this type have CreateWall (), CreateDoor (), CreateFloor (), CreateCeiling (), CreatePillar () of five methods, each method is to create the function and return the corresponding object. If the WallClass, DoorClass, WindowClass, CeilingClass, FloorClass, PillarClass products as examples, then the category A is like a factory production of these products. This is the use of the word of the reasons for factories.
Tips: A bad name, if HouseFactory will be better. Under normal circumstances, the use of your system in a model, the best keywords to use as a model of the corresponding method type or part of the name, so that your fellow defenders code or system can understand your intention.
Completion of our software, customers very satisfied. However, our customers want to export the software, he found a problem, the localization of the software too, and are built by the Chinese-style housing. So he wants to make our software to build a house style of different regions.
This is our problem! We need to redesign the original system and the completion of a different architectural style around the world is impossible. We will complete the first part of the style (the first customer to put the country's software), and then increase the Other ...
1) the establishment of an abstract Factory (Abstract Factory) category HouseFactory, statements in this category:
2) the establishment of the corresponding products of the abstract (Abstract Product) Cluster:
Wall, Door, Floor, Ceiling, Pillar
3) the establishment of different styles for the corresponding concrete factory (Concrete Factory) category (do not forget their relations), such as:
4) the establishment of different styles to the specific product (Concrete Product) category (an abstract realization of the corresponding products), such as:
5. Talk I think you must understand how to create a flexible and use a lot of the above categories:
● Repeat the last two steps, you can add a new style of convenience;
● the use of the first two steps of the abstract class declaration to achieve the operation.
The focus of the abstract factory pattern that is not a statement of abstract factory class, but it stated that a series of abstract product category, we have an abstract through the use of these products can operate or not we have achieved the realization of specific product categories, and to ensure their consistency.
You may have found this software you can not build two-story villas, because it does not have stairs. To this end, we Stair to the definition of abstract class, abstract method it is necessary to increase CreateStair; the most important thing is we have to have been completed in the 76 kinds of style added to the corresponding categories of Stair, this is a big trouble. Indeed, the abstract factory pattern to adapt to the new products in the capacity is relatively weak. This is one of its shortcomings.
Chapter IV Generator (Builder)
1. Attempt to build a complex object that its separation, making the same building process that can create different.
2. Classification model created.
3. The question is what?
A chapter in the abstract factory, we learned that the housing system in the world how to support a variety of housing styles. The new question is: the user would like to see the same structure, the appearance of different styles of housing. For example: This house has a floor, a ceiling, four walls, a door, one of the windows. He would like to see the Chinese style housing and the corresponding Greek style housing.
1) Create a generator (Buider) categories:
BuildHouse () ()
BuildWall (int) ()
BuildDoor (int) ()
BuildWindow (int) ()
BuildFloor () ()
BuildCeiling () ()
BuildPillar () ()
GetHouse () (return Null;)
In this class, we each have a housing element method defines a Build. And defines a way to return to structure the results of GetHouse.
2) the definition of each style are a specific generator (Concrete Builder) categories:
And in these heavy-duty parent class method.
3) In addition, various types of housing categories:
5. How to use we can use the following method uses the above categories to solve our problem.
object Create (builder HouseBuilder)
builder.Door (1); / / in the construction of a door Wall1
builder.Window (2); / / in a window built Wall2
return builder.GetHouse ();
As long as the introduction to the methods adopted by different HouseSystem.Create generator can be the result of different styles.
In fact, HouseSystem generator model is also a participant-oriented are called (Director). Attention:
● Generator (HouseBuilder) category to include elements of each component of the generation method. For example: the staircase of the elements of architectural style, in some not, in other styles there, but also with HouseBuilder in BuilderStair method.
● Generator (HouseBuilder) category is not an abstract class that every generation method under normal circumstances do nothing. In this way, the specific generator and it would not have to consider elements not related to the generation method.
1) Perhaps you have such thoughts: Generator model seems too complicated, you can type directly in the House of the definition of specific CreateHouse methods can, for example:
ChinaHouse CreateHouse ()
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)); / / in the construction of a door Wall1
house.Add (new Window (2)); / / in a window built Wall2
Generator model and at least two categories to solve this problem: From category-oriented and specific type of generator.
Well, good pattern generator where? The answer is that the distribution of responsibilities. Generator model will generate a complex object made of the duties of a good distribution. It put the process-oriented structure of those methods, the assembly process on the specific type of generator. Help us take a look at the following.
2) HouseSystem category (De-oriented) can be very delicate to construct the House. This generation process, the product category (ChinaHouse, GreeceHouse ...) and type of generator (ChinaHouseBuilder, GreeceHouseBuilder) are concerned there is no need to. Specific type of generator is to consider the issue of assembly elements.
7.pattern generator can be applied to the following issues:
● system format conversion of documents to other formats (each format of the document are the equivalent of a product).
● compile problems (syntax analyzer is oriented, and compiled the results of the products).
About the author:
Yang Ning is a company overseas GrapeCity technical backbone of the Department of application development. Engaged in development for many years, there are Unix, Windows platform development experience. Of VB, c #, VB.Net, xml have a rich understanding. OO programming like research, analysis, design, project management and other related technology. Like learning new technologies.
.Net Object Oriented Programming Articles
- To: design patterns Singleton (the example is C #)
- Words - a prototype model
- Words: one-piece model
- To the practical application of a factory. Can be directly used by heart
- To: "simple" design pattern, then
- I Walaqi, such as with Black_Cat on "design patterns" and "factory method" "abstract factory" organize the discussion as follows, to facilitate "design mode" fans
- Words - the structure of model
- Remarks: With regard to the composition of
- Give you an example [. NET framework for development in the three examples - the user registration system]
- Jia-wen Recommended: VB.NET use object-oriented features
- Microsoft patterns and practices: model papers (Microsoft Patterns & Practices: Patterns) [strongly recommended]
- C # written norms
- Help me, I overloading (C #)
- Microsoft. NET's base class inheritance (1)
- Microsoft. NET's base class inheritance (2)
- The birth of a poker game --- (fullhouse) category on the process of design and debugging, error correction and the use of assertion (I)
- VSS data (Part III)
- VSS information (Part II, picture not uploaded)
- VSS information (and I wrote for the company, part of ICP's)
- In the team development environment using Visual Studio. NET (b)
Can't Find What You're Looking For?
Rating: Not yet rated