惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
小众软件
小众软件
GbyAI
GbyAI
B
Blog RSS Feed
月光博客
月光博客
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
博客园_首页
G
Google Developers Blog

博客园 - 走到天亮

设计模式之“适配器模式” 设计模式之“门面模式” 设计模式之“抽象工厂模式” 设计模式之“单例模式” 设计模式之“策略模式” 《C# to IL》第三章 选择和循环 《C# to IL》第二章 IL基础 《C# to IL》第一章 IL入门 淘宝下单高并发解决方案(转载) java linux 配置环境 Spring Aop之(二)--Aop 切面声明和通知 Spring aop Spring RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor Spring BeanNameAutoProxyCreator 与 ProxyFactoryBean CentOS的IP配置专题 Spring Bean属性绑定Bean返回值 【阿里的感悟】质量该如何做? .(转载) Ubuntu开机自动启动script(2) Ubuntu开机自动启动Script
设计模式之“代理模式”
走到天亮 · 2013-07-17 · via 博客园 - 走到天亮

代理(Proxy)模式给某一个对象提供一个代理,并由代理对象控制对原对象的引用。

代理模式的英文叫做Proxy或Surrogate,中文都可译成"代理"。所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动。在一些情况下,一个客户不想或者不能够直接引用一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。

类图:

实例:

代理执行远程方法

 public interface IMath
    {
        // Methods
        double Add(double x, double y);
        double Sub(double x, double y);

    }
   public class Math : MarshalByRefObject, IMath
    {
        // Methods
        public double Add(double x, double y) { return x + y; }
        public double Sub(double x, double y) { return x - y; }

    }
   public class MathProxy : IMath
    {
        // Fields
        Math math;

        // Constructors
        public MathProxy()
        {
            // Create Math instance in a different AppDomain
            AppDomain ad = System.AppDomain.CreateDomain("MathDomain", null, null);
            ObjectHandle o = ad.CreateInstance("TestDesgine", "TestDesgine.Math", false,
              System.Reflection.BindingFlags.CreateInstance, null, null, null, null, null);
            math = (Math)o.Unwrap();
        }

        // Methods
        public double Add(double x, double y)
        {
            return math.Add(x, y);
        }
        public double Sub(double x, double y)
        {
            return math.Sub(x, y);
        }
      
    }