[Design mode] Summary

Source: Internet
Author: User
Document directory
  • Single Responsibility Principle
  • Open and closed Principle
  • Rishi replacement principle
  • Dependency reversal Principle
  • Dimit Law
Introduction

Design Patterns are the cornerstone of software engineering. Only when proficient in the design model can you say that you really understand software engineering. It can be said that the design pattern is one of the essential skills of every architect.

The origin of the design pattern is the idea of object-oriented programming, and the essence of object-oriented design is abstract. Object-oriented abstraction is implemented through classes and objects, which produces three important object-oriented mechanisms: encapsulation, inheritance, and polymorphism. These three mechanisms are derived from a variety of design patterns.

In fact, the analysis and design of object-oriented systems are aimed at two points: High Cohesion and low coupling. This is what we are pursuing in software design. Therefore, whether it is encapsulation, inheritance, polymorphism, or the principles and instances of our design patterns in OO, we are working towards these two goals.

In the design and development of object-oriented systems, we have accumulated many principles, suchFor example, encapsulation inheritance and Polymorphism in object orientation, interface-oriented programming, combination rather than inheritance, and the idea of separating abstraction and implementation, etc.In the design model, you can always see their shadows, especially the differences between the combination (delegate) and inheritance bring about the difference in system coupling, but also involved in the design model multiple times.

The design pattern is an idea, while the thought is everything that guides the behavior, understanding and mastering the design pattern, not remembering 23 (or more) design scenarios and solutions (in fact, this is also a very important fortune). What we actually accept is the edification and baptism of an idea. After this idea is integrated into your mind, you will unconsciously use this idea for your design and development. This is the most important thing.

Principle single Responsibility Principle

For a class, there should be only one reason for its change. If a class has too many responsibilities, it is equivalent to coupling these responsibilities. A change in responsibilities may weaken or restrain the class from fulfilling other responsibilities. This coupling will lead to a fragile design, and the design will be unexpectedly damaged when changes occur. If you can think of more than one motive to change a class, this class has more than one responsibility.

Open and closed Principle

Software entities can be expanded, but cannot be modified. That is, it is open for expansion and closed for modification. In the face of requirements, changes to the program are done by Adding Code, rather than modifying the existing code. When a change occurs, we create an abstraction to isolate similar changes in the future. The principle of open and closed is the core of object orientation. Open personnel should Abstract The part of the program that shows frequent changes, and refuse to deliberately abstract and immature the part.

Rishi replacement principle

If a software entity uses a parent class, its subclass must be applied. In addition, it cannot detect the differences between parent class objects and Child class objects. That is to say, in the software, replacing the parent class with a subclass does not change the behavior of the program. Child types must be able to replace their parent types.

Dependency reversal Principle

Abstraction should not depend on details. Details should depend on abstraction. That is, do not implement programming for the interface. High-level modules cannot depend on low-level modules. Both of them must rely on abstraction. The dependency reversal principle is the object-oriented standard.

Dimit Law

If the two classes do not communicate directly, the two classes should not interact directly. If a class needs to call a method of another class, it can forward the call through the third class. In the structure design of sub-classes, each class should minimize the access permissions of members. This rule is strongly embodied in the adapter mode and interpretation mode.

23 design modes

Category

Description

Creation Mode

Factory (Factory mode): used for a derived class Object (product) of the same base class ). Use a factory derived class to create a product derived class. They have a one-to-one relationship.

Abstract Factory (Abstract Factory mode): used to create a derived class Object (product) of different base classes ).

Singleton (Singleton mode): ensures that a class has only one instance and provides a global access point to it.

Prototype: Use a Prototype instance to specify the type of the object to be created, and copy the Prototype to create a new object.

Builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Structural Mode

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

Adapter mode: converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

Decorator: dynamically adds some additional responsibilities to an object. The extended function is more flexible than the subclass generation method.

Composite (combination mode): combines objects into a tree structure to represent a "part-whole" hierarchy. It makes the customer's use of a single object and composite object consistent.

Flyweight: uses the sharing technology to effectively support a large number of fine-grained objects.

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

Proxy: provides a Proxy for other objects to control access to this object.

Behavior mode

Template: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Template allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

Visitor (Visitor mode): 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 classes of each element.

Strategy: defines a series of algorithms, encapsulates them one by one, and makes them replaceable. This mode makes the algorithm changes independent of the customers who use it.

State: allows an object to change its behavior when its internal State changes. The object seems to have modified its class.

Observer (Observer mode): defines a one-to-many dependency between objects, so that when the state of an object changes, all objects dependent on it are notified and automatically refreshed.

