对数据库数据操作,工厂方法设计模式(Factory Method)

在开发asp.net程序时,用户的数据将保存至数据库,程序与数据库之间的数据操作,会涉及到往数据库添加数据,编辑数据,更新数据和删除数据等。

本次Insus.NET介绍的是工厂方法,因此我们会从这几个操作入手。介绍工厂方法设计模式,得先从简单工厂设计模式(Simple Factory)开始,
简单工厂,有一个操作类,这个类有一个virtual方法,好让其它子类继承与重写:

对数据库数据操作,工厂方法设计模式(Factory Method) Operation
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for Operation

/// </summary>

namespace Insus.NET

{

    public class Operation

    {

        public virtual void Processing()

        {

        

        }

    }

}


数据添加类:

对数据库数据操作,工厂方法设计模式(Factory Method) OperationAdd
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for OperationAdd

/// </summary>

namespace Insus.NET

{

    public class OperationAdd : Operation

    {

        public override void Processing()

        {

            HttpContext.Current.Response.Write("数据添加操作。");

        }

    }

}


数据编辑类:

对数据库数据操作,工厂方法设计模式(Factory Method) OperationEdit
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for OperationEdit

/// </summary>

namespace Insus.NET

{

    public class OperationEdit : Operation 

    {

        public override void Processing()

        {

            HttpContext.Current.Response.Write("数据编辑操作。");

        }

    }

}


数据更新类:

对数据库数据操作,工厂方法设计模式(Factory Method) OperationUpdate
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for OperationUpdate

/// </summary>

namespace Insus.NET

{

    public class OperationUpdate : Operation 

    {

        public override void Processing()

        {

            HttpContext.Current.Response.Write("数据更新操作。");

        }

    }

}


数据删除类:

对数据库数据操作,工厂方法设计模式(Factory Method) OperationDelete
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for OperationDelete

/// </summary>

namespace Insus.NET

{

    public class OperationDelete : Operation 

    {

        public override void Processing()

        {

            HttpContext.Current.Response.Write("数据删除操作。");

        }

    }

}


操作工厂类

对数据库数据操作,工厂方法设计模式(Factory Method) OperationFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for OperationFactory

/// </summary>

namespace Insus.NET

{

    public class OperationFactory

    {

        public static Operation CreateOperate(string operate)

        {

            Operation objOperation = null;



            switch (operate)

            { 

                case "Add":

                    objOperation = new OperationAdd();

                    break;

                case "Edit":

                    objOperation = new OperationEdit();

                    break;

                case "Update":

                    objOperation = new OperationUpdate();

                    break;

                case "Delete":

                    objOperation = new OperationDelete();

                    break;

            }



            return objOperation;

        }

    }

}

 

OK,简单工厂完成,我们可以由简单工厂设计模式(Simple Factory)演变为工厂方法设计模式(Factory Method)。 

工厂方法设计模式,是定义一个用于创建对象的接口,让子类决定实例化哪一个类。

首先创建一个工厂接口:

对数据库数据操作,工厂方法设计模式(Factory Method) IOperationFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for IOperationFactory

/// </summary>

namespace Insus.NET

{

    public interface IOperationFactory

    {

        Operation CreateOperation();

    }

}


下面分别是为添加,编辑,更新和删除各创建一个具体的工厂,并实现上面刚刚建好的工厂接口:
添加工厂类:

对数据库数据操作,工厂方法设计模式(Factory Method) AddFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for AddFactory

/// </summary>

namespace Insus.NET

{

    public class AddFactory : IOperationFactory 

    {

        public Operation CreateOperation()

        {

          return new OperationAdd();

        }

    }

}


编辑工厂类:

对数据库数据操作,工厂方法设计模式(Factory Method) EditFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for EditFactory

/// </summary>

namespace Insus.NET

{

    public class EditFactory : IOperationFactory 

    {

        public Operation CreateOperation()

        {

            return new OperationEdit();

        }

    }

}


更新工厂类:

对数据库数据操作,工厂方法设计模式(Factory Method) UpdateFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for UpdateFactory

/// </summary>

namespace Insus.NET

{

    public class UpdateFactory : IOperationFactory 

    {

        public Operation CreateOperation()

        {

            return new OperationUpdate();

        }

    }

}


删除工厂方法:

对数据库数据操作,工厂方法设计模式(Factory Method) DeleteFactory
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



/// <summary>

/// Summary description for DeleteFactory

/// </summary>

namespace Insus.NET

{

    public class DeleteFactory : IOperationFactory 

    {

        public Operation CreateOperation()

        {

            return new OperationDelete();

        }

    }

}

 
这样一来,工厂方法设计模式,就是去掉简单工厂设计模式的操作工厂类,在客户端应用此设计模式,直接实例需要的哪个操作工厂,如添加工厂类还是删除工厂类等:

对数据库数据操作,工厂方法设计模式(Factory Method) FactoryMethodDemo.aspx.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Insus.NET;



public partial class FactoryMethodDemo : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        IOperationFactory factory = new UpdateFactory();  //实例更新工厂类。



        Operation operation = factory.CreateOperation();



        operation.Processing();

    }

}


 

 工厂方法设计模式源代码(.net 4.0):

 http://download.cnblogs.com/insus/CSharpNET/Insus_FactoryMethod.rar

 

 

你可能感兴趣的:(factory)