Turn: "Jane" Language design mode

Source: Internet
Author: User
Tags abstract define definition generator include inheritance domain
The design pattern of "simple" words

Author: Yang Ning
Introduction of the first chapter
1. This article is not suitable for ...
This article is not suitable for the readers who want to decorate the house through this article;
This article is not suitable for object-oriented programming master, will waste your time. If you are willing to take the time to read this article and make valuable suggestions, thank you very much! What the? Have you ever heard of design patterns? So you dare to call it a master?
2. This article is suitable for ...
If you are interested in programming opposite to object, and have no time to read the "design Patterns Elements of reusable object-oriented Software" of Gang of Four (hereinafter referred to as "designing pattern"). So, this article will help you understand 23 design patterns.
The first time I read this book was to fall asleep almost every time before I went to bed late. "Design pattern" in a rigorous, systematic style to discuss 23 design patterns, the original book can be said to be object-oriented programming a basic tutorial, but to understand its essence, it must be spent a certain amount of energy. The purpose of this article is to help you understand each design pattern more easily and not to be a substitute for the original book.
This article is not intended to introduce object-oriented basics. Therefore, it is assumed that the readers of this paper have enough knowledge and understanding of object-oriented encapsulation, inheritance and polymorphism. and to recognize the two principles of reusable object-oriented design:
Programming for interfaces, not for implementation;
Use a combination of objects rather than class inheritance.
3. What is the design pattern?
The concept of design patterns is presented by architect Christopher Alexander: "Each pattern describes a recurring problem around us, and the core of the solution to the problem." This way, you can use the program again and again without having to do repetitive work. "The above definition is a broad definition of the design pattern." We apply it to the domain of object-oriented software, and form a narrow definition of design pattern.
We can simply argue that design patterns are a specific way to solve a specific object-oriented software problem. But strictly speaking, the above understanding is inaccurate, is there only 23 problems in object-oriented software? Of course not.
In order to understand this concept more accurately, we introduce another term: framework. The word framework has a wide variety of applications and implications today. In design mode: A framework is a set of mutually collaborative classes that form a reusable design of a particular software.
A framework can be considered a software package that works for a particular area. This package provides solutions for each problem in the corresponding domain. So what's the difference between it and the design pattern?
The design pattern and framework are different for the problem domains:
The design pattern is aimed at the object-oriented problem domain, the framework is for the specific business problem domain;


