博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
阅读量:6281 次
发布时间:2019-06-22

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




乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
作者:
介绍
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
示例
有Message和MessageModel,Message有一个Insert()方法,该方法的参数是MessageModel。
AbstractMessageModel
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 抽象Message实体类(AbstractProduct) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
abstract 
class AbstractMessageModel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public AbstractMessageModel() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

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 AbstractMessageModel(
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                
/// <summary> 

InBlock.gif                
/// UserId 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
abstract 
string UserId 

InBlock.gif                { 

InBlock.gif                        get; 

InBlock.gif                        set; 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
SqlMessageModel
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SqlMessage实体类(Product) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class SqlMessageModel : AbstractMessageModel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public SqlMessageModel() 

InBlock.gif                        : 
base() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="userId">UserId</param> 

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

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

InBlock.gif                
public SqlMessageModel(
string userId, 
string msg, DateTime pt) 

InBlock.gif                        : 
base(msg, pt) 

InBlock.gif                { 

InBlock.gif                        
this._userId = userId; 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
private 
string _userId; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Message内容 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
override 
string UserId 

InBlock.gif                { 

InBlock.gif                        get { 
return _userId; } 

InBlock.gif                        set { _userId = value; } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
XmlMessageModel
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// XmlMessage实体类(Product) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class XmlMessageModel : AbstractMessageModel 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
public XmlMessageModel() 

InBlock.gif                        : 
base() 

InBlock.gif                { 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="userId">UserId</param> 

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

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

InBlock.gif                
public XmlMessageModel(
string userId, 
string msg, DateTime pt) 

InBlock.gif                        : 
base (msg, pt) 

InBlock.gif                { 

InBlock.gif                        
this._userId = userId; 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
private 
string _userId; 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// Message内容 

InBlock.gif                
/// </summary> 

InBlock.gif                
public 
override 
string UserId 

InBlock.gif                { 

InBlock.gif                        
// 解密算法后的值 

InBlock.gif                        get { 
return _userId; }    

InBlock.gif 

InBlock.gif                        
// 加密算法后的值 

InBlock.gif                        set { _userId = value; } 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
AbstractMessage
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 操作Message抽象类(AbstractProduct) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
abstract 
class AbstractMessage 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="amm">AbstractMessageModel</param> 

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

InBlock.gif                
public 
abstract 
string Insert(AbstractMessageModel amm); 

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.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Sql方式操作Message类(Product) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class SqlMessage : AbstractMessage 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="amm">AbstractMessageModel</param> 

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

InBlock.gif                
public 
override 
string Insert(AbstractMessageModel amm) 

InBlock.gif                { 

InBlock.gif                        
return 
"Sql方式插入Message。帐号:" + amm.UserId    

InBlock.gif                                + 
";内容:" + amm.Message    

InBlock.gif                                + 
";时间:" + amm.PublishTime.ToString(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
XmlMessage
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Xml方式操作Message类(Product) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class XmlMessage : AbstractMessage 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="amm">AbstractMessageModel</param> 

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

InBlock.gif                
public 
override 
string Insert(AbstractMessageModel amm) 

InBlock.gif                { 

InBlock.gif                        
return 
"Xml方式插入Message。帐号:" + amm.UserId 

InBlock.gif                                + 
";内容:" + amm.Message 

InBlock.gif                                + 
";时间:" + amm.PublishTime.ToString(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
AbstractMessageFactory
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 抽象Message工厂(AbstractFactory) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
abstract 
class AbstractMessageFactory 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建MessageModel对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
abstract AbstractMessageModel CreateMessageModel(); 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建Message对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
abstract AbstractMessage CreateMessage(); 

InBlock.gif        } 

InBlock.gif}
 
SqlMessageFactory
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// SqlMessage工厂(ConcreteFactory) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class SqlMessageFactory : AbstractMessageFactory 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建SqlMessageModel对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
override AbstractMessageModel CreateMessageModel() 

InBlock.gif                { 

InBlock.gif                        
return 
new SqlMessageModel(); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建SqlMessage对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
override AbstractMessage CreateMessage() 

InBlock.gif                { 

InBlock.gif                        
return 
new SqlMessage(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
XmlMessageFactory
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// XmlMessage工厂(ConcreteFactory) 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class XmlMessageFactory : AbstractMessageFactory 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建XmlMessageModel对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
override AbstractMessageModel CreateMessageModel() 

InBlock.gif                { 

InBlock.gif                        
return 
new XmlMessageModel(); 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 创建XmlMessage对象 

InBlock.gif                
/// </summary> 

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

InBlock.gif                
public 
override AbstractMessage CreateMessage() 

InBlock.gif                { 

InBlock.gif                        
return 
new XmlMessage(); 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
Message
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
namespace Pattern.AbstractFactory 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// Context类 

InBlock.gif        
/// </summary> 

InBlock.gif        
public 
class Message 

InBlock.gif        { 

InBlock.gif                
private AbstractMessage _insertMessage; 

InBlock.gif                
private AbstractMessageModel _messageModel; 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 构造函数 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="factory">AbstractMessageFactory</param> 

InBlock.gif                
public Message(AbstractMessageFactory factory) 

InBlock.gif                { 

InBlock.gif                        DateTime pt = DateTime.Now; 

InBlock.gif 

InBlock.gif                        _insertMessage = factory.CreateMessage(); 

InBlock.gif                        _messageModel = factory.CreateMessageModel(); 

InBlock.gif 

InBlock.gif                } 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 插入Message 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <param name="userId">UserId</param> 

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

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

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

InBlock.gif                
public 
string Insert(
string userId, 
string msg, DateTime pt) 

InBlock.gif                { 

InBlock.gif                        _messageModel.UserId = userId; 

InBlock.gif                        _messageModel.Message = msg; 

InBlock.gif                        _messageModel.PublishTime = pt; 

InBlock.gif 

InBlock.gif                        
return _insertMessage.Insert(_messageModel); 

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.AbstractFactory; 

InBlock.gif 

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

InBlock.gif

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

InBlock.gif        { 

InBlock.gif                AbstractMessageFactory amf = 
new SqlMessageFactory(); 

InBlock.gif 

InBlock.gif                Message m = 
new Message(amf); 

InBlock.gif 

InBlock.gif                Response.Write(m.Insert(
"admin"
"Sql方式", DateTime.Now)); 

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

InBlock.gif 

InBlock.gif                amf = 
new XmlMessageFactory(); 

InBlock.gif 

InBlock.gif                m = 
new Message(amf); 

InBlock.gif 

InBlock.gif                Response.Write(m.Insert(
"user"
"Xml方式", DateTime.Now)); 

InBlock.gif        } 

InBlock.gif}
 
 
运行结果
Sql方式插入Message。帐号:admin;内容:Sql方式;时间:2007-5-3 22:04:56
Xml方式插入Message。帐号:user;内容:Xml方式;时间:2007-5-3 22:04:56 


参考



OK
     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344482
,如需转载请自行联系原作者
你可能感兴趣的文章
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>
2019年BAT面试通关宝典:数据结构+JVM+并发编程+分布式...
查看>>
CMarkupSTL 读写xml文件
查看>>
洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur
查看>>
codevs——1842 递归第一次
查看>>
转给正在找工作的应届生 2013年各大小IT公司待遇
查看>>
某源码thread,socket研究4
查看>>
tomcat日志脚本
查看>>
mysql修改表、字段、库的字符集
查看>>
设置Serv-U FTP 支持被动模式连接 ,530错误等解决办法集锦
查看>>
zabbix3.0安装教程(环境)
查看>>
关于linux系统下批量修改文件名和后缀
查看>>
我和linux的第二十四天
查看>>