示例#1
0
        static void Main(string[] args)
        {
            Subject realSub = new RealSubject();
            Subject proxy   = new Proxy(realSub);

            proxy.request();


            IGamePlayer player = new GamePlayer("张三");
            //然后再定义一个代练者
            IGamePlayer constraintProxy = player.getProxy();

            //开始打游戏,记下时间戳
            Console.WriteLine("开始时间是:" + DateTime.Now);
            constraintProxy.login("zhangSan", "password");
            //开始杀怪
            constraintProxy.killBoss();
            //升级
            constraintProxy.upgrade();
            //记录结束游戏时间
            Console.WriteLine("结束时间是:" + DateTime.Now);


            DynamicProxy      proxy1 = new DynamicProxy(typeof(DynamicGamePlayer), new DynamicGamePlayer("张三"));
            DynamicGamePlayer plane  = (DynamicGamePlayer)proxy1.GetTransparentProxy();

            plane.login("zhangSan", "password");
            plane.killBoss();
            plane.upgrade();
            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            Client client = new Client();

            Console.WriteLine("Client: Executing the client code with a real subject:");
            RealSubject realSubject = new RealSubject();

            client.ClientCode(realSubject);

            Console.WriteLine();

            Console.WriteLine("Client: Executing the same client code with a proxy:");
            Proxy proxy = new Proxy(realSubject);

            client.ClientCode(proxy);

            Console.WriteLine("Practical Part: YouTubeCacheProxy");

            YouTubeDownloader naiveDownloader = new YouTubeDownloader(new ThirdPartyYouTubeClass());
            YouTubeDownloader smartDownloader = new YouTubeDownloader(new YouTubeCacheProxy());

            Console.WriteLine("---naiveDownloader --- start--");
            TimeSpan naive = Test(naiveDownloader);

            Console.WriteLine("---naiveDownloader --- end--");
            Console.WriteLine("---smartDownloader --- start--");
            TimeSpan smart = Test(smartDownloader);

            Console.WriteLine("---smartDownloader --- end--");
            Console.WriteLine("Time saved by caching proxy: " + (naive - smart) + "ms");
        }
示例#3
0
        static void Main(string[] args)
        {
            try
            {
                {
                    //new 一个对象  站着一些资源  但是干别的事儿去了
                    ISubject subject = new RealSubject();   // new 数据库连接
                    Console.WriteLine("do something else"); // 准备一些参数, 做一些判断
                    Console.WriteLine("do something else"); // new command 拼装 SQL
                    Console.WriteLine("do something else");
                    subject.GetSomethingLong();
                    subject.DoSomethingLong();
                }

                {
                    ISubject subject = new ProxySubject();
                    subject.GetSomethingLong();
                    subject.DoSomethingLong();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#4
0
 public override void Request()
 {
     if (realSubject == null)
     {
         realSubject = new RealSubject();
     }
     realSubject.Request();
 }
 public void doAction()
 {
     if (realsubject == null)
     {
         realsubject = new RealSubject();
     }
     realsubject.doAction();
 }
示例#6
0
 public void Request2()
 {
     if (realSubject == null)
     {
         realSubject = new RealSubject();
     }
     realSubject.Request2();
 }
示例#7
0
 public void Request()
 {
     if (realSubject == null)
     {
         realSubject = new RealSubject();
     }
     PreRequest();
     realSubject.Request();
     PostRequest();
 }
示例#8
0
        public void Request()
        {
            if (this.CheckAccess())
            {
                this._realSubject = new RealSubject();
                this._realSubject.Request();

                this.LogAccess();
            }
        }
示例#9
0
 public void Request()
 {
     if (realSubject == null)
     {
         realSubject = new RealSubject();
     }
     PreRequest();
     realSubject.Request();
     PostRequest();
 }
示例#10
0
        /// <summary>
        /// Make a request.
        /// </summary>
        public override void Request()
        {
            Console.WriteLine("Proxy.Request starting...");

            // this is a virtual proxy - we use lazy instantiation
            if (_realSubject == null)
            {
                _realSubject = new RealSubject();
            }
            _realSubject.Request();
        }
示例#11
0
        public override void Request()
        {
            //Use lazy initialization
            if (realSubject == null) {

                realSubject = new RealSubject();
            }

            else {
                realSubject.Request();
            }
        }
示例#12
0
        static void Main(string[] args)
        {
            ISubject subject = new RealSubject();

            subject.Get();
            subject.Do();

            ISubject pSubject = new ProxySubject();

            pSubject.Get();
            pSubject.Do();

            Console.ReadLine();
        }
示例#13
0
        static void Main(string[] args)
        {
            Client client = new Client();

            Console.WriteLine("Client: Executing client code with a real subject");
            RealSubject realSubject = new RealSubject();

            client.ClientCode(realSubject);

            Console.WriteLine();

            Console.WriteLine("Client: Executing client code with a proxy:");
            Proxy proxy = new Proxy(realSubject);

            client.ClientCode(proxy);
        }
示例#14
0
        static void Main(string[] args)
        {
            try
            {
                {
                    Console.WriteLine("***********Real**************");

                    ISubject subject = new RealSubject();//持有资源 /数据库链接

                    Console.WriteLine("do something else...");
                    Thread.Sleep(500);
                    Console.WriteLine("do something else...");
                    Console.WriteLine("do something else...");
                    Console.WriteLine("do something else...");
                    //subject.GetSomething();
                    subject.DoSomething();
                }
                {
                    Console.WriteLine("***********Proxy1**************");
                    ISubject subject = new ProxySubject();

                    Console.WriteLine("do something else...");
                    Thread.Sleep(500);
                    Console.WriteLine("do something else...");
                    Console.WriteLine("do something else...");
                    Console.WriteLine("do something else...");

                    //subject.GetSomething();//真的需要的时候,才去   持有资源 /数据库链接
                    subject.DoSomething();
                }
                {
                    Console.WriteLine("***********Proxy1**************");
                    ISubject subject = new ProxySubject();

                    subject.GetSomething();
                    subject.DoSomething();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
示例#15
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的代理模式");

                Console.WriteLine("********************");
                {
                    ISubject realSubject = new RealSubject();
                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");
                    realSubject.GetSomething();
                    realSubject.GetSomething();
                    realSubject.DoSomething();
                }
                Console.WriteLine("********************");
                {
                    ISubject proxySubject = new ProxySubject();


                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");
                    Console.WriteLine("干点别的");

                    proxySubject.GetSomething();
                    proxySubject.GetSomething();
                    proxySubject.GetSomething();
                    proxySubject.GetSomething();
                    proxySubject.DoSomething();
                }

                ServiceReference1.SearcherClient client = new ServiceReference1.SearcherClient();
                //client.QueryCommodityPage();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
示例#16
0
 public Proxy()
 {
     realSubject = new RealSubject();
 }
示例#17
0
 public Proxy(RealSubject realSubject)
 {
     _realSubject = realSubject;
 }