Design patterns are more abstract than frames:
The design pattern can produce code when it encounters a specific problem, and the framework is already ready to be represented in code.
A design pattern is a smaller architectural element than a frame:
You can include multiple design patterns in a frame.
Tips: Design patterns are like basic moves in Kung Fu. We combine these moves in a rational way, forming a routine (frame).
4. Why use Design patterns?
As programmers know a basic standard of good programs: High aggregation, low coupling. Object-oriented languages are much more complex than structured languages, and poor or poorly considered designs can lead to software redesign and development. However, in the actual design process, designers think more about how to solve the business problem, the internal structure of the software to consider less; the design pattern complements this shortcoming, it mainly considers how to reduce the dependencies between objects, reduce the coupling degree, make the system easier to expand and improve the reusability of the object. As a result, designers can optimize the structure within the system by using design patterns correctly.
Chapter II Summary of the brief
In the book design pattern, there are 23 models in total. Depending on the purpose, they are grouped into three categories:
Create type (creational): solves the problem of how to create an object.
Structural type (structural): solves the problem of how to combine classes or objects correctly.
Behavior type (behavioral): a problem that resolves how a class or object interacts and assigns responsibilities.
Tips: Abstract and specific (concrete) two words are often used in design patterns. The meaning of an abstract means that the class (method) It describes is an interface class (method), meaning that the class (method) it describes implements the corresponding abstract class (method).
Chapter III Abstract Factories (Abstracts factory)
1. Intent
Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
2. Classification
Create-mode.
3. What is the problem?
For those unfamiliar with this model, the word "Factory" is very strange, why use it? Then we'll run into another pattern, the factory approach (Factory method), so we'll explain what the factory means:
The house is made up of walls, doors, windows, floors, ceilings and pillars. If we write a house-building software for our clients, we will look at the walls, doors, windows, floors, ceilings and pillars as different classes: Wallclass, Doorclass, Windowclass, Ceilingclass, Pillarclass.
Now we create a new Class A, which has Createwall (), Createdoor (), Createfloor (), createceiling (), Createpillar () Five methods, The function of each method is to create and return the corresponding object. If the example of Wallclass, Doorclass, Windowclass, Ceilingclass, Floorclass, and Pillarclass is considered a product, then class A is like a factory producing these products. That's why we use the word factory.
Tips:a This name is too bad, if use housefactory will be better. In general, use a pattern in your system, preferably using the corresponding keyword as part of the class or method name, so that your partner or the system's code maintainer will understand what you are doing.
Our software is complete, the customer is very satisfied. However, our customers want to export the software, he found a problem, the software is too localized, built out of the Chinese-style housing. So he wants our software to build houses with different regional styles.
That's our problem! We need to redesign the original system, and it is impossible to finish the different architectural styles around the world at once. We will first complete some of the style (customer first to put the software of the country), and then add other ...
4. How to Solve
1 Establish an abstract factory (abstract Factory) class Housefactory, declared in this class:
Createwall ()
Createdoor ()
Createfloor ()
Createceiling ()
Createpillar ()
2 set up the corresponding abstract product class set:
Wall, Door, Floor, Ceiling, Pillar
3 Create the corresponding specific factory (concrete Factory) class for different styles (don't forget to implement the relationship), for example:
Chinahousefactory:housefactory
Greecehousefactory:housefactory
...
4 Create the corresponding specific product (concrete product) class for different styles (implement the corresponding abstract products), for example:
Chinawall:wall
Chinadoor:door
...
Greecewall:wall
Greecedoor:door
...
5. The topics
I think you know how to be flexible. Create and use a large pile of classes above:
Repeat the last two steps, you can easily add new style;
Use the abstract class declared in the previous two steps to implement the operation.
The focus of the abstract factory pattern is not the abstract factory class declared, but the series of abstract product classes it declares that we can use to manipulate specific product classes that we have implemented or have not yet implemented, and ensure consistency.
You may have found that the software could not build your two-storey villa because it has no stairs. To do this, we're going to define stair abstract classes and add Createstair abstract methods; The most important thing is that we have to add the corresponding stair classes in the 76 styles that have been completed, which is a big problem. Indeed, the ability of abstract factory models to adapt to new products is relatively weak. This is one of its drawbacks.
Fourth Chapter generator (Builder)
1. Intent
The construction of a complex object is separated from its representation, allowing the same build process to create different representations.
2. Classification
Create-mode.
3. What is the problem?
In the abstract factory chapter, we learned how to support a variety of housing styles in a global building system. The new problem is that users want to see the same structure, the appearance of different styles of housing. For example: The house has a floor, a ceiling, four walls, a door, a window. He wants to see the corresponding style of Chinese-style houses and Greek-style houses.
4. How to Solve
1 Create a generator (Buider) 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 of the building elements. and defines a method to return the constructed result gethouse.
2 Define a specific generator (concrete Builder) class for each style:
Chinahousebuilder:housebuilder
Greecehousebuilder:housebuilder
...
And to overload the methods of the parent class in these classes.
3 Also, the various types of housing categories:
Chinahouse
Greecehouse
...

5. How to use
We can use the above class to solve our problem in the following way.
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); Build a door on the WALL1
Builder.     Window (2); Build a window on the WALL2

Return builder. Gethouse ();
}
}
You can get different style results as long as you pass a different generator to the Housesystem.create method.
In fact, Housesystem is also a participant in the generator pattern, called the Guide (Director). Issues to be noted:
In the builder (housebuilder) class, you include the generation method for each component element. For example: staircase this element in some architectural style is not, in other styles have, also want to add Builderstair method in housebuilder.
The builder (Housebuilder) class is not an abstract class, and each of its build methods does nothing in general. In this way, the concrete generator does not have to consider the generation method of elements that are unrelated to it.

6. The topics
1 Maybe you have the idea that the generator pattern seems too complex to define the Createhouse method directly in the concrete 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)); Build a door on the WALL1
House.     ADD (New Window (2)); Build a window on the WALL2

return to house;
}
}
The generator pattern solves this problem with at least two classes: the Guide class and the concrete generator class.
So where is the generator pattern? The answer is responsibility assignment. The generator pattern makes a good allocation to the responsibility of creating a complex object. It puts the construction process into the guide's method, and puts the assembly process into the concrete generator class. Let's take a look at the instructions below.
2 Housesystem Class (the guide) can be very fine to construct house. And this generation process, for the product class (Chinahouse, Greecehouse ... and generator classes (Chinahousebuilder, greecehousebuilder) are not necessarily concerned. The concrete generator class considers the problem of assembly elements.
7. Flexibly
The generator pattern can be applied to the following issues:
Converts the system's document format to another format (the document in each format is equivalent to one product).
Compilation problems (the parser is the guide and the result is a product).


About the Author:
Yang Ning is the technical backbone of grapecity Company's overseas application development department. Engaged in many years of program development, has unix,windows platform development experience. For vb,c#, Vb.net,xml has a relatively rich understanding. Love to study OO programming, analysis, design, project management and other related technologies. Like to learn new technology.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.