Design mode Note 5: Proxy mode

Source: Internet
Author: User

1.1 Definitions

Provides a proxy for other objects to control access to this object.

In fact, two classes implement an interface or an abstract class, instantiate Class B objects in Class A, then class A will be able to proxy class B to implement the interface method.

1.2 Class Diagram

An instance of the Realsubject class is declared in the Porxy proxy class, invoking the method of the Realsubject instance when it is necessary to invoke a method that they all implement. The agent for Realsubject is implemented.

1.3 Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceProxy Mode8 {9     Abstract classSubjectTen     { One          Public Abstract voidRequest (); A     } -  -     classRealsubject:subject the     { -          Public Override voidRequest () -         { -Console.WriteLine ("a real request"); +         } -     } +  A     classProxy:subject at     { - Realsubject rs; -  -  -          Public Override voidRequest () -         { in             if(rs = =NULL) -             { tors =NewRealsubject (); +             } - Rs. Request (); the         } *     } $ Panax Notoginseng  -}
View Code

Call:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceProxy Mode8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -Subject s =NewProxy (); - s.request (); the  -         } -     } -}
View Code

1.4 Summary

4 major scenarios for proxy mode:

1, the remote agent, for an object in different address space to provide local representation.

2, virtual agent, as needed to create a large cost of objects. The picture in the browser is downloaded, which is used to optimize it.

3, the security agent, control the real access to the object's permissions.

4, can only reference, call the real object is the agent can help us do extra things, such as record the number of citations, release objects and so on.

  

Design mode Note 5: 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.