Design Patterns Learning Notes

Source: Internet
Author: User

1. Programming a principle: To avoid repetition in the best possible way; 2. As long as you hear the need to apply different business rules at different times during the analysis process, you can consider the possibility of using a policy model to deal with this change; Policy Mode: Encapsulate different processing methods into specific classes, then abstract an upper class, and then create a method class that contains an abstract class object that initializes the abstract class object when the method class is initialized, and instantiates different types in a method in a method class by passing in different arguments. This will return different calculation results, the advantage is that the client class will only see the method class, will not involve the specific calculation class, the implementation of a good separation, reduce the coupling; 3. Class design to comply with single principle of responsibility, in the case of a class, there should be only one cause for it to change; 4. Open-closed principle: In the face of demand, changes to the program are made by adding code, rather than changing the existing code, which is the spirit of the principle; 5. Dependency Reversal Principle
    • High-level modules should not rely on the underlying modules. All two should rely on abstraction;
    • Abstraction should not depend on detail. Detail should depend on abstraction;
Dependent dumpIn fact, it can be said that the object-oriented design of the logo, in which language to write the program is not important, if the writing is to consider how to focus on abstract programming rather than the details of programming, that the program all the dependencies are terminated in the abstract class or interface, that is, object-oriented design, which is the process of design. 6. Proxy Mode: Provides a proxy for other objects to control access to this object; 7. Simple Factory mode: The instantiation is placed in the factory, reducing the coupling with the client code; 8. Factory mode: The factory class is abstracted out of an interface, the advantage is to increase the type of time without modifying the simple Factory mode factory, directly add a factory class on it, so that the open-closed principle; 9. prototype Mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes;

Template Method Pattern : Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm;

One. appearance mode : Provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes the subsystem easier to use;

12. Builder Mode: Separating the construction of a complex object from its representation so that the same build process can create different representations; 13. Observer Pattern: Defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves; Event Delegate: The delegate approach solves the deficiencies in the observer pattern because the corresponding events of the possible objects in the observer pattern may be different, so there is no need to save an array of observer objects in the Subject object, place a delegate object in the Subject object, and then register different corresponding methods for the delegate object, In this way, invoking the delegate object at the time of notification can call the specific methods of different objects in sequence, and accomplish the simultaneous response of different objects to the same event; 14. State mode: When an object's internal state changes to allow its behavior to change, the object looks like it has changed its class; The state pattern mainly solves the situation that when the conditional expression that controls an object state transition is too complex, the judgment logic of State is transferred to a series of classes representing different states, which simplifies the complex judgment logic. When considering the state pattern: When an object's behavior depends on its state, and it must change its behavior according to the state at run time, it can consider using the state mode; 15. Adapter Mode: The interface of a class is converted to another interface that the customer wants, the adapter mode makes the original interface incompatible and cannot work together those classes can work together, but can not often, at the beginning of the project or as far as possible to achieve the unity of the interface, as long as the real need to use this mode;

Memo Mode : captures the internal state of an object without compromising encapsulation, and saves the state outside of that object so that it can be restored to its previously saved state, and the benefit is to encapsulate the details of the state that needs to be saved in a different class. This will not destroy the client code when it is necessary to modify the saving state information, and close the implementation details;

17. Combination Mode: Combining objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns that allow users to have consistency with the use of individual objects and composite objects, and when discovering that a requirement is a structure that reflects part and whole hierarchies, and that the user can ignore the difference between a combined object and a single object. When you use all the objects in a composite structure uniformly, you should consider the combination pattern, such as the relationship between the company and the branch and subordinate parts. 18. single-case mode: Ensure that a class has only one instance, and provides a global access point to access it, declare a static type of this variable in the class, and then declare a static access to access the instance variable to determine whether the variable is defined and decide whether to define a new variable. However, to declare the constructor as private to prevent external creation of the object; a hungry man -A singleton is a static instance that is created when the class is loaded; Lazy TypeThe singleton is initialized only when it is applied for the first time, but it is necessary to establish double lock processing in multi-threaded application to ensure security. 19. synthesis/polymerization multiplexing principles: Use composition/aggregation as much as possible, do not use class inheritance, class inheritance will result in high coupling between subclass and parent class, limit flexibility and reusability, when using inheritance, we must consider using it in is-a relationship; Bridging Mode: The system may have a multi-angle classification, each classification is likely to change, then the multi-angle separation to allow them to change independently, reduce the coupling between them, such as mobile phone brand and mobile phone software, can be implemented separately, and then through the mobile phone has the software of the way the aggregation operation to form a different mobile phone, In this way, when adding a mobile phone brand or software function, just add the class, without destroying the previous class structure, so that the open-close principle is well guaranteed; 20. Command mode: Encapsulates a request as an object, allowing you to parameterize the client with different requests, queue requests or log requests, and support revocable operations; 21st. Responsibility Chain Model: Allows multiple objects to have the opportunity to process the request, thereby avoiding the coupling between the sender and receiver of the request, connecting the object to a chain, and passing the request along the chain until an object has processed it; For example, a request for leave, each specific auditor set up a superior, so as long as the requests are passed to the manager level , who will not care about the future, and finally get the results can be, but also flexible to change the relationship between the level, so that the number of branches in a class to determine the difficulty of maintenance, flexibility is poor; 22. Broker Mode: Using a Mediation object to encapsulate a series of object interactions, the mediator does not need to show the interaction between the objects, so that they are loosely coupled, and can independently change the interaction between them, such as the United Nations to deal with the problem between Iraq and the United States, without the need for two countries to communicate directly, Through the United Nations intermediary to exchange can be, reduce the coupling degree; 23. enjoy meta mode: The use of sharing technology to effectively support a large number of fine-grained objects; application scenario: If an application uses a large number of objects, and a large number of these objects cause a large storage overhead, you should consider using it, and there is the object's most state can be external state, if you delete the external state of the object, You can instead use a relatively small number of shared objects to replace many groups of objects, you can consider using the enjoy meta-mode, but at the same time this mode also increases the complexity of the code, so if not very many objects are not necessary to use, such as Weiqi game is suitable for such objects, because a game requires more than 300 objects, But in fact the pieces are black and white, then these are the internal state, and the position of the pawn is the external state, with this mode can only generate 2 objects, and then use the external state to complete the game, which greatly reduces the memory overhead;

Design Patterns Learning Notes

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.