Design Mode note (13)-proxy mode (Structural)

Source: Internet
Author: User
Document directory
  • Gof Definition
  • Motivation
  • Application scenarios in proxy Mode
  • Key Points of proxy Mode
Gof Definition

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

Motivation

In an object-oriented system, some objects are required for some reason (for example, the overhead of object creation is large, security control is required for some operations, or external access is required ), direct access may cause a lot of trouble for users or system structures. How can we manage/control the unique complexity of these objects without losing transparent operation objects? Adding an indirect layer is a common solution in software development. See the following structure:

Subject: This class defines some common methods of realsubject and proxy.

Realsubject: this class is the entity class that the proxy wants to proxy. The client only interacts with the proxy.

Code implementation:

/// <Summary> /// public interface /// </Summary> interface isubject {void request ();} /// <summary> /// proxy class /// </Summary> public class realsubject: isubject {public void request () {console. writeline ("request by proxy class") ;}/// <summary> /// proxy class /// </Summary> public class Proxy: isubject {realsubject = new realsubject (); Public void request () {realsubject. request ();}} /// <summary> /// Customer Code /// </Summary> /// <Param name = "ARGs"> </param> static void main (string [] ARGs) {proxy = new proxy (); proxy. request ();}

 

The above code is very simple. The client uses the proxy class. In fact, it calls methods in the realsubject class and serves the purpose of proxy.

Application scenarios in proxy Mode

The remote proxy provides a local representation of an object in different address spaces. In this way, the fact that an object has different address spaces can be hidden. (WebService in net)

Virtual Proxy: Creates objects with high overhead as needed. It stores real objects that take a long time for instantiation.

Security Proxy, used to control the access permissions of real objects.

Intelligent guidance refers to the proxy to handle other things when the call is an object.

[The above four points are taken from the big talk design model]

Key Points of proxy Mode

Adding an indirect layer is a common solution to many complex problems in software systems. In an object-oriented system, using some objects directly brings about many problems. As the proxy object of the indirect layer, it is a common method to solve this problem.

The implementation methods and implementation granularity of specific proxy design patterns vary greatly, and some may control individual objects in fine granularity, such as copy-on-write technology, some may provide an abstract proxy layer for the component module and proxy the object at the architecture level.

Proxy does not necessarily require consistency of interfaces. As long as indirect control is implemented, sometimes loss and some transparency are acceptable.

Return to the beginning (INDEX)

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.