zoukankan      html  css  js  c++  java
  • 大话设计模式-代理模式(7)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        interface IGiveGift
        {
            void GiveDolls();
            void GiveFlowers();
            void GvieChoolate();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class Proxy:IGiveGift
        {
            Pursuit gg;
    
            public Proxy(SchoolGirl mm)
            {
                gg = new Pursuit(mm);
            }
    
            public void GiveDolls()
            {
                gg.GiveDolls();
            }
    
            public void GiveFlowers()
            {
                gg.GiveFlowers();
            }
    
            public void GvieChoolate()
            {
                gg.GvieChoolate();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        abstract class Subject
        {
            public abstract void Request();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class Pursuit:IGiveGift
        {
            SchoolGirl mm;
    
            public Pursuit(SchoolGirl mm) 
            {
                this.mm = mm;
            }
    
            public void GiveDolls()
            {
                Console.WriteLine("{0}送你洋娃娃!",mm.Name);
            }
    
            public void GiveFlowers()
            {
                Console.WriteLine("{0}送你鲜花!",mm.Name);
            }
    
            public void GvieChoolate()
            {
                Console.WriteLine("{0}送你巧克力!",mm.Name);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class RealSubject:Subject
        {
            public override void Request()
            {
                Console.WriteLine("真实的请求!");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class SubProxy:Subject
        {
            RealSubject realSubject;
    
            public override void Request()
            {
                if (realSubject == null)
                {
                    realSubject = new RealSubject();
                }
                realSubject.Request();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class SchoolGirl
        {
            private string name;
    
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ProxyFactory
    {
        class Program
        {
            static void Main(string[] args)
            {
                SchoolGirl mm = new SchoolGirl();
                mm.Name = "李娇娇";
    
                Proxy daili = new Proxy(mm);
                daili.GiveDolls();
                daili.GiveFlowers();
                daili.GvieChoolate();
    
    
                SubProxy subProxy = new SubProxy();
                subProxy.Request();
    
                Console.ReadLine();
            }
        }
    }

    代理模式

    代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问.

    优点

    (1).职责清晰,真实的角色就是实现实际的业务逻辑,不用关心其他非本职责的事务,通过后期的代理完成一件完成事务,附带的结果就是编程简洁清晰。
    (2).代理对象可以在客户端和目标对象之间起到中介的作用,这样起到了中介的作用和保护了目标对象的作用。
    (3).高扩展性

    模式结构

    一个是真正的你要访问的对象(目标类),一个是代理对象,真正对象与代理对象实现同一个接口,先访问代理类再访问真正要访问的对象。

  • 相关阅读:
    HAProxy、Keepalived 在 Ocatvia 的应用实现与分析
    Octavia 的 HTTPS 与自建、签发 CA 证书
    Octavia 创建 loadbalancer 的实现与分析
    OpenStack Rally 质量评估与自动化测试利器
    自建 CA 中心并签发 CA 证书
    Failed building wheel for netifaces
    通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
    OpenStack Placement Project
    我们建了一个 Golang 硬核技术交流群(内含视频福利)
    没有图形界面的软件有什么用?
  • 原文地址:https://www.cnblogs.com/rinack/p/5275758.html
Copyright © 2011-2022 走看看