C#代理模式-Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace ProxyModelDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("代理模式");

                {
                    //Console.WriteLine("*************Real******************");
                    //ISubject subject = new RealSubject();
                    //subject.GetSomethingLong();
                    //subject.DoSomethingLong();
                }
                {
                    Console.WriteLine("*************Real******************");
                    ISubject subject = new RealSubject();
                   
                    {
                        //构造了对象,占用了资源,但是干别的事情去了。。。浪费了资源,占着茅坑不拉屎
                        Console.WriteLine("do something else1....");
                        Console.WriteLine("do something else2....");
                        Console.WriteLine("do something else3....");
                        System.Threading.Thread.Sleep(2000);
                    }
                    subject.GetSomethingLong();//第一次将数据加入缓存
                    subject.DoSomethingLong();
                }

                {
                    Console.WriteLine("*************Proxty******************");
                    ////按照线程,给线程存储数据,字典
                    //CallContext.SetData("CurrentUser", "Eleven");

                    ISubject subject = new ProxySubject();

                    {
                        //构造了对象,占用了资源,但是干别的事情去了。。。浪费了资源,占着茅坑不拉屎
                        Console.WriteLine("do something else1....");
                        Console.WriteLine("do something else2....");
                        Console.WriteLine("do something else3....");
                        System.Threading.Thread.Sleep(2000);
                    }
                    subject.GetSomethingLong(); //调用缓存 //希望资源暂用在这里才发生
                    subject.DoSomethingLong();
                }

                {
                    Console.WriteLine("*************Proxty******************");
                    ISubject subject = new ProxySubject();
                    subject.GetSomethingLong();//调用缓存
                    subject.DoSomethingLong();
                }

                {
                    Console.WriteLine("*************Proxty******************");
                    ISubject subject = new ProxySubject();
                    subject.GetSomethingLong();//调用缓存
                    subject.DoSomethingLong();
                }

                Console.Read();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/89428636
今日推荐