Memento: captures the internal state of an object without compromising encapsulation, and saves the State outside the object. In this way, the object can be restored to the Saved state.

Command (Command mode): encapsulate a request as an object, so that you can parameterize the customer with different requests; queue requests or record request logs, and supports cancelable operations.

Mediator: A Mediator object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

Chain of Responsibility (responsible Chain mode): to cancel the coupling between the request sender and receiver, multiple objects have the opportunity to process the request. Connect these objects into a chain and pass the request along the chain until an object processes it.

Interpreter (Interpreter mode): defines a language, a syntax expression, and an Interpreter that uses the Interpreter to interpret sentences in a language.

Iterator (Iterator mode): provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

  

Creation Mode:The instantiation process is abstracted. They help a system to create, combine, manage, and represent its objects independently.In essence, all creation modes encapsulate the object creation process.

Creation modes are dividedObject creation mode and class Creation Mode.The so-called object creation model is to delegate the instantiation work to another object.

Class creation model is a class that is instantiated by inheritance. The class creation mode has two important features: ① the client side does not know the specific class implemented by the function (unless you look at the source code) ② hides how class instances are created and put together. These two important features are achieved through the Virtual Interface Technology of abstract classes, so that designers can decide when and where to create and who to create. Follow object creation.The createtype class mode delays part of object creation to subclass, while the createtype Object Mode delays it to another object.

The creation mode mainly focuses on the object creation process and encapsulates the object creation process. The client can directly obtain the object without worrying about how to create the object.

  • Singleton mode: used to obtain a unique object in memory
  • Factory mode: used to create complex objects
  • Abstract Factory mode: used to create a group of complex objects that are related or mutually dependent.
  • Builder mode: used to create modular, more complex objects
  • Prototype: used to obtain a copy of an object

Structural Mode:As the name impliesThe structure of classes and objects is discussed,It uses an Inheritance Mechanism to combine interfaces or implementations (Class-structured mode), or by combining some objects to implement new functions (Object-structured mode ).

These structural models have great similarity in some aspects, but they have different focuses. The purpose is to coordinate the entire process and minimize coupling to adapt to future changes. Solve the coupling problem between modules from the program structure. How to organize the organizational relationships between the organization classes or objects to form a large structure mainly uses inheritance to organize interfaces or implementations.

The structure class mode uses the Inheritance Mechanism to combine classes, while the structure Object Mode describes the object assembly method.

Behavior mode:Describes how classes or objects interact and how responsibilities are assigned. Abstract A behavior to make the entire process easier. Interaction and Responsibility Assignment between categories or objects (that is, what is used ).

The behavior class mode uses inheritance to describe the algorithm and control flow, while the behavior Object Mode describes how a group of objects collaborate to complete tasks that a single object cannot complete.

Both the creation mode and the structure mode emphasize the relationship between static class objects. The behavior mode focuses on the communication relationship between class objects.

Relationships between classes

There are many types of Inter-class relationships, which can be divided into two types:Vertical and horizontal. A vertical link is an inherited link. The horizontal relationship follows the UMLThe recommendations can be roughly divided into four types:

UML notation

Link

Dependency)

Dotted line + arrow (-->)

... Use a... use (as a parameter in the function) is contingent. As shown in the code, Class B is used by Class A in A method as A parameter; local variable.

One-way dependency (--->)

Association)

Solid line + arrow (→)

... Has a... Yes. It is embodied in the member variables and parameters. If one-way Association of Class A points to Class B, Class A has A property B.

One-way Association (→), two-way Association (-), and self-Association

The difference between dependency and association: one is to use, the other is to own; the association can be bidirectional, and the dependency must be unidirectional.

Aggregation)

Hollow diamond + solid line + arrow (◇ →)

... Has a... Yes. Aggregation is a special case of association. It reflects the relationship between the whole and the part, that is, the relationship between has-. At this time, the whole and parts can be separated, and they have their own lifecycles. Class A {...} class B {A * ;.....}

When there is a general-partial relationship between classes, they must be aggregated and combined.

Composition)

Solid diamond + solid line + arrow (◆ →)

... Is a part of..., a combination is a special case of association. The whole and part are inseparable. They have the same lifecycle, and the combination class is responsible for the combination class. Class A {...} class B {A ;...}. Inheritance is a kind of); combine part a part)

Generalization)

Also known as generalized relationships

Solid line + empty triangle (-△)

... A kind of... is a kind. Subclass inherits the parent class

Implementation)

Dotted line + empty triangle (---△)

Subclass implementation abstract class

Their strong/weak relationship: dependency <Association <aggregation <combination. Aggregation and combination are two types of associations, including combination and aggregation.


Design Mode Column23 design pattern C ++ source code

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.