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

設計模式(C#) - 工廠方法模式(Factory Method Pattern)

編輯:關於C#

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

介紹

定義一個用於創建對象的接口,讓子類決定將哪一個類實例化。Factory Method使一個類的實例化延遲到其子類。

示例

有SqlMessage和XmlMessage,用Factory Method來實例化它們。

MessageModel

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// Message實體類
  /// </summary>
  public class MessageModel
  {
    /**//// <summary>
    /// 構造函數
    /// </summary>
    /// <param name="msg">Message內容</param>
    /// <param name="pt">Message發布時間</param>
    public MessageModel(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; }
    }
  }
}

AbstractMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// 操作Message抽象類(Product)
  /// </summary>
  public abstract class AbstractMessage
  {
    /**//// <summary>
    /// 獲取Message
    /// </summary>
    /// <returns></returns>
    public abstract List<MessageModel> Get();

    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public abstract bool Insert(MessageModel mm);
  }
}

SqlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// Sql方式操作Message(ConcreteProduct)
  /// </summary>
  public class SqlMessage : AbstractMessage
  {
    /**//// <summary>
    /// 獲取Message
    /// </summary>
    /// <returns></returns>
    public override List<MessageModel> Get()
    {
      List<MessageModel> l = new List<MessageModel>();
      l.Add(new MessageModel("SQL方式獲取Message", DateTime.Now));

      return l;
    }

    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public override bool Insert(MessageModel mm)
    {
      // 代碼略
      return true;
    }
  }
}

XmlMessage

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// Xml方式操作Message(ConcreteProduct)
  /// </summary>
  public class XmlMessage : AbstractMessage
  {
    /**//// <summary>
    /// 獲取Message
    /// </summary>
    /// <returns></returns>
    public override List<MessageModel> Get()
    {
      List<MessageModel> l = new List<MessageModel>();
      l.Add(new MessageModel("XML方式獲取Message", DateTime.Now));

      return l;
    }

    /**//// <summary>
    /// 插入Message
    /// </summary>
    /// <param name="mm">Message實體對象</param>
    /// <returns></returns>
    public override bool Insert(MessageModel mm)
    {
      // 代碼略
      return true;
    }
  }
}

AbstractMessageFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// 抽象Message工廠(Creator)
  /// </summary>
  public abstract class AbstractMessageFactory
  {
    // An Operation

    /**//// <summary>
    /// 工廠方法
    /// </summary>
    /// <returns>AbstractMessage</returns>
    public abstract AbstractMessage CreateMessage();
  }
}

SqlMessageFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// SqlMessage工廠(ConcreteCreator)
  /// </summary>
  public class SqlMessageFactory : AbstractMessageFactory
  {
    /**//// <summary>
    /// 實現工廠方法,返回SqlMessage對象
    /// </summary>
    /// <returns></returns>
    public override AbstractMessage CreateMessage()
    {
      return new SqlMessage();
    }
  }
}

XmlMessageFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace Pattern.FactoryMethod
{
  /**//// <summary>
  /// XmlMessage工廠(ConcreteCreator)
  /// </summary>
  public class XmlMessageFactory : AbstractMessageFactory
  {
    /**//// <summary>
    /// 實現工廠方法,返回XmlMessage對象
    /// </summary>
    /// <returns></returns>
    public override AbstractMessage CreateMessage()
    {
      return new XmlMessage();
    }
  }
}

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

public partial class FactoryMethod : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    AbstractMessageFactory amf = new SqlMessageFactory();
    AbstractMessage am = amf.CreateMessage();

    Response.Write(am.Insert(new MessageModel("插入", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(am.Get()[0].Message + " " + am.Get()[0].PublishTime.ToString());
    Response.Write("<br />");

    amf = new XmlMessageFactory();
    am = amf.CreateMessage();

    Response.Write(am.Insert(new MessageModel("插入", DateTime.Now)));
    Response.Write("<br />");
    Response.Write(am.Get()[0].Message + " " + am.Get()[0].PublishTime.ToString());
  }
}

運行結果

True

SQL方式獲取Message 2007-5-4 22:34:39

True

XML方式獲取Message 2007-5-4 22:34:39

參考

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

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