Proxy extraction in Design Mode

Source: Internet
Author: User

The 23 GOF design modes are generally divided into three categories: Creation Mode, structure mode, and behavior mode.

The Creation Mode abstracts the instantiation process, which helps a system to create, combine, and express its objects independently. A class creation mode uses inheritance to change the class to be instantiated, while an object Creation Mode delegates the Instantiation to another object. The creation mode has two main themes. First, they all encapsulate information about the specific classes used by the system. Second, they hide how instances of these classes are created and put together. All objects in the system know interfaces defined by abstract classes. Therefore, the Creation Mode gives great flexibility in what is created, who creates it, how it is created, and when it is created. They allow you to configure a system with "product" objects with very different structures and functions. The configuration can be static (specified during compilation) or dynamic (at runtime ).

The schema involves how to combine classes and objects to obtain a larger structure. The structure class mode uses an Inheritance Mechanism to combine interfaces or implementations. The structured object mode not combines interfaces and implementations, but describes how to combine some objects to implement new functions. Because the object composite relationship can be changed at runtime, the object composite method has more flexibility, and this mechanism is impossible to achieve with static class composite.

The behavior pattern involves the assignment of duties between algorithms and objects. The behavior pattern not only describes the object or class pattern, but also describes the communication pattern between them. These modes depict complex control flows that are difficult to trace during runtime. They shift users' attention from the control flow to the contact information between objects. The behavior class mode uses the Inheritance Mechanism to distribute actions among classes. Behavior object mode uses Object combination instead of inheritance. Some behavior object modes describe how peer-to-peer objects collaborate to complete tasks that cannot be completed independently by any object.

The creation mode includes: 1. FactoryMethod (Factory method mode); 2. Abstract Factory (Abstract Factory mode); 3. Singleton (Singleton mode); 4. Builder mode and Builder mode ); 5. Prototype (Prototype ).

Structural modes include: 6. Bridge (Bridge Mode); 7. Adapter (Adapter mode); 8. Decorator (decoration mode); 9. Composite (combination mode ); 10. Flyweight (metadata mode); 11. Facade (appearance mode); 12. Proxy mode ).

Behavior patterns include: 13. TemplateMethod (template method mode); 14. Strategy (Policy mode); 15. State (State mode); 16. Observer (Observer mode ); 17. Memento (memorandum mode); 18. Mediator (intermediary mode); 19. Command (Command mode); 20. Visitor (Visitor mode ); 21. Chain of Responsibility (responsible Chain mode); 22. Iterator (Iterator mode); 23. Interpreter (Interpreter mode ).

Factory Method: Defines an interface for creating objects, so that the subclass determines which class to instantiate. Factory Method delays the instantiation of a class to its subclass.

Abstract Factory: Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

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

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

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

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

Adapter: 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 add some additional responsibilities to an object. In terms of extended functions, the Decorator mode is more flexible than the subclass generation method.

Composite: Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the customer's use of a single object and Composite object consistent.

Flyweight: Use 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 the subsystem easier to use.

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

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

Strategy: Define a series of algorithms, encapsulate them one by one, and make 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: Defines a one-to-many dependency between objects, so that when the status of an object changes, all objects dependent on it will be notified and automatically refreshed.

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

Mediator: Encapsulate a series of object interactions with an intermediary object. 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.

Command: Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support cancelable operations.

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

Chain of Responsibility: To remove 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.

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

Interpreter: For a given language, define a representation of its syntax and define an interpreter that uses this representation to interpret sentences in the language.

Proxy :( 1) intent: provide a Proxy for other objects to control access to this object.

(2) Applicability: the Proxy mode is used to replace simple pointers with common and complex object pointers. A. Remote Proxy: provides A local representation of an object in different address spaces. B. Virtual Proxy: Creates objects with high overhead as needed. C. Protection Proxy: controls access to the original object. Protection proxy is used when the object should have different access permissions. D. Smart Reference: instead of a simple pointer, it performs some additional operations when accessing the object. Its typical uses include: a. reference counting to the actual object, so that the object can be released automatically when it is not referenced. B. When a persistent object is referenced for the first time, load it into the memory. C. before accessing an actual object, check whether it has been locked to ensure that other objects cannot be changed.

(3) related mode: A. The Adapter provides A different interface for the object it adapts. Instead, the proxy provides the same interface as its entity. However, the proxy for access protection may reject operations performed by the entity. Therefore, its interface may actually be a subset of the object interface. B. Although the implementation of Decorator is similar to that of proxy, the purpose of decorator is different. The Decorator adds one or more functions to an object, while the proxy controls access to the object. The proxy implementation is similar to the Decorator implementation, but the degree of similarity varies. The implementation of Protection Proxy may be similar to that of Decorator. On the other hand, Remote Proxy does not include direct references to objects, but is only an indirect reference. At the beginning of Virtual Proxy, an indirect reference is used, for example, a file name, but a direct reference is obtained and used.

Remote Proxy: You can hide the fact that an object is in a different address space.

Virtual Proxy: stores objects that need to be instantiated for a long time through a proxy.

Security Proxy: used to control the access permissions of real objects.

Smart reference: when a real object is called, the proxy handles other things.

The biggest benefit of the Proxy mode is that the logic and implementation are completely decoupled.

Sample Code 1:

# Include
# Include
Using namespace std; // defines the Interface class Interface {public: virtual void Request () = 0 ;}; // real class RealClass: public Interface {public: virtual void Request () {cout <"Real requests" <
Request (); delete m_realClass ;}}; // client int main () {ProxyClass * test = new ProxyClass (); test-> Request (); /* Actual result Request */return 0 ;}

Sample Code 2:

Proxy. h:

#ifndef _PROXY_H_#define _PROXY_H_class Subject{public:virtual ~Subject();virtual void Request() = 0;protected:Subject();private:};class ConcreteSubject : public Subject{public:ConcreteSubject();~ConcreteSubject();void Request();protected:private:};class Proxy{public:Proxy();Proxy(Subject* sub);~Proxy();void Request();protected:private:Subject* _sub;};#endif//~_PROXY_H_

Proxy. cpp:

#include "Proxy.h"#include 
  using namespace std;Subject::Subject(){}Subject::~Subject(){}ConcreteSubject::ConcreteSubject(){}ConcreteSubject::~ConcreteSubject(){}void ConcreteSubject::Request(){cout<<"ConcreteSubject ... request ..."<

Main. cpp:

#include "Proxy.h"#include 
  using namespace std;int main(){Subject* sub = new ConcreteSubject();Proxy* p = new Proxy(sub);p->Request();/*resultProxy request ...ConcreteSubject ... request ...*/return 0;}

Proxy mode structure:


1. Big talk Design Model C ++

2. Design Patterns-Analysis of GoF23 Design Patterns

3. Design Patterns-Basics of reusable Object-Oriented Software

Related Article

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: 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.