浅尝DesignPattern_Proxy

本文详细介绍了UML设计模式中的代理模式概念、原理、实现方式及应用实例,包括如何通过代理模式维护引用、控制对真实对象的访问、以及不同类型的代理(如远程代理、虚拟代理、保护代理)的功能特性。并通过代码示例展示了如何在实际场景中利用代理模式优化系统设计。

UML:

2010042413083938.png

Proxy   (MathProxy)
  • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
  • 维持一个引用让代理访问真正的Subject。代理可以引用一个Subject,如果RealSubject和Subject接口是相同的。
  • provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
  • 提供等同于Subject 的接口,这样代理就能替代真正的Subject 
  • controls access to the real subject and may be responsible for creating and deleting it.
  • 控制进入真正的Subject 而且负责创建和删除它
  • other responsibilites depend on the kind of proxy:其他责任取决于代理的类型
    • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
    • 远程代理是负责编码空间的要求,其参数和真正的问题在一个不同的地址编码发送请求。
    • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
    • 虚拟代理可缓存的其他信息的真正Subject ,使他们可以推迟访问它。例如,ImageProxy的用处就是缓存实际图像的。
    • protection proxies check that the caller has the access permissions required to perform a request.
    • 保护代理检查调用方拥有访问权限的规定要求执行。
Subject   (IMath)
  • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • 定义RealSubject和代理的共同界面,使代理可以在任何地方使用RealSubject。
RealSubject   (Math)
  • defines the real object that the proxy represents. 
  • 定义了真正的对象,是代理代表的对象。

Sample:

IMath.cs

 

1 public interface IMath
2 {
3 double Add(double x, double y);
4 double Sub(double x, double y);
5 double Mul(double x, double y);
6 double Div(double x, double y);
7 }

 

 

 

Math.cs

 

1 class Math:IMath
2 {
3 #region IMath 成员
4
5 public double Add(double x, double y)
6 {
7 return x + y;
8 }
9
10 public double Sub(double x, double y)
11 {
12 return x - y;
13 }
14
15 public double Mul(double x, double y)
16 {
17 return x * y;
18 }
19
20 public double Div(double x, double y)
21 {
22 return x / y;
23 }
24
25 #endregion
26 }

 


 

MathProxy.cs
1 class MathProxy:IMath
2 {
3 private Math math = new Math();
4 #region IMath 成员
5
6 public double Add(double x, double y)
7 {
8 return math.Add(x, y);
9 }
10
11 public double Sub(double x, double y)
12 {
13 return math.Sub(x, y);
14 }
15
16 public double Mul(double x, double y)
17 {
18 return math.Mul(x, y);
19 }
20
21 public double Div(double x, double y)
22 {
23 return math.Div(x, y);
24 }
25
26 #endregion
27 }

 

Program.cs
1 #region Proxy
2 MathProxy proxy = new MathProxy();
3 Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));
4 Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));
5 Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));
6 Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));
7 #endregion

 




转载于:https://www.cnblogs.com/TivonStone/archive/2010/04/24/1719294.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值