Food reading-big talk design model-big talk Design Model

Source: Internet
Author: User

Food reading-big talk design model-big talk Design Model

Although I had three years of development experience, I still had a dish. Under the guidance of laruence, I began to enter the software path.

First, laruence recommends the first book, "big talk design model". Taking notes while reading a book, the book draws 6 design principles and 23 design patterns based on stories.

Experience makes people grow and experience inspires them. However, all the inspiration should have its own cause, that is, the six object-oriented design principles that keep changing, that is, the principle of single responsibility, the principle of openness-the principle of closure, the principle of dependency reversal, the principle of Rishi replacement, the principle of dimit and synthesis-the principle of aggregation reuse. However, these six principles are just a series of "slogans" and detailed guidance methods are required for implementation. Therefore, 23 design models emerged. In the final analysis, it is to improve existing code through reconstruction, reduce the Coupling Degree of code, and finally implement programming design that is easy to maintain, easy to expand, easy to reuse, and flexible, and get wonderful code.

The main purpose of this article is to record my learning status and experiences at this time so that I can review them later. At the same time, it also provides guidance for our friends in the software industry.

I. Six design principles (core: strong cohesion and loose coupling)

1. Single Responsibility Principle

Story: the mobile phone has complete functions, but it will be "wilting" at critical moments;

Concept: For the first type, there should be only one reason for its change, reflecting the separation of duties of the class.

Tips: such as the mvc Architecture, which separates the logic from the interface.

2. Open-Close Principle

Story: Master of postgraduate entrance exams and job search, Hong Kong, Macao, one country, two systems;

Concept: the core of all object-oriented principles. The software entity should be extensible and unchangeable. That is, it is open to the extension and closed to the modification.

Tips: only those parts that present frequent changes in the program should be abstracted. Rejecting immature abstraction is as important as the abstraction itself.

3. Dependency reversal Principle

Story: the threshold for computer repair is low, because the main board, CPU and memory of the PC are designed for interfaces, rather than for implementation. The coupling is low, the dependency is weak, and the service is easy to maintain; excessive strong coupling of Radio development is difficult to troubleshoot and difficult to maintain;

Concept: High-Level modules should not depend on low-level modules, both of which should depend on their abstraction; abstraction should not depend on details, and details should depend on abstraction;

Tips: (1) interface programming should be performed rather than implementation. Because Memory Design for implementation should correspond to the motherboard of a specific brand, that would lead to the embarrassment of changing the memory and the motherboard;

(2) The dependency reversal principle is actually a sign of object-oriented design. If the programming is based on abstract programming, that is, object-oriented design; otherwise, detailed programming is a procedural design;

4. Lee's replacement principle

Story: inheritance relationships, such as birds and penguins;

Concept: in the software, replace the parent class with its subclass, and the behavior of the program remains unchanged. Child types must be able to replace their parent types. It is because the child types can be replaced that the modules using the parent type can be expanded without modification;

Tips: The Li's replacement principle is a supplement to the open-closed principle. The key step for implementing the open-closed principle is abstraction. The inheritance relationship between the base class and sub-classes is the specific implementation of abstraction. Therefore, the Li's replacement principle is the standard for the specific steps to implement abstraction.

5. dimit's Law

Story: No acquaintances are difficult to handle, mainly dealing with permissions;

Concept: Also known as the minimum knowledge principle. The weaker the coupling between classes, the more conducive it is to reuse. A class with weak coupling is modified and will not affect related classes. It mainly emphasizes the loose coupling between classes;

Tips: in the design of classes, each class should minimize the access permissions of members.

6. Synthesis-Principles of aggregation Reuse

Story: The hardware produced by the hardware manufacturer and the software produced by the software manufacturer are in a synthetic relationship; the mobile phone software included in the mobile phone brand is in an aggregation relationship;

Concept: Composite aggregation is the relationship of "has a", while inheritance is the relationship of "is. Because inheritance is a strongly coupled structure, the Child class will change as the parent class changes. Therefore, it is not an "is a" relationship. We generally do not use inheritance. The principle of merging aggregation reuse is preferred, which helps maintain the encapsulation of each class and reduce the hierarchy of inheritance.

Tips: Use synthesis/aggregation whenever possible, and do not use class inheritance whenever possible.

Ii. 23 design modes

A. Create Mode

1. Simple factory Model

Story: Design and Implementation of calculator functions;

Concept: it is also called the static factory method mode. It is determined by a factory object to create a product instance;

Tips: provides an interface for creating a series of or related dependent objects without specifying their specific classes.

2. Builder Mode

Story;

Concept: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. Users only need to specify the construction type, without knowing the specific construction process and details.

Tips: Builder interface objects are used to create complex objects. The building sequence of these objects is usually stable, but the building of objects is usually subject to complex changes. The advantage is to separate the Construction Code from the representation code.

3. Factory method mode

Story: learn from Lei Feng, do not leave a name, volunteers love action to pass on.

Concept: defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to its subclass.

4. prototype mode

Concept: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object;

Tips: it is mainly used for copying objects. Its core is Prototype in class diagrams. One advantage of using the prototype mode is to simplify object creation, making object creation as simple as copying and pasting during document editing.

5. Singleton Mode

Concept: ensures that a class has only one instance and provides a global access point to access it.

Tips: The Singleton mode allows the class itself to store its unique instances.

B. Structural Mode

6. Adapter Mode

