Big talk design mode C + + version--proxy mode

Source: Internet
Author: User

before the beginning of this article to send a welfare, geoscience "big talk design mode" a book of high-definition electronic version (with catalog) has been uploaded to Csdn, free points download.
: http://download.csdn.net/detail/gufeng99/8843487

Proxy mode is a relatively simple but practical design mode, he can flexibly replace the object of the agent, but to ensure the integrity of the function, such as the sale of clothing agents, he can act as the agent of the United States Tesla's clothes, if the United States Tesla's clothes are not good to sell, he can also change agents to sell Giordano, but no matter how change , or to meet the needs of everyone-buy clothes.
The following is a big story design pattern book example, design an agent to help Xiao Ming send flowers to Xiao Red.

1, according to the interface programming, design the interface of the agent object

Class Ipursuit{public:virtual~ipursuit () {}virtualvoidsendflowers () = 0;};

2, the proxy class, also inherits the proxy object class, maintains the interface consistent
Class Cproxy:public Ipursuit{public:cproxy (): M_poipursuit (NULL) {}~cproxy () {if (m_poipursuit) {deletem_poipursuit;m _poipursuit = NULL;}} Voidsetpursuit (ipursuit* poipursuit) {//If the old agent is to be removed first, it will cause a memory leak if (m_poipursuit) {deletem_poipursuit;} M_poipursuit = Poipursuit;} Voidsendflowers () {if (m_poipursuit) {printf ("Proxy help"); M_poipursuit->sendflowers ();}} Private:ipursuit*m_poipursuit;};
The proxy class actually did nothing, but the same function called the corresponding function of the object being proxied, when the role of the setter. It is important to note that the proxy object is released in the proxy, so the object of the proxy must always be new, which is to be created on the heap.
3. Proxy object class
Class Cpursuit:public Ipursuit{public:cpursuit (tstring tstrname): M_tstrname (tstrname) {}~cpursuit () {} Voidsendflowers () {_tprintf (_t ("%s sent flowers to xiaohong\n"), M_tstrname.c_str ());} Private:tstringm_tstrname;};
Attach another tstring macro
#ifdef  Unicode#definetstringstd::wstring#else#definetstringstd::string#endif
4. Test example
Voidtest () {ipursuit*poixiaoming = new Cpursuit (_t ("xiaoming")); Cproxyocproxy;ocproxy.setpursuit (poixiaoming); Ocproxy.sendflowers ();}
5, the application of the agent class
This example is very image, but it is difficult to see the application and advantages of proxy mode. In fact, in the "Big talk design mode C + +-Abstract Factory mode" has an example of operating database management staff information, because the database may be used in the process of switching databases, such as the previous use of MySQL, a customer may require support for access, then you have to switch This is done in the same way as proxy mode.
5.1 Proxy mode to implement employee database management class to switch database
Typedefstruct Employee {Intnid; Tstringtstrname;}; Class Iemployee{public:~iemployee () {}virtualboolinserttodb (employee& stemployee) = 0; Virtualemployeegetemployee (int NID) = 0;}; Class Cproxy:public Iemployee{public:public:cproxy (): M_poiemployee (NULL) {}~cproxy () {if (M_poiemployee) {deletem_ Poiemployee;m_poiemployee = NULL;}} Voidsetemployee (iemployee* poiemployee) {if (m_poiemployee) {deletem_poiemployee;} M_poiemployee = Poiemployee;} Boolinserttodb (employee& stemployee) {if (M_poiemployee) {returnm_poiemployee->inserttodb (stEmployee);} Returnfalse;} Employeegetemployee (int NID) {if (M_poiemployee) {returnm_poiemployee->getemployee (NID);} Employeestemployee;returnstemployee;} Private:iemployee*m_poiemployee;}; Class Cemployeefrommysql:public Iemployee{public:boolinserttodb (employee& stemployee) {_tprintf (_T ("Insert Employee%s into mysql\n "), StEmployee.tstrName.c_str ()); returntrue;} Employeegetemployee (int nID) {employeestemployee;printf ("Get an employee from MySQL by id%d\n", NID); returnstemployee;}; Class Cemployeefromaccess:publiciemployee{public:boolinserttodb (employee& stemployee) {_tprintf (_T ("Insert Employee%s into access\n "), StEmployee.tstrName.c_str ()); returntrue;} Employeegetemployee (int nID) {employeestemployee;printf ("Get an employee from Access by ID%d\n", nID); returnstemployee;}} ;
5.2 Use examples
Voiddatabasetest () {iemployee*poiemployee = new cemployeefrommysql (); Cproxyocproxy;ocproxy.setemployee (Poiemployee); Employeestemployee;stemployee.nid = 1;stemployee.tstrname = _t ("Jim"); Ocproxy.inserttodb (stemployee);// Switch database Objects poiemployee= newcemployeefromaccess (); Ocproxy.setemployee (Poiemployee); Ocproxy.inserttodb (StEmployee);}
As can be seen from the use example, the proxy class enables the client to dynamically switch the database during use, which is different from the largest plant pattern, and is especially useful in places where it is often necessary to switch similar object patterns.

Copyright NOTICE: This article for Bo Master original article, if you need to reprint please explain go to http://blog.csdn.net/gufeng99

Big talk design mode C + + version--proxy mode

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.