This is my first design mode, because I feel that the amount of personal code is not much, has not seen, and now only understand the main, usually a little attention, after a year or two to further detailed study. Due to my carelessness, written in Word without saving me on the reload system ... Only the lower half is recorded here.
"The design pattern of Zen" This book, is still relatively easy to understand, this strong type of Java language, the emphasis on encapsulation and inheritance, to explain the design model is more appropriate. The introduction of the example for beginners is still very good, but after the explanation of the text is a bit on the surface. I'll post the notes below.
Note # ornament Mode (Decorator pattern) dynamically adds responsibility to an object, and the adornment mode is more flexible than the subclass in which it is added. Decoration class is the sibling class (all inherit from the same original, need to be modified by the abstract class) A modification or supplement that can have multiple Concretedecorator class each specifically responsible for a class of modifiers, the modified concrete primitive class as the input of the next class, the need to have a private property to represent the object to be decorated, while explicitly calling the parent class method, layer decoration, Layers call the parent class method, and the entire decoration chain works. Features:
- The decoration class and the adornment class are decoupled from each other, so it is very easy to dynamically extend the decorator class.
- The real performer is also the specific modified class, the special proxy mode;
- It is easy to agree with the expansion of decorative class, need careful treatment.
#策略模式 (Strategy Pattern) defines a set of algorithms and encapsulates them so that they can be interchanged. Encapsulating each algorithm, or strategy, and shielding the high-level modules (avoiding display calls in the scene) directly, such as calculators, you can encapsulate the subtraction method in each class, Called in the context class, which is the portal to the calling policy. is the same as the proxy mode, but the difference is that the wrapper role and the encapsulated role of the policy mode
not the same interface (??? How to understand interface here.) #适配器模式 (Adapter Pattern) transforms the interface of one class into another interface that the client expects, so that the two classes that would otherwise not work together because of an interface mismatch are working together. is divided into object adapters and class adapters, The former is implemented through object Federation (referencing other objects), which provides a way to access the contents of a container by inheriting . #迭代器模型 (Iterator Pattern) it. Without exposing the details inside the container. Personal understanding is to convert foreach into an OOP programming, in a concrete class, a container as a constructor parameter, an initialization iterator, an iterator traversal container . #组合模式 (Composite Pattern) Combine objects into a tree structure to represent a "partial-whole" hierarchy, making the user consistent with the use of a single object or composition object. Personal Understanding: The original parallel objects, can be abstracted into a tree structure, so that these objects can extract the abstract class, with the same interface to deal with many objects. is divided into transparent ( The combination of the methods used in the abstract) and opaque, the former can reduce the type conversion (generic?). How?) #观察者模式 (Observer Pattern) define a one-to-many dependency between object so this when one object changes s Tate,all its dependents is notified and updated automatically. In the observer, the definition has its own state listener, once the state changes the call listener, notify its processing, if you need to return the results, May cause the entire processing chain to be too long, can be asynchronous or cached (pre-opened resources) #门面模式 (facade Pattern) Requires that the external communication of a subsystem be done through a unified object. The façade mode provides a high-level excuse for making the subsystem easy to use. Personal understanding: The internal logic of the encapsulation subsystem, understood as the API to invoke the system, note that the interface does not contain the business logic of the subsystem, Otherwise, it is unreasonable to rely on the subsystem to the interface, to ensure that the subsystem is completely hidden from the outside . #备忘录模式 (Memento Pattern) without violating encapsulation, Capture and externalize An object ' s internal State so, the object can is restored to the later. In layman's words, save the States of an object for recovery. Separate memo classes (even save multiple states, clone or Traverse objects, put in a map, specify K EY to differentiate them), Memo management class. The core of this pattern is the need for a map or array. #访问者模式 (Visitor pattern) that holds the previous state, or some of the operations that are used in the data structure, in the form of a save-in-time It can define new operations without altering existing data structures. A visitor class, a visitor class, the key point is that the visitor has an excuse to set the visitor, within the interface: Visitor.visit (this); Visitor is the parameter of the interface, so that it can be accessed within the visitor's internal structure, so this is also a disadvantage of the visitor pattern: The visitor knows the internal structure of the visitor. The iterator pattern can only access objects of the same type or interface, and the visitor pattern may traverse different objects. This is also one of the important uses of the visitor pattern. An effective complement to the iterator pattern . #状态模式 (state pattern) allows its behavior to change when the states within an object change, This object seems to have changed its class. An object can be converted between different states to change its action, without using design patterns, a large heap of if/else or switch/case statements are required to determine the next action based on the current state. Now encapsulate all States into classes, inheriting from the same interface, Overrides the actions that can be performed in each state, which replaces the state judgment statement. The execution of the action is given to the context class, and each state class has a protected property _context = new Context (), when a state performs an action, a new setting is set through _context, _ Context.state.handle (), the real performer is inside the context class. state mode change program structure, eliminate If/switch statement, maintainability improvement. Well-encapsulated. Dynamic extended State class; Easy to cause state class expansion. # The interpreter pattern (interpreter pattern) Given a language, defines a representation of its grammar, and defines an interpreter that uses that representation to interpret examples in the language. For example, string calculations (with stacks) #享元模式 ( Flyweight Pattern) use sharing to support large number of fine-grained objects effectively. Takes a large number of objects, extracts the common attributes, with attributes as key (the combination of multiple attributes), the object as a value, placed in the pool, the additional attributes are assigned. It is important to note that the objects in the pool are reused, That is, in high concurrency situations, different requests are likely to have access to the same object, causing the data to be tampered with. The number of objects that need to be generated to support the number of businesses, depending on the situation . #桥梁模式 (Bridge Pattern) decoupling abstraction from reality, So that the two can change independently. How is structure abstract and reality? How does abstraction and reality understand? I don ' t know. Abstract role reference implementation role, specific actions have to implement the role to do .
Design pattern in the lower part of Zen notes