/*************************************** **************************************** ***************************/
* 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 );
}