设计模式-代理模式

通过代理完成对业务类的访问,包一层方便人以功能扩展。

代理模式:VPN代理,FQ代理,火车票代理等。

通过代理业务类去完成对真实业务类的调用,代理类不能扩展业务功能,在不修改RealSubject前提下,插入功能。

包一层:没有什么技术问题是包一层解决不了的,如果有,那么就再包一层。比如来个日志记录,可以避免修改业务类,只需要修改代理类。在来个异常处理;在来个性能提成----缓存结果........

通过代理,能够为对象扩展功能(不是增加业务)而不去修改原始业务类,也就是包了一层,我的地盘听我的

 /// <summary>
 /// 业务接口
 /// </summary>
 public interface ISubject
 {
     /// <summary>
     /// get
     /// </summary>
     /// <returns></returns>
     bool GetSomething();

     /// <summary>
     /// do
     /// </summary>
     void DoSomething();
 }
public class ProxySubject : ISubject
{
    //组合一下
    private static ISubject _Subject = new RealSubject();
    public void DoSomething()
    {
        try
        {
            Console.WriteLine("prepare DoSomething...");
            _Subject.DoSomething();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw ex;
        }
    }

    private static Dictionary<string, bool> ProxyDictionary = new Dictionary<string, bool>();
    public bool GetSomething()
    {
        try
        {
            Console.WriteLine("prepare GetSomething...");
            string key = "Proxy_GetSomething";
            bool bResult = false;
            if (!ProxyDictionary.ContainsKey(key))
            {
                bResult = _Subject.GetSomething();
                ProxyDictionary.Add(key, bResult);
            }
            else
            {
                bResult = ProxyDictionary[key];
            }
            return bResult;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw ex;
        }
    }
}
View Code
/// <summary>
/// 一个耗时耗资源的对象方法
/// 一个第三方封装的类和方法
/// </summary>
public class RealSubject : ISubject
{
    public RealSubject()
    {
        Thread.Sleep(2000);
        long lResult = 0;
        for (int i = 0; i < 100000000; i++)
        {
            lResult += i;
        }
        Console.WriteLine("RealSubject被构造。。。");
    }

    /// <summary>
    /// 火车站查询火车票
    /// </summary>
    public bool GetSomething()
    {
        Console.WriteLine("坐车去火车站看看余票信息。。。");
        Thread.Sleep(3000);
        Console.WriteLine("到火车站,看到是有票的");
        return true;
    }

    /// <summary>
    /// 火车站买票
    /// </summary>
    public void DoSomething()
    {
        Console.WriteLine("开始排队。。。");
        Thread.Sleep(2000);
        Console.WriteLine("终于买到票了。。。");
    }
}
View Code

调用一下:

 {
     Console.WriteLine("***********Real**************");

     ISubject subject = new RealSubject();
     subject.GetSomething();
     //subject.DoSomething();
 }
 {
     Console.WriteLine("***********Proxy**************");
     ISubject subject = new ProxySubject();
     subject.GetSomething();
     //subject.DoSomething();
 }
 {
     Console.WriteLine("***********Proxy**************");
     ISubject subject = new ProxySubject();
     subject.GetSomething();
     //subject.DoSomething();
 }
View Code

调用下看下结果:

 

 

 通过代理,能够为对象扩展功能(不是增加业务),而不是去修改原始业务类,就是包一层。

WebService就是代理模式。

 

posted @ 2019-09-19 20:05  冰乐  阅读(341)  评论(0编辑  收藏  举报