Story: Yao Ming needs translation in the NBA;

Concept: adapt a class interface to what you expect. Including the class adapter mode and Object Adapter mode;

Tips: it is mainly used to reuse some existing classes, but the interface is inconsistent with the requirements of the Reuse Environment. Adaptation can be considered only when the original design and Code cannot be changed;

7. Bridging Mode

Story: mobile phone brands are not uniform, and software is incompatible;

Concept: separates abstract parts from their implementations so that they can all change independently.

8. Combination Mode

Story: An office management system for large companies with distribution agencies;

Concept: combines objects into a tree structure to represent the "part-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object;

Tips: The usage condition is: when the requirement reflects the structure of the part and the overall level, and you want the user to ignore the differences between the combined object and a single object, when all objects in the composite structure are used uniformly;

9. decoration Mode

Story: clothes to see my girlfriend;

Concept: the implementation of each decoration object and how to use this object to split away;

Tips: The biggest benefit of doing so is to effectively separate the core responsibilities of the class from the decorative functional area, and remove repeated decorative logic in the relevant class.

10. Appearance Mode

Story: stock investment is highly risky. Many investors have too many relationships with many stocks, but it is not conducive to operation. fund managers deal with thousands of stocks and other investment products, with lower risks;

Concept: provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use;

Tips: Usage: (1) in the initial stage of design, we should consciously separate the two layers; (2) in the development stage, subsystems tend to become more and more complex due to continuous reconstruction and evolution. If Fa C ade is added, a simple interface can be provided to reduce dependencies between them. (3) when maintaining a legacy large system, this system may be very difficult to maintain and expand. In this case, you can develop a Fa C ade class for the new system to provide interfaces with rough or highly complex designs and clear legacy code, let the new system interact with the Fa C ade object, and Fa C ade interacts with the legacy code in all the complicated work;

11. hengyuan Model

Concept: supports a large number of fine-grained objects efficiently in a shared manner;

Tips: 1) the metadata mode makes the system more complex. In order for objects to be shared, some States need to be externalized, which complicate the logic of the program;

12. Proxy Mode

Story: Zhuo Jia Yi sent Jiaojiao through Dai Li. As a result, Dai Li fell in love with Jiaojiao;

Concept: introduce a certain degree of indirect property when accessing an object, because this indirect property can be used for multiple purposes.

Tips: the proxy mode includes remote proxy, virtual proxy, Security Proxy, and smart guidance;

C. Behavior Model

13. Observer Mode

Story: Job stock trading, front-end Secretary ventilation report; requires two-way coupling between front-end class and observer;

Concept: defines a one-to-many relationship, allowing multiple observer objects to listen to a topic object at the same time;

Tips: when you do not know how many objects need to be changed, you should consider using the observer mode. In reality, coupling is being implemented, so that both parties are dependent on abstraction, rather than on specifics;

14. template method mode

Story: do not understand the English questions;

Concept: defines the skeleton of an algorithm in a method, and delays some implementation steps to the subclass. The template method allows subclass to redefine some steps in the algorithm without changing the algorithm structure;

15. Command mode

Story: the archive port of a guerrilla kebabs queues requests or records request logs and supports revocation. In fact, it is a tight coupling between the "behavior requestor" and "behavior implementer; opening stores for barbecue;

Concept: encapsulate a request into an object so that you can parameterize the customer with different requests, queue the request or record the request log, and support unrecoverable operations;

Tips: Agile development principles;

16. Status Mode

Story: The mental states of people vary with time;

Concept: allows an object to change its behavior when its internal state changes. This object looks like it has changed its class.

Tips: the State mode mainly solves the problem when the conditional expressions that control the state of an object are too complex. Transferring the status judgment logic to a series of classes that indicate different states can simplify the complicated judgment logic. The advantage is that behavior localization related to a specific State is separated;

17. Responsibility Chain Model

Story: design the salary increase code;

Concept: Enables multiple objects to process requests to avoid coupling between request senders and recipients. Connect the object to a chain and pass the request along the chain until an object processes it.

18. interpreter Mode

Story: Understand the hidden lines behind the comment of the boss;

Concept: if a specific type of problem occurs frequently enough, it may be worthwhile to express each instance of the problem as a sentence in a simple language;

Tips: You can build an interpreter to solve the problem by interpreting these sentences;

19. intermediary Mode

Story: Council, United Nations, and other mediation organizations;

Concept: it is also called the mediator mode. It defines an intermediary object to encapsulate the interaction between series objects. The intermediary allows each object to reference each other without being displayed, so that the coupling is loose and the interaction between them can be changed independently.

20. Visitor Mode

Story: Similarities and Differences between man and woman;

Concept: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the element classes.

Tips: The purpose is to separate processing from the data structure;

21. Rule Mode

Story: the realization of various discounts during mall promotions;

Concept: defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other, so that the algorithms are independent of customers who use them;

Tips: Advantages: Simplified unit testing;

22. Memorandum Mode

Story: Game storage progress;

Concept: capture the internal state of an object without damaging the closed state, and save the state outside the object. In this way, the object can be restored to the previously saved state.

Tips: involved roles: Originator, Memento, and Caretaker );

23. iterator Mode

Story;

Concept: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object;

Tips: separates the traversal behavior of the set object and abstracts an iterator class to take charge of it, so that the internal structure of the set is not exposed, it also allows multiple codes to transparently access data in the collection;

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.