乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

简介:
[索引页]
[源码下载]


乐在其中设计模式(C#) - 提供者模式(Provider Pattern)


作者: webabcd


介绍
为一个API进行定义和实现的分离。


示例
有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。根据配置文件中的配置来决定数据持久化方案是使用SqlServer数据库还是Xml文件。
 
MessageModel
InBlock.gif using System; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
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}
 
MessageProvider
InBlock.gif using System.Configuration.Provider; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 操作Message抽象类 
InBlock.gif         /// </summary> 
InBlock.gif         public  abstract  class MessageProvider : ProviderBase 
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  abstract  bool Insert(MessageModel mm); 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 获得Message 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  abstract List<MessageModel> Get(); 
InBlock.gif        } 
InBlock.gif}
 
SqlMessageProvider
InBlock.gif using System; 
InBlock.gif using System.Collections.Specialized; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif 
InBlock.gif using System.Configuration.Provider; 
InBlock.gif using System.Configuration; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Sql方式操作Message 
InBlock.gif         /// </summary> 
InBlock.gif         public  class SqlMessageProvider : MessageProvider 
InBlock.gif        { 
InBlock.gif                 private  string _connectionString; 
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  override  bool Insert(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                         // 代码略 
InBlock.gif                         return  true
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 获取Message 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  override List<MessageModel> Get() 
InBlock.gif                { 
InBlock.gif                        List<MessageModel> l =  new List<MessageModel>(); 
InBlock.gif                        l.Add( new MessageModel( "SQL方式,连接字符串是" +  this._connectionString, DateTime.Now)); 
InBlock.gif 
InBlock.gif                         return l; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 初始化提供程序。 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">该提供程序的友好名称。</param> 
InBlock.gif                 /// <param name="config">名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。</param> 
InBlock.gif                 public  override  void Initialize( string name, NameValueCollection config) 
InBlock.gif                { 
InBlock.gif                         if ( string.IsNullOrEmpty(name)) 
InBlock.gif                                name =  "MessageProvider"
InBlock.gif 
InBlock.gif                         if ( null == config) 
InBlock.gif                                 throw  new ArgumentException( "config参数不能为null"); 
InBlock.gif 
InBlock.gif                         if ( string.IsNullOrEmpty(config[ "description"])) 
InBlock.gif                        { 
InBlock.gif                                config.Remove( "description"); 
InBlock.gif                                config.Add( "description""SqlServer操作Message"); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         base.Initialize(name, config); 
InBlock.gif 
InBlock.gif                         string temp = config[ "connectionStringName"]; 
InBlock.gif                         if (temp ==  null || temp.Length < 1) 
InBlock.gif                                 throw  new ProviderException( "connectionStringName属性缺少或为空"); 
InBlock.gif 
InBlock.gif                        _connectionString = ConfigurationManager.ConnectionStrings[temp].ConnectionString; 
InBlock.gif                         if (_connectionString ==  null || _connectionString.Length < 1) 
InBlock.gif                        { 
InBlock.gif                                 throw  new ProviderException( "没找到'" + temp +  "'所指的连接字符串,或所指连接字符串为空"); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        config.Remove( "connectionStringName"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
XmlMessageProvider
InBlock.gif using System; 
InBlock.gif using System.Collections.Specialized; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif 
InBlock.gif using System.Configuration.Provider; 
InBlock.gif using System.Configuration; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Xmll方式操作Message 
InBlock.gif         /// </summary> 
InBlock.gif         public  class XmlMessageProvider : MessageProvider 
InBlock.gif        { 
InBlock.gif                 private  string _connectionString; 
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  override  bool Insert(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                         // 代码略 
InBlock.gif                         return  true
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 获取Message 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  override List<MessageModel> Get() 
InBlock.gif                { 
InBlock.gif                        List<MessageModel> l =  new List<MessageModel>(); 
InBlock.gif                        l.Add( new MessageModel( "XML方式,连接字符串是" +  this._connectionString, DateTime.Now)); 
InBlock.gif 
InBlock.gif                         return l; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 初始化提供程序。 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">该提供程序的友好名称。</param> 
InBlock.gif                 /// <param name="config">名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。</param> 
InBlock.gif                 public  override  void Initialize( string name, NameValueCollection config) 
InBlock.gif                { 
InBlock.gif                         if ( string.IsNullOrEmpty(name)) 
InBlock.gif                                name =  "MessageProvider"
InBlock.gif 
InBlock.gif                         if ( null == config) 
InBlock.gif                                 throw  new ArgumentException( "config参数不能为null"); 
InBlock.gif 
InBlock.gif                         if ( string.IsNullOrEmpty(config[ "description"])) 
InBlock.gif                        { 
InBlock.gif                                config.Remove( "description"); 
InBlock.gif                                config.Add( "description""XML操作Message"); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                         base.Initialize(name, config); 
InBlock.gif 
InBlock.gif                         string temp = config[ "connectionStringName"]; 
InBlock.gif                         if (temp ==  null || temp.Length < 1) 
InBlock.gif                                 throw  new ProviderException( "connectionStringName属性缺少或为空"); 
InBlock.gif 
InBlock.gif                        _connectionString = ConfigurationManager.ConnectionStrings[temp].ConnectionString; 
InBlock.gif                         if (_connectionString ==  null || _connectionString.Length < 1) 
InBlock.gif                        { 
InBlock.gif                                 throw  new ProviderException( "没找到'" + temp +  "'所指的连接字符串,或所指连接字符串为空"); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        config.Remove( "connectionStringName"); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
 
 




     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344596,如需转载请自行联系原作者

相关文章
|
17天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
1月前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
3月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPISDK设置软件触发模式(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPISDK设置软件触发模式(C#)
40 1
|
3月前
|
设计模式 存储 算法
Java 设计模式最佳实践:三、行为模式
Java 设计模式最佳实践:三、行为模式
22 0
|
2月前
|
设计模式 前端开发 JavaScript
观察者模式 vs 发布-订阅模式:两种设计模式的对决!
欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚开始学习前端的读者们打造的。无论你是初学者还是有一些基础的开发者,我们都会在这里为你提供一个系统而又亲切的学习平台。我们以问答形式更新,为大家呈现精选的前端知识点和最佳实践。通过深入浅出的解释概念,并提供实际案例和练习,让你逐步建立起一个扎实的基础。无论是HTML、CSS、JavaScript还是最新的前端框架和工具,我们都将为你提供丰富的内容和实用技巧,帮助你更好地理解并运用前端开发中的各种技术。
|
13天前
|
设计模式 Java 数据库
小谈设计模式(2)—简单工厂模式
小谈设计模式(2)—简单工厂模式
|
1天前
|
设计模式 存储 JavaScript
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
|
1天前
|
设计模式 Java Go
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
|
3天前
|
设计模式
设计模式(一)简单工厂模式
设计模式(一)简单工厂模式
12 0
|
11天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
16 1