程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 設計模式(C#) - 抽象工廠模式(Abstract Factory Pattern)

設計模式(C#) - 抽象工廠模式(Abstract Factory Pattern)

編輯:關於C#

返回“設計模式(C#)系列文章索引”

介紹

提供一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。

示例

有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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved