Interpretation of the 23 GoF Design Patterns bell (condensed version)

Today saw a 23 clock design patterns book of Gof (PDF version).

This book "Design Patterns Explained -GoF 23 kinds of design patterns analysis" has been more streamlined.

This article is the equivalent of notes.


1.Factory mode

More than 1.1 subclassing integration

    This function is factory model can be integrated together to create multiple subclasses. So that the user need not be concerned to achieve created. You do not need to know them in the end is the use of different sub-classes to implement or use parameter markers to achieve.
    Another feature is the factory model can be used in different ways to create different objects names, the same function as hidden implementation.
    In fact this way is like an integrated package of code. Compare 2b argument is that this looks better. Of course, he could just really nice. In the course of subsequent use and maintenance, a factory mode will reduce the amount of work you N times, and less error prone.

1.1.1 For example

    There are N sub-type, without the use of factory and what subclass needs met, directly create this subclass. Depending on the logic it looks clear and simple.
    But once need to be updated, such as the seeds become N N / 2 species (We found two categories can actually be integrated together). This time If you create this type of M in place, you have to change the M times (not to mention full-text alternative approach, which may result in a greater risk. You thought he was working well, but he may be too Alternatively more, or alternatively the drain). The use of factory mode, you only need to change the factory. Even change the code number is just as much, factory model also has his advantages: you can know exactly where to change!

1.2 Creating delay

   factory mode can also create delay action, we will determine the parameters of certain parameters when creating the factory. Then created when needed. This can hide specific initialization action before this. You do not need to know before you create what are initializing operation.

1.2.1 For example

   If you want to summon a summoned creature, when it is created, you just want to care about his lifetime, not on the call of a pig or eagle. We will need to start a factory in saving download directly create and pass parameters required when needed.
  Of course, you can put all related to this summons parameters are saved, and then create when needed. If we optimized this way, a package of these parameters to the object. In fact, this is a comparison of the original factory. Some may just put the factory on initialization determined before the earlier stuff. This avoids every repeat its actions (for example, each time by the respective subclass type switch).

2 AbstactFactory mode

    AbstactFactory Factory mode FactoryObject more like an application. But usually only in the factory for the creation of a class, and AbstactFactory created many types. So will AbstactFactory as a new model is somewhat redundant, regarded as a kind of factory upgrade

3.Singleton mode

    The core idea of ​​singleton class is only one object. In addition to all the problems are not a problem.
    For example, I can use the class type of static member variables, global variables can not be used, can not let all the variables and static methods of the class have become used to fulfill the demand.
    can!
    In fact, Singleton is the use of static member variables of the way to achieve.
    As long as you feel the way you achieve better use and implement the concept of Singleton pattern. It is to achieve a single-mode embodiment.
    Under normal circumstances we'll Factory and Singleton combined.
   In fact, as long as it satisfies: This class is only one object. It should be implemented using a single embodiment mode.

3.1 Advantages

    Singleton will have to solve the problem class is instantiated many times as possible radically (by the principle of a single case of damage, you can let the singleton instance multiple times, but this operation can not be accidentally generated).
    EXAMPLE each other when the single call, you do not need concerns initialization sequence (of course the issue cycle called single embodiment can resolve initialization, initialization is required singleton A single Example B, Example B single initialization indirectly require a single example A) .
    Singleton in achieving the more natural (more natural achieve more readily understood, and less error-prone).

4.Builder mode

    Builder pattern regarded as a very weak mode (just a little skill).
    Builder pattern reminds me of a cartoon-hour watch. Concrete is not clear, but there is a memorable scene.
    "Component legs and feet, the composition of the torso and arms, to make up my head ..."
    This is the meaning roughly Builder mode. An Object initialization divided into several parts. It may be at different times, different places, different people and even to achieve the initialization of a part of him. When all the parts are initialized, you can get up to this Object.

5.Prototype mode

    Class provides a way to copy itself. So that when you when a base class object Clone when he does not need to know the base class object is actually the object of that subclass.

