C + + design mode

Source: Internet
Author: User

Copyright notice: If no source note, techie bright blog article are original. Reprint please indicate the title and address of this article in the form of link:
This article title: C + + design mode This article address: http://techieliang.com/2017/12/764/ Article Directory
    • 1. Six major design principles
    • 1.1. Single Duty principle (Responsibility Principle)
    • 1.2. Richter replacement principle (Liskov Substitution Principle, LSP)
    • 1.3. Dependency inversion principle (dependence inversion Principle, DIP)
    • 1.4. Interface Isolation principle
    • 1.5. Dimitri (Law of Demeter, LoD)
    • 1.6. Opening and closing principle
    • 2. Design Pattern classification
    • 2.1. Create pattern
    • 2.2. Institutional mode
    • 2.3. Behavioral Mode

Recently began to look at the design of the pattern of Zen, before the pity did not apply, this time to look at the finishing

1. Six major design principles 1.1. Single Duty principle (Responsibility Principle)

There should never is more than one reason for a class-to-change. There should be and only one cause for class changes.

1.2. Richter replacement principle (Liskov Substitution Principle, LSP)

The first definition is also the most authentic definition:The If for each object O1 of type S There are an object O2 of type T such so for all programs P defined in terms of t,the Beh Avior of P was unchanged when O1 was substituted for O2 and S is a subtype of T.so1t The object o2t All programs defined p in all objects o1 replace o2st

The second definition: Functions that use pointers or references to base classes must being able to use objects of derived classes Withou T knowing it. (all references to base classes must be able to transparently use objects of their subclasses.) )

1.3. Dependency inversion principle (dependence inversion Principle, DIP)

The modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
High-level modules should not rely on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.

There are three ways to pass a dependency: the constructor passes the dependent object, the Setter method passes the dependent object, and the interface declares the dependent object.

1.4. Interface Isolation principle

The object of the instance interface (object Interface) class conforms to the definition of the class
The definition of class interface classInterface conforms to the interface definition, and C + + is a virtual function that conforms to the virtual keyword definition, and Java conforms to the interface defined by the Interface keyword.

    • Clients should not being forced to depend upon interfaces that they don ' t use. (the client should not rely on interfaces it does not need.) )
    • The dependency of one class to another one should depend on the smallest possible interface. (the dependencies between classes should be based on the smallest interface.) )
1.5. Dimitri (Law of Demeter, LoD)

Also known as the least-knowledge principle (Least knowledge Principle, LKP), an object should have a minimal understanding of other objects.

    • Only talk to your immediate friends(communicates only with direct friends. )
    • Friends also have distance, try not to publish too many public methods and non-static variables, as far as possible, the use of private, package-private, protected and other access rights
    • Is their own is their own, if a method in this class, do not increase the relationship between the class, but also do not have a negative impact on this class, it is placed in this class
    • Careful use of Serializable
1.6. Opening and closing principle

Software entities like classes,modules and functions should is open for extension but closed for modifications. (a software entity such as classes, modules, and functions should be open to extensions and closed for modification.) )

2. Design Pattern classification

There are 23 types of design patterns can be divided into three categories, various types of detailed introduction see Techie Bright Blog

type Brief Introduction
Create model (creational Patterns) Dealing with the creation of objects, trying to create objects in a suitable way, hiding the logic of the creation process does not need to use new. Wiki
Structural mode (Structural Patterns) Simplifies design by describing the combination of inter-entity relationships, classes, and objects. Wiki
Behavioral mode (behavioral Patterns) Identify common communication patterns between objects and implement design patterns for these patterns. By doing so, these patterns increase the flexibility to perform this communication. Wiki
2.1. Create pattern
  • Singleton mode (Singleton pattern)
    Ensure a class have only one instance, and provide a global point of access to it. (make sure that there is only one instance of a class, and instantiate it yourself and provide this instance to the entire system.) )
  • Factory method Pattern (Factory mode)
    Define an interface for creating a object,but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. (Define an interface for creating objects so that subclasses decide which class to instantiate.) The factory method defers the instantiation of a class to its subclasses. )
  • Abstract Factory mode (Factory pattern)
    Provide an interface for creating families of related or dependent objects without specifying their concrete classes. (provides an interface for creating a set of related or interdependent objects without specifying their specific classes.) )
  • Builder pattern/ Builder pattern (constructor pattern)
    Separate the construction of a complex object from it representation so, the same construction process can create dif Ferent representations. (separating the construction of a complex object from its representation allows the same build process to create different representations.) )
  • Prototype mode (Prototype pattern)
    Specify the kinds of objects to create using a prototypical Instance,and create new objects by copying this prototype. (Specify the kind of objects created with the prototype instance and create new objects by copying the prototypes.) )
