Design Pattern simple code-proxy pattern (buy a house)

Source: Internet
Author: User

/*************************************** **************************************** ***************************/
* Author: Yi Yutian (http://blog.csdn.net/dylgsy ). This article can be ressed casually, but please keep this information

* Proxy mode: the proxy mode, as its name implies, is to handle some things on our behalf. We don't need to do everything ourselves.

* Programming: Setting proxy means forwarding customer requests to real entities when necessary. There are many examples in reality, such as the Proxy Server

* The proxy server stores a lot of information locally. When we access it, it seems like we are accessing a real server.

* If it is not on the proxy server, it will automatically connect to the Real Server, where it gets new information, and then save it locally. All this is our generation.

* We Don't Need To Know What we do automatically (for customers, it seems like they are accessing real servers. But for the real server, it can reduce the burden of one copy ).
/*************************************** **************************************** ***************************/

UML is as follows:

/*************************************** **************************************** ***************************/
* Instance description:

* Buying a house requires a bunch of procedures. We don't want to bother with it, so we need a house agent to help us with the procedures (intermediary)

/*************************************** **************************************** ***************************/

 

# Include <iostream>
Using namespace STD;

// Define an abstract class first
Class csubject
...{
Public:
// Define the operation for purchasing a house
Virtual void buyroom () = 0;
Bool m_bdone;
};

// Define the buyer's class, derived from csubject
Class cbuyer: Public csubject
...{
Public:
Cbuyer ()
...{
M_bdone = false ;;
}
Virtual void buyroom ()
...{
If (! M_bdone)
...{
// The customer's concept of buying a house is that I only pay for it and don't want to worry about other things.
Cout <"I am a customer and I only need to pay for it. "<Endl;
Cout <"the procedure is not completed, and you cannot pay for it" <Endl;
Cout <Endl;
}
Else
...{
Cout <"I am a customer and I only need to pay for it. "<Endl;
Cout <"well, you have completed all the procedures. The house was bought. "<Endl;
Cout <Endl;
}
}
};

// Defines the proxy class, derived from csubject
// The proxy class has a reference from the buyer, so that the buyer's function can be called at an appropriate time.
Class cbuyerproxy: Public csubject
...{
Public:
Cbuyerproxy (csubject * s)
...{
_ S = s;

}
Virtual void buyroom ()
...{
Cout <"I am an intermediary: Loan ";
Cout <"tax payable ";
Cout <"Contract Signing" <Endl;

Cout <"I only handle the formalities, and the money will be paid by the buyer, so the buyer's function is called here" <Endl;

_ S-> m_bdone = true;
_ S-> buyroom ();
}

PRIVATE:
Csubject * _ s;
};


Void buyroom (csubject * s)
...{
S-> buyroom ();
}

// Use the above class to simulate buying a house
Void main ()
...{
Cbuyer buyer;
Cbuyerproxy buyerproxy (& buyer );

// Do not buy a house through an intermediary
Buyroom (& buyer );

// Buy a house through an intermediary
Buyroom (& buyerproxy );
}

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.