6. Bridge mode

    The bridge mode together with a combination of multiple types. If we want to support the N kinds of platform, M kinds of realization. If the direct use subclass you will produce M * N sub-class. Of course, in the actual development process using the Bridge pattern is natural. When found no increase in the realization of a class is the need to increase N sub-class, you naturally think of using a new class, to integrate multi-layered change.

7.Adapter mode

    Interface package that many different implementations of the interfaces of the package into a unified interface. You can hide low-level calls. Used for cross-platform (thread mechanism different platforms are not the same) and third-party libraries (third-party calls the style and the style of the existing inconsistencies) call.

8.Decorator mode

    Decorator pattern that allows add decorators, a method for strengthening. For example, we encrypt a text off, we can use a variety of hungry decorator (encryption) to encrypt. Of course, when the need to decrypt the reverse order.

9.Composite mode

    Combined mode. Hidden structural features. You do not need to know that the node is a leaf node or a non-leaf node. For example, to send a person to wage a department or wages, or wages to a group. For the wages of people do not care to take the money of a person or department or team, he just wants to know how much money you want to send. As for the interior really what made him go on.

10.Flyweight mode

    Sharing mode, some of the common data will be used in multiple places as a shared Object. For example, there are many pieces of Christmas trees (inside the tree is exactly the same, very strange), we will put him in different places, corridor inside the room, out of the house. In different places with different decorative items. This time the description of the tree itself is the same, but his position is inconsistent and decorations.
    Have to say is, under normal circumstances, we also need an object to manage these shared Object (or you do not know come out to a new or use existing). Object and this is generally a single embodiment. At the same time the Manager may also be a factory.

11.Facade mode

    Appearance mode. The subsystem into a set of operations packaged API. (Lord knows will do ah, put forward a specially designed model, loaded to force home the equipment)

12.Proxy mode

    HttpProxy it should not be unfamiliar. In fact the agent is to hide the caller, while the reverse proxy is to hide the implementor. The more design mode Proxy mode is hidden implementers. So this place is called reverse proxy mode is better.

13.Template mode

    Template mode, the logic underlying implementation hidden. This model should be the most frequently used. std :: vector, std :: list, std :: map, std :: set. Even if not used certainly heard of it.

14.Strategy mode

    Strategy mode. The functional diversity of the encapsulated object. For assembly during running.

15.State mode

    State mode. The state machine does not know, do not use the realization of different states.

16.Observer mode

   OB mode, monitoring certain events, and make the appropriate treatment. Listener should be removed to allow the addition of dynamic living.

17.Memento mode

    Memo mode. This mode allows to save the current state of an Object down, and can be restored to its state stored by the Object. (This is a design pattern for granted, of course we want to go back at any time last operation, in order to achieve this function, in extreme cases, your memory resource consumption required * 2).

18.Mediator mode

    Intermediary model will be called and implement both hidden. For communication modules have very good results.

19.Command mode

    Command mode, the parameters and parameter Ming hidden.

20.Visitor mode

   Visitor pattern, the operation of a data Object encapsulated. So that when you update the operating mode and can be independent of the original data.

21.Chain of Responsibility pattern

    Chain of Responsibility pattern, a process event, or (in fact, may be further processed and transferred) to pass it to the subsequent node processing.

22.Iterator mode

    You can go with the use std :: list, std :: map, std :: vector, std :: set. Iterator iterator pattern is to realize the function.

23.Interpreter mode

    Interpreter pattern. Parsing script. Implement each interpreter, it is to invent a new scripting language (is not very Cock).

24 Postscript

    In fact, design patterns are just some programming skills. Not saying that we must use some design patterns. But when you are bothered by a problem or you expect some kind of trouble, you might think the solution will generally tend to the existing design patterns. So you can be trained to master these modes, when you encounter problems using the core idea of ​​these modes. Finally, to produce a solution, and a pattern or a substantially uniform, or a combination of several modes, or some of their own and then doped understood that the results are excellent. Design model is more a summary of previous experience, he may no longer be applicable to the present environment, but understand his principles maybe you will create a new model to give inspiration!
    
发布了54 篇原创文章 · 获赞 1 · 访问量 2万+

Guess you like

Origin blog.csdn.net/u011255131/article/details/78017114