The book of "Big Talk design mode"

Source: Internet
Author: User

The first time I read "Big talk design mode", it was just when I was in touch with C #. Tired of the official textbooks of the tome, inadvertently opened this lively and interesting book, is very bright. Because of the weak base of C # at that time, it is only when the novel to see, hungry, taste, a breath of four o'clock in the morning, was not aware of the passage of time to frighten silly.

Now reread, more is to revisit the design pattern of application scenarios and feel the passion for programming. While taking notes while reading books, initially understand the UML class diagram, the efficiency is much higher. Touched also a lot. Master lead the door, practice to see the individual AH.

For programmers, it's better to look at the wonderful code than to see the wonderful code. As a footballer (software programmer), being able to play in person and eventually become a star (software architect) is far more exciting than being a cheering fan (software user & code admirer).

So how can you come up with wonderful code? Experience makes people grow and experiences inspire. However, all the inspiration should be the cause, that is original aim's six object-oriented design principles, namely, the single responsibility principle, the open-closed principle, the dependency reversal principle, the Richter substitution principle, the Dimitri law and the synthesis--aggregation reuse principle. But these six principles are only a series of "slogans", the actual implementation of the need for detailed guidance, so 23 design patterns emerged. In the final analysis, it is through refactoring to improve the existing code, so that the coupling of the code is reduced, the final realization of easy maintenance, easy to expand, easy to reuse and flexible programming design, get wonderful code.

Programming is a technology, but also an art. Programmers who know only programming techniques are code workers, known as yards farmers; however, programming does not only refer to the mastery of programming techniques, but rather to stand at a higher level to appreciate program code, software design, architecture, and the transformation from yards to architects. So we need to learn design patterns. Design patterns are a summary of the software industry's experience and therefore have a wider range of adaptability, regardless of the programming language used, and regardless of the type of business encountered, design patterns are free to "intrusion".

Know that design patterns should be from a large number of actual combat experience, if not a large number of immediately put into practice the scene is not to understand the design pattern. Understanding the design pattern in combat will be my ultimate goal. At the beginning of the introduction, the initial goal is to have a preliminary understanding of design patterns.

The main purpose of writing this article is to record my learning status and experience at this time, in order to review later. Also cultivate a good habit of reading and taking notes. The following stories are the practice scenarios in "Big Talk design mode".

The following cut to the point: (6 design principles &23 Type design mode)

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

1. Single Responsibility Principle

Story: Mobile phone full-featured, but a critical moment on the "withered" off;

Concept: For a class, there should be only one cause of its change, reflecting the separation of duties of the class.

Tips: Like the MVC architecture, implement the separation of logic and interface.

2. Openness--the principle of closure

Story: Postgraduate Examination & Job hunting, Hong Kong, Macao, one country, two systems;

Concept: Is the core of all object-oriented principles. Software entities should be extensible and non-modifiable. That is, the extension is open, and the modification is closed.

Tips: It should only be abstracted from those parts of the program that are showing frequent changes, rejecting immature abstractions as important as the abstraction itself.

3. Dependency reversal principle

Story: The low threshold of computer repair, because the PC motherboard, CPU and memory for the interface design, but not for the implementation of design, low coupling, weak dependence, easy maintenance, and radio too strong coupling development, difficult to troubleshoot, difficult to repair;

Concept: High-level modules should not rely on low-layer modules, both should rely on their abstraction; abstractions should not depend on details, and details should be based on abstraction;

Tips: (1) should be for the interface programming, do not implement programming, because the implementation to design memory, it is necessary to correspond to a specific brand of the motherboard, which will appear in exchange for memory needs to replace the motherboard also embarrassed;

(2) The principle of dependency reversal is a sign of object-oriented design. If the writing is to consider the abstract programming is object-oriented design, on the contrary, the details of programming is the process of design;

4. The principle of substitution on the Richter scale

Story: Inheritance relationships, such as birds & penguins;

Concept: In software, the parent class is replaced by its subclass, and the behavior of the program does not change. Subtypes must be able to replace their parent type, because the substitution of subtypes makes it possible to extend a module that uses a parent type without modification;

