A summary of the model of big talk design--creating pattern

Source: Internet
Author: User

Design Pattern Programmer's help is very big, is the weapon of the programming, is the promotion raise salary the weapon! For most beginners, the design pattern is a very abstract concept, and before you learn the design pattern, it is better to think that the code is error-free. But as you go further and farther along the path of the programmer, you are already unknowingly practicing design patterns. The design pattern conveys a message to the world that programming is more than just a technology, it is an art!

The design pattern can be divided into three categories: creation mode, structural mode, and behavioral mode.

What I want to tell you today is the creation pattern, which hides how instances of classes are created and put together, and what the whole system knows about these objects is an interface defined by an abstract class. In this way, creating a pattern provides great flexibility in what it creates, who creates it, how it is created, and how it is created.

Example of a simple calculator to tell the factory model

1. Simple Factory mode: use simple factory to instantiate the appropriate object, through polymorphism, return to the parent class way to achieve the results of the calculator.



Operationfactoty Factory class

<span style= "FONT-SIZE:12PX;" >public class Operationfactory () {public    static operation Createoperate (String operate) {        operation oper = null;        Switch (operate) {case            "+":            oper = new Operationadd ();            break;            Case "-":            oper = new Operationsub ();            break;            Case "*":            oper = new Operationmul ();            break;            Case "/":            oper = new Operationdiv ();            break;        }    }    Return oper}</span>

The biggest advantage of the simple factory model is that the factory class contains the necessary logical judgments, dynamically instantiating the related classes based on the client's selection criteria, and for the client, to remove the reliance on the specific product. But the problem in adding an algorithm, we need to add a "case" in the factory class branch conditions, modify the original class, which means that we are not only open to the expansion, but also open to changes, contrary to the open-closed principle . So we have a factory method model.

2. Factory Method Mode: Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.



The whole plant and product system has not changed, but only the change of the expansion, which fully conforms to the spirit of the open-closed principle.

The factory method pattern overcomes the shortcomings of the open-closed atom in a simple factory, while preserving the advantages of the encapsulation object creation process. They all encapsulate the creation of objects, it is necessary to replace objects, do not need to make large changes can be achieved, reducing the customer program and product object coupling. The factory method model is a further abstraction and generalization of the simple factory model.

Due to the polymorphism, the factory method pattern maintains the advantages of a simple factory model and overcomes its drawbacks. But the disadvantage is that because each additional product, it is necessary to add a product factory class, adding additional development volume.

Note: The design is usually started by the factory method, and when the designer discovers that more flexibility is needed, the design will think of his creation pattern evolution. When designers weigh the tradeoffs between design standards, understanding multiple creation patterns can give designers more room to choose.


3. Abstract Factory mode: Provides an interface to create a series of related or interdependent objects without specifying their specific class

In project development, the technique of repeated application is common, but the choice of database is often decided by the developer, but it does not exclude the requirement of the customer to specify the database. At this time the trouble of the database came, take SQL Server,acess, the two still have a certain difference, the connection statement, the limit of the key words, and so on, how to do to increase demand, modify the program, without affecting their links, do not think of the time problem repeatedly copy and other ideas, That will only increase the workload! And that's the elegant programming that the design pattern advocates!

The factory method implements the operation of the user table


But the database cannot have only one table, plus a departmental table.


Unconsciously, you have evolved through constant evolution. Reconstruct the abstract method pattern. When there is only one user class and the user operation class, only the factory method pattern is required, but now obviously there are many tables in the database, and SQL Server and access are two different classifications, so solving this problem involving multiple product families, There is a specialized factory model called Abstract Factory mode.

Of course, the single use of various modes is relatively weak, can be used together to achieve the art of programming. There is not much to say ~ ~ ~.


4. Prototype mode: Specify the kind of objects created with the prototype instance and wear a new object by copying the prototypes

The prototype pattern is actually creating another customizable object from one object without needing to know the details of any creation.


It is usually easier to create the appropriate number of prototypes and clone them than to manually instantiate the class each time with the appropriate state.


5. Singleton mode: Simple and beautiful, ensure that there is only one instance of a class. and provides a global access point to access it.

Usually we can have a global variable that makes an object accessible, but it doesn't prevent you from instantiating multiple objects. One of the best ways is to have the class itself responsible for preserving its only instance. This class guarantees that no other instance can be created, and it can provide a way to access the instance.


6. Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.

Let's talk about cohesion and coupling before we talk about the Builder model: cohesion describes how closely the internal components of a routine are interconnected. Two-coupling describes how closely a routine relates to other routines. The goal of software development should be to create such routines: internal integrity, which is high cohesion, and the connection to other routines is small, direct, visible, flexible, which is loosely coupled.

The builder pattern separates a complex object from his presentation. This makes it easy to change the internal representation of the product and to separate the construction code from the presentation code. In this way, for the customer, it does not need to care about the product creation process, but just tell us what we need, we can use the same build process to create different products to customers.

Create complex objects in which the internal build order is usually stable, but the building within the object often faces complex changes.

meaning: The creation Pattern abstracts the process of instantiation. They help a system to be independent of those objects that are created, composed, and represented. The Create pattern encapsulates the information about which specific classes the system uses. Allows customers to configure a system with a "product" object with very different structure and functionality. A configuration can be static, specified at compile time, or dynamic, or specified at run time.

simple record of the study, if there are problems can refer to a variety of design patterns Books ~ ~ ~

A summary of the model of big talk design--creating pattern

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.