Design Mode (knowledge point 2)

Source: Internet
Author: User

1. many variants of the template method pattern are seen in the real-world code, so don't expect them to be recognized at a glance.

2. Both the policy mode and the template method mode encapsulate the algorithm. One is a combination and the other is an inheritance.

3. The factory method is a special version of the template method.

4. Cohesion, which is used to measure a class or template to achieve a single purpose or responsibility. When a template or class is designed to support only one set of related functions, we say it has a high cohesion. If it is designed to support a set of unrelated functions, we say it has low cohesion.

Cohesion is a more common concept than the single responsibility principle, but the two are closely related. classes that follow this principle may have a high cohesion, it is easier to maintain than low-level clustering with many responsibilities.

5. think of the Rule mode as an elastic alternative to inheritance. If you use inheritance to define the behavior of a class, you will be trapped by this behavior, or even have to modify it. With the policy mode, you can combine different objects to change behavior.

6. the State mode is an alternative solution that does not need to place many condition judgments in the context. By encapsulating the behavior into the State object, you can change the context behavior by simply changing the State object in the context.

7. Factory mode: The Factory method is used to process object creation and encapsulate such behavior in sub-classes. In this way, the super class code in the client program is decoupled from the Code created for the subclass object.

8. Command mode: You can use the command mode to decouple the requested object from the requested object.

Receiver necessity (decoupling): In general, we try to design a "Dummies" command object that only knows the behavior of calling one receiver. However, many "smart" command objects implement a lot of logic and directly complete a request. As a result, the degree of decoupling between callers and receivers is not as good as that between "Dummies" command objects.

9. The intention of the adapter mode: "change" the interface meets the customer's expectations

10. Intention of appearance mode: provide a simplified sub-system interface

11. The decorator Can add his/her own actions before and after the actions of the entrusted decorator to achieve a specific purpose.

12. Design Principles

Find the changing aspects in the program and then separate them from the fixed ones.

In the observer mode, the state of the topic and the number and type of the observer are changed. In this mode, you can change objects dependent on the topic State without changing the topic. This is called Advance planning!

For interface programming, not for implementation programming

Both the topic and the observer use interfaces: The Observer uses the topic interface to register with the topic, and the topic uses the observer interface to notify the observer. In this way, the two can operate normally and have the advantage of loose coupling.

Multi-Purpose Combination and less inheritance

Observer mode uses "Combination" to combine many observers into the topic. This relationship between objects is not generated by inheritance, but by using combinations at runtime.

13. The combination mode is designed with a single responsibility in exchange for Transparency





Design inevitably requires compromise: the trade-off between abstraction and speed is balanced between space and time. [the code should be closed like the lotus in the sunset glow (without change ), open (Scalable) Like the lotus in the early morning]

Design Mode (knowledge point 2)

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.