博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)

Posted on 2007-05-04 22:39  webabcd  阅读(6727)  评论(19编辑  收藏
[索引页]
[源码下载]


乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)


作者:webabcd


介绍
定义一个用于创建对象的接口,让子类决定将哪一个类实例化。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


OK
[源码下载]