Tips: The principle of substitution on the Richter scale is a supplement to the open-closed principle. The key step to achieving an open-closed principle is abstraction. The inheritance of the base class and subclass is the concrete implementation of abstraction, so the principle of the substitution of the Richter scale is the specification of the concrete steps to realize the abstraction.

5. Dimitri Law

Story: No acquaintance difficult to handle, mainly to deal with the issue of authority;

Concept: Also called least knowledge principle. The weaker the coupling between classes, the more beneficial it is to reuse, and a class that is in a weak coupling is modified without affecting the related classes. The main emphasis is on the loose coupling between classes;

Tips: In the design of a class, each class should minimize the access rights of members.

6. Synthesis--polymerization multiplexing principle

Story: Hardware manufacturers of hardware and software manufacturers to produce software for the synthesis relationship, mobile phone brand contains the mobile phone software for the aggregation relationship;

Concept: Synthetic aggregation is the "has a" relationship, and inheritance is the "is a" relationship. Since inheritance is a strongly coupled structure, the parent class changes and the subclass changes. So not "is a" relationship, we generally do not use inheritance. Preferential use of the synthetic aggregation multiplexing principle helps to preserve the encapsulation of each class and lower the hierarchy of inheritance.

Tips: Use composition/aggregation as much as possible, and do not use class inheritance as much as possible.

Two. 23 Design modes

A. Create pattern

1. Simple Factory mode

Story: the function design and realization of calculator;

Concept: Also known as the Static factory method mode, is determined by a factory object to create an instance of which product class;

Tips: Provides an interface to create a series of or related dependent objects without specifying their specific classes.

2. Builder mode

Story: Fried noodles did not put salt, Chi Giachi taste, and KFC McDonald's thousands blindly;

Concept: Separates the construction of a complex object from its representation so that the same build process can create different representations. The user only needs to specify the type of construction, without needing to know the process and details of the construction.

The objects of the Tips:builder interface are used to create complex objects in which the order of construction between the objects is usually stable, but the construction of the objects within the object is often subject to complex changes. The advantage is that it separates the construction code from the presentation code.

3. Factory Method Mode

Story: Learn Lei Feng, not the names, volunteers love action by legend.

Concept: Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.

4. Prototype mode

Concept: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes;

Tips: Mainly used for object replication, its core is the class diagram of the prototype class prototype. One of the benefits of using the prototype pattern is to simplify the creation of objects, making it as simple as copying and pasting as we edit the document.

5. Single-Case mode

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

Tips: Singleton mode lets the class itself be responsible for saving its only instance.

B. Structured mode

6. Adapter Mode

Story: Yao Ming needs to be translated in the NBA;

Concept: The interface of a class is adapted to what the user expects. Includes class adapter mode and object adapter mode;

Tips: It is mainly used in situations where you want to reuse some existing classes, but interfaces are inconsistent with the requirements of the reuse environment. Consider adaptation only if the original design and code cannot be changed;

7. Bridging mode

Story: Mobile phone brand is not unified, software is not compatible;

Concept: Separates the abstract part from its implementation, so that they can all change independently.

8. Combination Mode

Story: For large companies with distribution agencies to do Office management system;

Concept: Combines objects into a tree structure to represent a "part-to-whole" hierarchy. The combination mode makes the user consistent with the use of a single object and a combined object;

Tips: The use of conditions: when the requirements of the structure of the partial and overall hierarchy, and you want users can ignore the combination of objects and a single object, the uniform use of all objects in the composite structure;

9. Decoration mode

Story: What clothes to wear to see a girlfriend;

Concept: The implementation of each adornment object and how to use this object to split away;

Tips: The best thing to do is to effectively separate the core functions of the class from the decorative functions, and to remove the repetitive decorative logic in the related classes.

10. Appearance mode

Story: The risk of stock investment, many investors and many stock links too much, but not to operate, but the fund is the fund managers and thousands of shares and other investment products deal with less risk;

Concept: 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;

