Proxy patter -- proxy Mode

Source: Internet
Author: User

There is often a processing method for complex software systems, that is, adding an indirect layer, so that the system can obtain a more flexible solution to meet specific needs. In an object-oriented system, some objects require high overhead for some reason, such as object creation, security control for some operations, or access outside the process, direct access may cause a lot of trouble to the user or system structure. The Proxy design pattern is to manage and control the unique complexity of these objects by adding an indirect layer without losing transparent operation objects. [Cpp] // Proxy. h # include <string> # include <iostream> # include <string> class IEmployee {public: virtual string get_name (int ID) = 0; virtual int get_age (int ID) = 0; virtual double get_salary (int ID) = 0; public: virtual ~ IEmployee () ;}; IEmployee ::~ IEmployee () {cout <"in the destructor of IEmployee... "<endl;} class Employee: public IEmployee {public: string get_name (int ID); int get_age (int ID); double get_salary (int ID );~ Employee () ;}; string Employee: get_name (int ID ){//... assume that the database is queried here and the name of the Employee corresponding to the ID is string name = "tom"; return name;} int Employee: get_age (int ID ){//... assume that the database is queried here and the age int age = 20; return age;} double Employee: get_salary (int ID ){//... assume that the database is queried here and the Employee's salary corresponding to the ID is double salary = 100000.00; return salary;} Employee ::~ Employee () {cout <"in the destructor of Employee... "<endl ;}// proxy class EmployeeProxy: public IEmployee {public: string get_name (int ID); int get_age (int ID); double get_salary (int ID );~ EmployeeProxy () ;}; string EmployeeProxy: get_name (int ID ){//... assume that the get_name (int ID) method in the Employee is accessed through socket or RPC, And the return value string name = "tom"; return name;} int EmployeeProxy :: get_age (int ID ){//... assume that the get_age (int ID) method in the Employee is accessed through socket or RPC, and the corresponding return value int age = 20; return age;} double EmployeeProxy :: get_salary (int ID ){//... assume that the get_salary (int ID) side of the Employee is accessed through socket, RPC, and other methods. And accept the corresponding return value double salary = 100000.00; return salary;} EmployeeProxy ::~ EmployeeProxy () {cout <"in the destructor of EmployeeProxy... "<endl;} // Proxy. cpp # include "Proxy. h "int main (int argc, char ** argv) {IEmployee * employee = new EmployeeProxy; cout <employee-> get_name (10) <endl; cout <employee-> get_age (10) <endl; cout <employee-> get_salary (10) <endl; delete employee; return 0 ;}

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.