C + + Design mode--a simple agent mode

Source: Internet
Author: User
Proxy mode: Provides a proxy for other objects to control access to the object (the agent, that is, himself).

The implementation method in C # is to use interface interface to define the required methods, whereas in C + + you need to implement a class to use as an interface, which defines the required methods. In this way, I do not need to participate in the specific activities, and by the agent. Client (third party) also do not know himself, to achieve the role of agents.

The above means: Use a class to encapsulate the required proxy methods.

Proxy mode application:

Remote creation, which provides a local representation of an object in a different address space. This can hide the fact that an object exists in a different address space. "DP"

Virtual proxies create expensive objects as needed. It's a real object that takes a long time to store the instantiation, for example: Open a large HTML page, we see the picture is a download to see, and not open the picture frame, is the virtual agent to replace the real picture. At this point the agent stores the path and size of the real picture.

A security agent that controls the "DP" of permissions for real-time object access, typically when the object should have different access rights.

Smart guidance refers to the fact that when invoking a real object, the agent handles something else "DP".

In this case, the agent mobile package:

Test Case:

[Code]int Main () {//The whole process, I did not appear    std::string name = "Jarrett";    Third-party company    CP (name);  The name of the applicant (i) tells the company    //Agent    Proxy *px = new proxy (CP);    Handle 58 RMB Package    px->a58 ();    Handle 88 RMB Package    px->a88 ();    Handle 128 RMB Package    px->a128 ();    return 0;}

Proxy class Implementation method:

[code]//body, provides interface.    No instantiation is defined as pure virtual function class subject{public:virtual void A58 () const = 0;    virtual void A88 () const = 0; virtual void A128 () const = 0;};/ /third-party company class company{public:std::string name;//companies need to know the name of the package company (std::string na): Name (NA) {}};//himself (the agent) CL The oneself:public subject{private:company CP;//I want to know who I am doing to whom, here I know which company to apply for the package Public:oneself (Corporation C): CP (c) {}/ /Here is the proxy class to initialize void A58 () const override{std::cout << cp.name << "for packages.\n";//Tell the mobile company I have 58 sets of    Meal} void A88 () const override{std::cout << cp.name << "for packages.\n";    } void A128 () const override{std::cout << cp.name << "for packages.\n";    }};class proxy:public subject{private://agent who? Oneself Os;public:proxy (company CP): OS (oneself (CP)) {}///Here Initializes my constructor, which achieves the purpose of the proxy void A58 () const override{OS.    A58 (); } void A88 () const override{OS.    A88 (); } void A128 () const override{OS.A128 (); }};

The above is the C + + design mode of the simple knowledge of the agent model, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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