二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式(Abstract Factory Pattern)
介绍
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
示例
有Message和MessageModel,Message有一个Insert()方法,该方法的参数是MessageModel。

AbstractMessageModel
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// 抽象Message实体类(AbstractProduct)
/// </summary>
public abstract class AbstractMessageModel
{
/// <summary>
/// 构造函数
/// </summary>
public AbstractMessageModel()
{ } /// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public AbstractMessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
} private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
} private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
} /// <summary>
/// UserId
/// </summary>
public abstract string UserId
{
get;
set;
}
}
}
SqlMessageModel
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// SqlMessage实体类(Product)
/// </summary>
public class SqlMessageModel : AbstractMessageModel
{
/// <summary>
/// 构造函数
/// </summary>
public SqlMessageModel()
: base()
{ } /// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public SqlMessageModel(string userId, string msg, DateTime pt)
: base(msg, pt)
{
this._userId = userId;
} private string _userId;
/// <summary>
/// Message内容
/// </summary>
public override string UserId
{
get { return _userId; }
set { _userId = value; }
}
}
}
XmlMessageModel
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// XmlMessage实体类(Product)
/// </summary>
public class XmlMessageModel : AbstractMessageModel
{
/// <summary>
/// 构造函数
/// </summary>
public XmlMessageModel()
: base()
{ } /// <summary>
/// 构造函数
/// </summary>
/// <param name="userId">UserId</param>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public XmlMessageModel(string userId, string msg, DateTime pt)
: base (msg, pt)
{
this._userId = userId;
} private string _userId;
/// <summary>
/// Message内容
/// </summary>
public override string UserId
{
// 解密算法后的值
get { return _userId; } // 加密算法后的值
set { _userId = value; }
}
}
}
AbstractMessage
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// 操作Message抽象类(AbstractProduct)
/// </summary>
public abstract class AbstractMessage
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="amm">AbstractMessageModel</param>
/// <returns></returns>
public abstract string Insert(AbstractMessageModel amm);
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// Sql方式操作Message类(Product)
/// </summary>
public class SqlMessage : AbstractMessage
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="amm">AbstractMessageModel</param>
/// <returns></returns>
public override string Insert(AbstractMessageModel amm)
{
return "Sql方式插入Message。帐号:" + amm.UserId
+ ";内容:" + amm.Message
+ ";时间:" + amm.PublishTime.ToString();
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// Xml方式操作Message类(Product)
/// </summary>
public class XmlMessage : AbstractMessage
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="amm">AbstractMessageModel</param>
/// <returns></returns>
public override string Insert(AbstractMessageModel amm)
{
return "Xml方式插入Message。帐号:" + amm.UserId
+ ";内容:" + amm.Message
+ ";时间:" + amm.PublishTime.ToString();
}
}
}
AbstractMessageFactory
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// 抽象Message工厂(AbstractFactory)
/// </summary>
public abstract class AbstractMessageFactory
{
/// <summary>
/// 创建MessageModel对象
/// </summary>
/// <returns></returns>
public abstract AbstractMessageModel CreateMessageModel(); /// <summary>
/// 创建Message对象
/// </summary>
/// <returns></returns>
public abstract AbstractMessage CreateMessage();
}
}
SqlMessageFactory
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// SqlMessage工厂(ConcreteFactory)
/// </summary>
public class SqlMessageFactory : AbstractMessageFactory
{
/// <summary>
/// 创建SqlMessageModel对象
/// </summary>
/// <returns></returns>
public override AbstractMessageModel CreateMessageModel()
{
return new SqlMessageModel();
} /// <summary>
/// 创建SqlMessage对象
/// </summary>
/// <returns></returns>
public override AbstractMessage CreateMessage()
{
return new SqlMessage();
}
}
}
XmlMessageFactory
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// XmlMessage工厂(ConcreteFactory)
/// </summary>
public class XmlMessageFactory : AbstractMessageFactory
{
/// <summary>
/// 创建XmlMessageModel对象
/// </summary>
/// <returns></returns>
public override AbstractMessageModel CreateMessageModel()
{
return new XmlMessageModel();
} /// <summary>
/// 创建XmlMessage对象
/// </summary>
/// <returns></returns>
public override AbstractMessage CreateMessage()
{
return new XmlMessage();
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.AbstractFactory
{
/// <summary>
/// Context类
/// </summary>
public class Message
{
private AbstractMessage _insertMessage;
private AbstractMessageModel _messageModel; /// <summary>
/// 构造函数
/// </summary>
/// <param name="factory">AbstractMessageFactory</param>
public Message(AbstractMessageFactory factory)
{
DateTime pt = DateTime.Now; _insertMessage = factory.CreateMessage();
_messageModel = factory.CreateMessageModel(); } /// <summary>
/// 插入Message
/// </summary>
/// <param name="userId">UserId</param>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
/// <returns></returns>
public string Insert(string userId, string msg, DateTime pt)
{
_messageModel.UserId = userId;
_messageModel.Message = msg;
_messageModel.PublishTime = pt; return _insertMessage.Insert(_messageModel);
}
}
}
Client
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using Pattern.AbstractFactory; public partial class AbstractFactory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AbstractMessageFactory amf = new SqlMessageFactory(); Message m = new Message(amf); Response.Write(m.Insert("admin", "Sql方式", DateTime.Now));
Response.Write("<br />"); amf = new XmlMessageFactory(); m = new Message(amf); Response.Write(m.Insert("user", "Xml方式", DateTime.Now));
}
}
运行结果
Sql方式插入Message。帐号:admin;内容:Sql方式;时间:2007-5-3 22:04:56
Xml方式插入Message。帐号:user;内容:Xml方式;时间:2007-5-3 22:04:56
二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)的更多相关文章
- 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释
抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...
- 二十四种设计模式:模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...
- 【设计模式】抽象工厂模式 Abstract Factory Pattern
简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...
- 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern
概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...
- 设计模式之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式的由来 抽象工厂模式,最开始是为了解决操作系统按钮和窗体风格,而产生的一种设计模式.例如:在windows系统中,我们要用windows设定的按钮和窗体,当我们切换Linux系统时,要 ...
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...
- Objective-C设计模式——抽象工厂模式Abstract Factory(对象创建)
抽象工厂模式 理解了工厂方法模式,其实抽象工厂和工厂方法模式有很多的相似之处.抽象工厂同样是分离客户端对象的创建和逻辑代码的,但是抽象工厂往往是产生一组数据而不单单是产生一个产品. 抽象工厂提供一个创 ...
随机推荐
- [ python ] 下划线的意义和一些特殊方法
Python用下划线 Python用下划线为变量前缀和后缀制定特殊变量 _xxx 不能用 'from module import *' 导入__xxx__ 系统定义名字__xxx 类中的私有变量名 核 ...
- Struts2学习笔记03 之 Result组件
二.Result原理 1.stream 2.redirectAction 3.Json
- redis之(十七)自己实现redis的cluster集群环境的搭建
[一]创建不同节点的配置文件和目录.并将配置文件中的port,cluster-enable,daemonize项做修改. --->port:修改成redis实例对应的端口号 --->clu ...
- 微信小程序 - 分包加载
小程序开发大家都知道,对主包的大小进行了限制,从最初的1M变成了现再的2M,一般情况下是够用了:但是偶尔可能会出现超出2M的可能,我们可以对小程序进行分包加载. 1.小程序分包加载 a. 某些情况下, ...
- Java学习笔记(十三)——通过Netbeans开发环境生成oracle数据库中表的对应hibernate映射文件
[前面的话] 身体慢慢已经快好了,感觉真好,哈哈. 这篇文章要通过Hibernate对数据库进行操作,而Netbeans可以直接通过数据库逆向生成对应的映射文件.基础文章,选择性阅读. [步骤] 1. ...
- nginx的http负载均衡
注意:nginx自带的http服务后端检测有缺陷,无法根据状态码来检测,建议使用tengine的nginx_upstream_check_module来实现后端服务的http健康状态检测 (1)负载均 ...
- 7/31 CSU-ACM2018暑期训练7-贪心
比赛链接 A-CSU - 1588 现在有n堆果子,第i堆有ai个果子.现在要把这些果子合并成一堆,每次合并的代价是两堆果子的总果子数.求合并所有果子的最小代价. Input 第一行包含一个整数T(T ...
- POJ2828 Buy Tickets [树状数组,二分答案]
题目传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22611 Accepted: 110 ...
- socket编程(TCP)
1.模型:(图片来自百度百科) 服务端: 1)创建socket对象 int socket(int domain, int type, int protocol) domain:即协议域,又称为协议族( ...
- Eclipse 主题(修改背景色)