Seven proxies in Design Mode

Source: Internet
Author: User

Proxy mode, which provides a proxy for other objects to control access to this object.

Based on the example given by the author, that is, the givegift class implements virtual interfaces, which are implemented by the pursuer persuit. The proxy uses these interfaces and the client performs the overall call.

Let's list the code first.

The essence of the proxy mode is to implement entity actions through the proxy. The implementation of the code is only an Interface related to the proxy and the actual entity. <br/> the actual entity implementation interface, the proxy has access interface permissions <br/> givegift class defines virtual interfaces <br/> # pragma once <br/> # include "stdafx. H "<br/> class givegift <br/> {<br/> Public: <br/> virtual void givedolls () = 0; <br/> virtual void giveflowers () = 0; <br/> virtual void givechocolate () = 0; <br/>}< br/> pursuit class implementation interface <br/> # pragma once <br/> # include "stdafx. H "<br/> # include <iostream> <br/> # include" giv Egift. H "<br/>; Class schoolgirl; <br/> using namespace STD; <br/> class pursuit: Public givegift <br/>{< br/> schoolgirl * mm; <br/> Public: <br/> pursuit (schoolgirl * Mm) <br/>{< br/> This-> MM = mm; <br/>}< br/> pursuit () <br/>{</P> <p >}< br/> Public: <br/> void givedolls () <br/>{< br/> If (Mm! = NULL) <br/>{< br/> cout <mm-> getname (); <br/> cout <"show you a doll" <Endl; <br/>}< br/> void giveflowers () <br/>{< br/> If (Mm! = NULL) <br/>{< br/> cout <mm-> getname (); <br/> cout <"send flowers" <Endl; <br/>}< br/> void givechocolate () <br/>{< br/> If (Mm! = NULL) <br/>{< br/> cout <mm-> getname (); <br/> cout <"send you chocolate" <Endl; <br/>}< br/>}; <br/> proxy call interface <br/> # include "schoolgirl. H "<br/> # include" pursuit. H "<br/> class Proxy: public pursuit <br/>{< br/> pursuit * gg; <br/> Public: <br/> proxy (schoolgirl * mm) <br/>{< br/> GG = new pursuit (mm); <br/>}< br/> Public: <br/> void givedolls () <br/>{< br/> GG-> givedolls (); <br/>}< br/> void giveflowers () <br/>{< br/> GG-> giveflowers (); <br/>}< br/> void givechocolate () <br/>{< br/> GG-> givechocolate (); <br/>}< br/> }; <br/> client <br/> # include "stdafx. H "<br/> # include <iostream> <br/> # include" proxy. H "<br/> # include" schoolgirl. H "<br/> using namespace STD; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> schoolgirl * Jiaojiao = new schoolgirl (); <br/> Jiaojiao-> setname ("Li Jiaojiao "); <br/> proxy * daili = new proxy (Jiaojiao); <br/> const char * name = Jiaojiao-> getname (); <br/> daili-> givedolls (); <br/> daili-> giveflowers (); <br/> daili-> givechocolate (); <br/> cin. get (); <br/> return 0; <br/>}< br/> 

Proxy Mode

1)
Remote proxy,Provides a local representation of an object in different address spaces to hide the fact that an object exists in different address spaces.. Example:WebServiceIn. NetWhen you addWebReference toWebServiceIn this case,WebreferenceIn fact, they act as proxies, which allows the client program to call the proxy to solve the remote access problem.

2)
Virtual proxy,Create objects with high overhead as needed and store real objects that take a long time for instantiation.. For example, open a largeHtmlThere may be a lot of text and images in the webpage. For the image boxes opened for replacement, the virtual proxy is used to replace the real images, in this case, the virtual proxy stores the path and size of the real image.

3)
Security Proxy,Control the access permissions of real objects. It is generally used when the object should have different access permissions.

4)
Smart Guide, RefersWhen calling a real object, the proxy handles other things. For example, calculate the number of references of a real object, so that when the object is not referenced, it can be released independently; or when a persistent object is referenced at a time, it is loaded into the memory; or, before accessing an actual object, check whether it has been locked to ensure that other objects cannot change it.

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.