C ++ design mode-proxy Mode
# Include
Using namespace std;
// In the proxy mode, note that although the proxy can implement a function, the proxy does not have this function. The proxy is implemented by calling other functions.
// For the specific function, the basic class pointer in the polymorphism refers to the object of the derived class, and calls the fun function of the derived class.
Class Base
{
Public: virtual void fun () = 0;
Virtual ~ Base (){}
};
Class Derived: public Base
{
Public: void fun ()
{Cout <"Derived fun" <
};
Class proxy: public Base
{
Private: Base * pBase; // Note: If you write an object or pointer to the Derived class, you can certainly implement it,
// But if the proxy class needs to implement other functions, it needs to add the object or pointer of the class.
// Violation of the open/closed principle in c ++ design mode (open interface, disable modification)
Public: proxy (Base * t) {pBase = t ;}
Void fun ()
{
PBase-> fun ();
}
};
Int main (void)
{
Derived * pDerived = new Derived ();
Proxy * p = new proxy (pDerived );
P-> fun ();
Delete pDerived;
Delete p;
}