原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

[索引页][源码下载]

乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

作者:webabcd





介绍

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。





示例

有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





参考

http://www.dofactory.com/Patterns/PatternAbstract.aspx





OK

[源码下载]

乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)的更多相关文章

  1. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  2. 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...

  3. 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern

    概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...

  4. 设计模式之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式的由来 抽象工厂模式,最开始是为了解决操作系统按钮和窗体风格,而产生的一种设计模式.例如:在windows系统中,我们要用windows设定的按钮和窗体,当我们切换Linux系统时,要 ...

  5. 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释

    抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...

  6. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  7. C#设计模式——抽象工厂模式(Abstract Factory Pattern)

    一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...

  8. 六个创建模式之抽象工厂模式(Abstract Factory Pattern)

    问题: 使用工厂方法模式的主要问题是工厂类过多,每个产品对应一个工厂,不利于维护.因此可以考虑使用一个工厂创建一个产品族. 定义: 提供创建一些列相关或相互依赖的对象实例的接口,这些类可以称为一个产品 ...

  9. 23种设计模式之抽象工厂(Abstract Factory Pattern)

    抽象工厂 当想创建一组密不可分的对象时,工厂方法似乎就不够用了 抽象工厂是应对产品族概念的.应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品.比如,每个汽车公司可能要同时生产轿车.货车. ...

随机推荐

  1. make 2>&1 | tee log.txt之小析

    前言 接触过linux的人,或多或少都会了解一点make 2>&1 | tee log.txt这个命令. 1. make是什么? make是linux下一个非常强大的命令,简单点就是你要 ...

  2. Mod in math

    An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...

  3. 基于S5pv210流媒体server的实现之网络摄像头(by liukun321 咕唧咕唧)

    这里仅介绍流媒体server端的实现思路.及编码注意问题,不会贴代码的详细实现. 直接入正题先介绍一下系统硬件框架: server端连接PC机用VLC播放例如以下图: server端应用程序能够分为图 ...

  4. linux常用系统配置命令汇总

    系统配置及查看信息相关命令 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看C ...

  5. hyper-v 报错 0x80070569

    在Windows8.1Pro版使用过程中,突然出现HYPER-V无法创建虚拟机.显示错误为: 登录失败:未授予用户在此计算机上的请求登录类型.(0x80070569). 回顾起近期通过组策略增强了系统 ...

  6. Android中怎样在应用A中启动或安装应用B

    看到别人做的游戏攻略,想着自己的游戏攻略也加入新的功能,即Android中怎样在应用A中启动或安装应用B.就查了一些资料整理下来. 启动或安装对应的应用的方法: Step1:推断是否安装目标应用.仅仅 ...

  7. TopCoder中插件的用法

    今天弄了一下TopCoder的插件,发现真的很好很强大,插件的下载地址为 : http://community.topcoder.com/tc?module=Static&d1=applet& ...

  8. Android判断应用程序从后台回到前台

    MainActivity如下: package cc.testbackgroundtofront; import java.util.List; import android.app.Activity ...

  9. Jetty:开发指导Handlers

    Rewrite Handler RewriteHandler匹配一个基于该请求的规则集合,然后根据匹配规则的变更请求. 最常见的要求是改写URI.但不限于:规则可以被配置为重定向响应.设置cookie ...

  10. Linux下精确控制时间的函数

    Linux下精确控制时间的函数 在测试程序接口运行时间的时候,常用time,gettimeofday等函数,但是这些函数在程序执行的时候是耗费时间的,如果仅仅测试时间还行,但是如果程序中用到时间控制类 ...