Tips: (1) in the early stages of design, the different two layers should be consciously separated; (2) in the development phase, subsystems are often more and more complex because of the continuous reconfiguration evolution. If the addition of façade can provide a simple interface to reduce the dependence between them, (3) when maintaining a large legacy system, it may be very difficult to maintain and expand the system, at this time, the new system can be developed a façade class, To provide interfaces that are poorly designed or highly complex and legacy code, allowing the new system to interact with façade objects, façade all complex work with legacy code;

11. Hengyuan Mode

Concept: Efficiently support a large number of fine-grained objects in a shared manner;

TIPS:1) Enjoy the meta-mode to make the system more complex. In order to make the object shareable, some states need to be externally, which complicates the logic of the program;

12. Proxy mode

Story: Cho Jayi through Dai send jiao jiao things, results Dai and jiao jiao love;

Concept: A degree of indirection is introduced when accessing objects because of the indirection that can be attached to multiple uses.

Tips: Agent mode includes: Remote agent, virtual agent, security agent and intelligent guidance;

C. Behavioral patterns

13. Observer mode

story: To go to the stock, front desk Secretary Tip; Request foreground class and observer two-way coupling;

Concept: Defines a one-to-many relationship that allows multiple observer objects to listen to a Subject object at the same time;

Tips: When you don't know how many objects to change, you should consider using the observer pattern. Actually in decoupling, let the coupling both rely on abstraction, rather than rely on the specific;

14. Template Method Mode

Story: Can not read English test questions, will do nothing;

Concept:: Defines the skeleton of an algorithm in one method, and delays some implementation steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without changing the structure of the algorithm;

15. Command mode

Story: Guerrilla the stalls of kebab skewers the queue of requests or the logging of request logs and the support of revocable operations are, in fact, tightly coupled between the "behavior requestor" and the "Act-implementing person"; The shop that makes the barbecue instead;

Concept: Encapsulates a request with an object, allowing you to parameterize the client with different requests, queue requests or log requests, and support revocable operations;

Tips: Principles of agile development;

16. State mode

Story: Different time people's mental state is different;

Concept: When an object's internal state changes to allow its behavior to change, the object looks like it has changed its class.

Tips: The state mode primarily solves situations where conditional expressions that control the state of an object are too complex. It is possible to simplify the complex judgment logic by transferring the judgment logic of State to a series of classes representing different states. The advantage is that the behavior related to a particular state is localized, and the behavior of different states is separated;

17. Responsibility Chain Model

Story: Design a pay code;

Concept: Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and the recipient of the request. Link the object to a chain and pass the request along the chain until an object is processed by him.

18. Interpreter Mode

Story: Understand the subtext behind the boss's comments;

Concept: If a particular type of problem occurs at a sufficiently high frequency, it may be worthwhile to describe each instance of the problem as a sentence in a simple language;

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

19. Broker Mode

Story: Mediation organizations such as the Security Council & the United Nations;

Concept: Also called mediator mode, defines a mediation object to encapsulate the interaction between series objects. The mediator makes the objects not need to be referenced to each other, so that they are loosely coupled and can change their interactions independently.

20. Visitor Mode

Similarities and differences of Story:man & woman;

Concept: Represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the individual element classes.

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

21. Policy mode

Story: the promotion of the sale of various discounts to achieve;

Concept: Defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other, allowing the algorithm to be independent of the customers who use it;

Tips: The advantages are: simplified unit testing;

22. Memo Mode

Story: Game storage progress;

Concept: Captures the internal state of an object without breaking the closure, and saves the state outside of the object. The object can then be restored to its previously saved state.

Tips: Involving roles: Promoters (originator), memos (Memento), and managers (caretaker);

23. Iterator Mode

Story: The conductor to iterate through the ticket, do not let slip through;

Concept: Provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation;

Tips: Separating the traversal behavior of a collection object, abstracting an iterator class to be responsible for not exposing the internal structure of the collection, but also allows multiple code to transparently access the data inside the collection;

Life is still going on, and programming will continue.

To be Continued ...

The book of "Big Talk design mode"

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.