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

推荐订阅源

T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
博客园 - Franky
The Cloudflare Blog
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
B
Blog
Y
Y Combinator Blog
V
V2EX
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
博客园 - 司徒正美
IT之家
IT之家
G
Google Developers Blog
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(2) Proxy 代理模式
Tristan(GuoZhijian) · 2008-02-25 · via 博客园 - Tristan(GuoZhijian)

VS 2008

代理模式使得我们可以通过引入一个代理对象来控制另一对象的创建及访问。

1. 模式静态类图

2. 应用

    现在有一个用于应用程序写文本日志的组件,创建这个组件的一个实例的时候,需要告诉它当前应用程序的名称,组件根据不同的应用程序做不同的处理(如,不同的应用程序写文本日志到不同文件夹),现在,我建立了一个应用程序,使用这个组件来写文本日志,但是如果每次写文本日志都要告诉它应用程序名,那不是太麻烦了,所以引入一个代理类,来负责该组件的创建。

ITextLog

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Proxy.BBL {
    
interface ITextLog {
        
void Write(string message);
    }

}

TextLog

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Proxy.BBL {
    
class TextLog : ITextLog {

        
private string appName = string.Empty;

        
public TextLog(string appName) {
            
this.appName = appName;
        }



        
ITextLog Members
    }

}

TextLogProxy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Proxy.BBL {
    
class TextLogProxy : ITextLog {

        ITextLog textLog 
= null;

        
private ITextLog GetTextLog() {
            
if (textLog == null{
                textLog 
= new TextLog("BoroughGridService");
            }

            
return textLog;
        }


        
ITextLog Members
    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Proxy.BBL;

namespace DesignPattern.Proxy {
    
class Program {
        
static void Main(string[] args) {
            ITextLog textLog 
= new TextLogProxy();
            textLog.Write(
"this is a message");
        }

    }

}