Design Pattern-Proxy pattern (C ++ implementation)

Source: Internet
Author: User

Proxy mode, so what is proxy mode? We can understand it in this way. I am very busy and I am not busy with you. If you want to contact me, please contact my agent first, the agent will always know
What can the agent do and cannot do? That is, two people have the same interface. Although the agent cannot work
Agents can work. It is very abstract. It depends on the actual situation of the instance!

 

For example, if Simon Qing is looking for Pan Jinlian, Pan Jinlian is embarrassed to reply. What should I do? Look for the Wang Po as an agent, as shown in the following code:
First define a type of women

[Cpp]
Class KindWomen // indicates this type of women, both Wang Po and Pan Jinlian belong to this type of women.
{
Public:
Virtual void HappyWithMan () = 0; // happy with men, you know, Wang Po and Pan Jinlian will
Virtual void MakeEyesWithMan () = 0; // give a wink to a man, both Wang Po and Pan Jinlian
};
 
// Pan Jinlian
Class Panjinlian: public KindWomen
{
Public:
Virtual void HappyWithMan ()
{
Cout <"Pan Jinlian and men are happy ......" <endl;
}
 
Virtual void MakeEyesWithMan ()
{
Cout <"Pan Jinlian glances at men." <endl;
}
};
 
// Wang Po appearance
Class WangPo: public KindWomen
{
Private:
KindWomen * kindWomen;
Public:
Virtual void HappyWithMan () // Wang knows that she is old and happy, so she asks others to do it.
{
KindWomen-> HappyWithMan ();
}
 
Virtual void MakeEyesWithMan () // Wang Po is so ugly and who can look at it? So she calls someone else to throw it.
{
KindWomen-> MakeEyesWithMan ();
}
 
WangPo (KindWomen * m_pKindWomen) // sets the proxy owner.
{
This-> kindWomen = m_pKindWomen;
}
};
 
// Master function of Ximen Qing Control
Int main ()
{
// Simon Qing watched Pan Jinlian, but Pan Jinlian was shy and did not dare to put his eyes on him. At this time, Wang Po was required to help him do something.
 
WangPo wangpo = new WangPo (new Panjinlian (); // Wang Po, acting for Pan Jinlian
 
Wangpo. MakeEyesWithMan (); // Wang Po began to give a wink, but in fact Pan Jinlian was giving a wink.
 
Wangpo. HappyWithMan (); // Wang Po starts to be happy with men... in fact, Wang Po can be happy with men. Behind the scenes, Pan Jinlian is happy.
System ("pause ");
Return 0;
}

Class KindWomen // indicates this type of women, both Wang Po and Pan Jinlian belong to this type of women.
{
Public:
Virtual void HappyWithMan () = 0; // happy with men, you know, Wang Po and Pan Jinlian will
Virtual void MakeEyesWithMan () = 0; // give a wink to a man, both Wang Po and Pan Jinlian
};

// Pan Jinlian
Class Panjinlian: public KindWomen
{
Public:
Virtual void HappyWithMan ()
{
Cout <"Pan Jinlian and men are happy ......" <endl;
}

Virtual void MakeEyesWithMan ()
{
Cout <"Pan Jinlian glances at men." <endl;
}
};

// Wang Po appearance
Class WangPo: public KindWomen
{
Private:
KindWomen * kindWomen;
Public:
Virtual void HappyWithMan () // Wang knows that she is old and happy, so she asks others to do it.
{
KindWomen-> HappyWithMan ();
}

Virtual void MakeEyesWithMan () // Wang Po is so ugly and who can look at it? So she calls someone else to throw it.
{
KindWomen-> MakeEyesWithMan ();
}

WangPo (KindWomen * m_pKindWomen) // sets the proxy owner.
{
This-> kindWomen = m_pKindWomen;
}
};

// Master function of Ximen Qing Control
Int main ()
{
// Simon Qing watched Pan Jinlian, but Pan Jinlian was shy and did not dare to put his eyes on him. At this time, Wang Po was required to help him do something.

WangPo wangpo = new WangPo (new Panjinlian (); // Wang Po, acting for Pan Jinlian

Wangpo. MakeEyesWithMan (); // Wang Po began to give a wink, but in fact Pan Jinlian was giving a wink.

Wangpo. HappyWithMan (); // Wang Po starts to be happy with men... in fact, Wang Po can be happy with men. Behind the scenes, Pan Jinlian is happy.
System ("pause ");
Return 0;
}
If Lu Junyi's wife wants to play with Simon Qing, he can also find Wang Po as an agent, inherit the abstract class and implement the method, and pass it to Wang Po to act as an agent, sometimes, if you need to proxy too many objects, you can also use it in the meta mode.

 

To sum up, the proxy mode is implemented by using interfaces and polymorphism. It encapsulates the people who need the proxy, and the people who directly come forward to do the work are agents, but what we are actually working on is the person to be represented.


 

 

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.