博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
阅读量:6508 次
发布时间:2019-06-24

本文共 3176 字,大约阅读时间需要 10 分钟。




乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
作者:
介绍
为其他对象提供一个代理以控制对这个对象的访问。
示例
有一个Message实体类,某对象对它的操作有Insert()和Get()方法,用一个代理来控制对这个对象的访问。
 
MessageModel
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.Proxy 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Message实体类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class MessageModel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="msg">Message内容</param> 

InBlock.gif                
/// <param name="pt">Message发布时间</param> 

InBlock.gif                
public MessageModel(
string msg, DateTime pt) 

InBlock.gif                { 

InBlock.gif                        
this._message = msg; 

InBlock.gif                        
this._publishTime = pt; 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
private 
string _message; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Message内容 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
string Message 

InBlock.gif                { 

InBlock.gif                        get { 
return _message; } 

InBlock.gif                        set { _message = value; } 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
private DateTime _publishTime; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Message发布时间 

InBlock.gif                
/// </summary> 

InBlock.gif                
public DateTime PublishTime 

InBlock.gif                { 

InBlock.gif                        get { 
return _publishTime; } 

InBlock.gif                        set { _publishTime = value; } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
IMessage
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.Proxy 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 对Message操作的接口 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
interface IMessage 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                List<MessageModel> Get(); 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="mm">Message实体对象</param> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
bool Insert(MessageModel mm); 

InBlock.gif        } 

InBlock.gif}
 
SqlMessage
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.Proxy 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Sql方式操作Message 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class SqlMessage : IMessage 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public List<MessageModel> Get() 

InBlock.gif                { 

InBlock.gif                        List<MessageModel> l = 
new List<MessageModel>(); 

InBlock.gif                        l.Add(
new MessageModel(
"SQL方式获取Message", DateTime.Now)); 

InBlock.gif 

InBlock.gif                        
return l; 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="mm">Message实体对象</param> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public 
bool Insert(MessageModel mm) 

InBlock.gif                { 

InBlock.gif                        
// 代码略 

InBlock.gif                        
return 
true

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
MessageProxy
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.Proxy 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 代理类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class MessageProxy : IMessage 

InBlock.gif        { 

InBlock.gif                SqlMessage _sqlMessage; 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public MessageProxy() 

InBlock.gif                { 

InBlock.gif                        _sqlMessage = 
new SqlMessage(); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public List<MessageModel> Get() 

InBlock.gif                { 

InBlock.gif                        
return _sqlMessage.Get(); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="mm">Message实体对象</param> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public 
bool Insert(MessageModel mm) 

InBlock.gif                { 

InBlock.gif                        
return _sqlMessage.Insert(mm); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
client
InBlock.gif
using System; 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Configuration; 

InBlock.gif
using System.Collections; 

InBlock.gif
using System.Web; 

InBlock.gif
using System.Web.Security; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI.WebControls.WebParts; 

InBlock.gif
using System.Web.UI.HtmlControls; 

InBlock.gif 

InBlock.gif
using Pattern.Proxy; 

InBlock.gif 

InBlock.gif
public partial 
class Proxy : System.Web.UI.Page 

InBlock.gif

InBlock.gif        
protected 
void Page_Load(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif                MessageProxy m = 
new MessageProxy(); 

InBlock.gif                Response.Write(m.Insert(
new MessageModel(
"插入", DateTime.Now))); 

InBlock.gif                Response.Write(
"<br />"); 

InBlock.gif                Response.Write(m.Get()[0].Message + 
" " + m.Get()[0].PublishTime.ToString()); 

InBlock.gif        } 

InBlock.gif}
 
 
运行结果
True
SQL方式获取Message 2007-4-1 16:31:24 


参考



OK
     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344541
,如需转载请自行联系原作者
你可能感兴趣的文章
css属性的继承、初识值、计算值、当前值、应用值
查看>>
【159天】尚学堂高琪Java300集视频精华笔记(128)
查看>>
关于 Linux 进程的 UID、EUID、GID 和 EGID
查看>>
【162天】黑马程序员27天视频学习笔记【Day02-上】
查看>>
ComponentOne 2017 V2版本正式发布
查看>>
【mysql】环境安装、服务启动、密码设置
查看>>
Rancher如何对接Ceph-RBD块存储
查看>>
微信小程序实战练习(仿五洲到家微信版)
查看>>
Django 博客开发教程 8 - 博客文章详情页
查看>>
JS+CSS实现数字滚动
查看>>
Node + FFmpeg 实现Canvas动画导出视频
查看>>
基于Vue2全家桶的移动端AppDEMO实现
查看>>
FineReport中如何实现自动滚屏效果
查看>>
KubeEdge:开源的Kubernetes原生边缘计算框架
查看>>
为了使用好Apache Flink,Yelp实现了一个连接算法
查看>>
超百万观众同场看直播秒开不卡顿,快手是如何做到的?|首次披露
查看>>
webpack+react项目初体验——记录我的webpack环境配置
查看>>
微软宣布公开预览其内容分发网络
查看>>
dotTrace 6.1帮你理解SQL查询如何影响应用性能
查看>>
LLVM 4中将加入新的LLVM链接器LLD
查看>>