2.2. Institutional mode
  • Agent mode (proxy pattern)
    Provide a surrogate or placeholder for another object to control access to it. (provides a proxy for other objects to control access to this object.) )
  • Decoration mode (Decorator pattern)
    Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible
    Alternative to subclassing for extending functionality. (dynamically add some extra responsibilities to an object.) For added functionality, the adornment mode is more flexible than generating subclasses. )
  • Adapter Mode/ Transformer mode (Adapter pattern)/ packaging mode ( wrapper)
    Packing mode also includes decorative mode
    convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn ' t otherwise because of incompatible interfaces.
  • combined mode/ compositing mode (Composite pattern)/ part – overall mode (part-whole)
    compose objects into the tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. " section – overall " hierarchy, Makes the user consistent with the use of individual objects and composite objects. )
  • Appearance mode/ façade mode (facade pattern)
    Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. (requires that the outside of a subsystem and its internal communication must be done through a unified object.) Façade mode provides a high-level interface that makes subsystems easier to use. )
  • Enjoy meta mode (Flyweight pattern)
    The important way to realize pool technology. Use sharing to support large numbers of fine-grained objects efficiently. (using shared objects can effectively support a large number of fine-grained objects.) )
  • Bridge Mode/Bridging mode (bridge pattern)
    Decouple an abstraction from its implementation so and the can vary independently. (decoupling abstractions and implementations so that they can vary independently.) )
2.3. Behavioral Mode
  • Template method Pattern (template mode)
    Define The skeleton of a algorithm in a operation,deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm ' s structure. (a framework that defines an algorithm in an operation, and some steps are deferred to subclasses.) Enables subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. )
  • Command pattern
    Encapsulate a request as an Object,thereby letting are parameterize clients with different requests,queue or log requests, and Support undoable
    Operations. (encapsulating a request as an object allows you to parameterize the client with different requests, queue requests or log request logs, and provide undo and redo functions for the command.) )
  • Mediator mode (mediator pattern)
    Define an object, encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to all other
    Explicitly,and It lets you vary their interaction independently. (encapsulating a series of object interactions with a mediation object, the mediator makes the objects do not need to be shown to interact with each other, so that they are loosely coupled and can independently change the interaction between them.) )
  • Responsibility chain mode/responsibility Chain mode (Chain of Responsibility pattern)
    Avoid coupling the sender of a request to their receiver by giving + than one object a chance to handle the request. Chain the receiving objects and pass the request along the Chain until an object handles it. (enables multiple objects to have the opportunity to process the request, thus avoiding the coupling between the sender and the recipient of the request.) Link the objects to a chain and pass the request along this chain until an object has processed it. )
  • Policy mode/ policy mode (strategy pattern)
    Define a family of algorithms,encapsulate each one,and make them interchangeable. (define a set of algorithms that encapsulate each algorithm and make them interchangeable.) )
  • Iterator mode (Iterator pattern)
    Provide a-from access the elements of an aggregate object sequentially without exposing its underlying representation. (It provides a way to access individual elements in a container object without exposing the object's internal details.) )
  • Viewer mode (Observer pattern)/ Publish subscription mode (Publish/subscribe)
    Define a one-to-many dependency between objects so then one object changes State,all its dependents is notified and Updated automatically. (defines a one-to-many dependency between objects, so that whenever an object changes state, all objects that depend on it are notified and automatically updated.) )
  • Memo Mode (Memento pattern)
    Without violating Encapsulation,capture and externalize an object ' s internal state so, the object can be restored to T He state later. (without compromising encapsulation, capture the internal state of an object and save the state outside that object.) The object can then be restored to its previously saved state. )
  • Visitor Mode (Visitor pattern)
    Represent an operation to is performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. (encapsulates operations that act on elements of a data structure, and it can define new ones that act on these elements without altering the data structure.)
    Operation. )
  • Status mode (state pattern)
    Allow a object to alter it behavior when it internal state changes. The object would appear to the change of its class. (When an object's internal state changes to allow it to change its behavior, the object looks like it changed its class.) )
  • Interpreter mode (interpreter pattern)
    Given a language, define a representation for it grammar along with an interpreter that uses the representation to INTERP RET sentences in the language. (given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language.) )
Reprint please indicate the title and address of this article in the form of link: Techie Liang blog»c++ design mode

C + + 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.