首页 > 代码库 > 简单工厂模式与Strategy模式

简单工厂模式与Strategy模式

参考资料:《大话设计模式》

简单工厂模式:

///
///简单工厂模式实验,2014.05.07---通过工厂类实例化对象
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ds
{
    class Program
    {
        static void Main(string[] args)
        {
            Operation oper = OperationFactory.OperationProduct("/");          
            oper.OperationNum(100,25);
            Console.WriteLine(oper.Result());
        }
    }


    class OperationFactory
    {
        /// <summary>
        /// 实例化一个具体类
        /// </summary>
        /// <param name="oper"></param>
        /// <returns></returns>
        public static Operation OperationProduct(string oper)
        {
            Operation Oper = null;
            switch (oper)
            {
                case "+":
                    Oper = new Operation_add();
                    break;
                case "-":
                    Oper = new Operation_sub();
                    break;
                case "*":
                    Oper = new Operation_mul();
                    break;
                case "/":
                    Oper = new Operation_div();
                    break;
            }
            return Oper;
        }
    }

    abstract class Operation
    {
        protected int numA;
        protected int numB;
        /// <summary>
        /// 传入操作数
        /// </summary>
        /// <param name="numA"></param>
        /// <param name="numB"></param>
        public void OperationNum(int numA, int numB)
        {
            this.numA = numA;
            this.numB = numB;
        }
        /// <summary>
        /// 返回操作数结果
        /// </summary>
        /// <returns></returns>
        public abstract int Result();
    }

    class Operation_add : Operation
    {
        public override int Result()
        {
            return numA + numB;
        }
    }
    class Operation_sub : Operation
    {
        public override int Result()
        {
            return numA - numB;
        }
    }
    class Operation_mul : Operation
    {
        public override int Result()
        {
            return numA * numB;
        }
    }
    class Operation_div : Operation
    {
        public override int Result()
        {
            if (numB > 0)
            {
                return numA / numB;
            }
            else
            {
                Console.WriteLine("除数必须为正数");
                return 0;
            }
        }
    }
}

Strategy(策略)模式:

///
///简单工厂模式---工厂类只返回实例化的对象,策略模式在Context类中,把方法(变化点)也封装了。
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ds_Strategy
{
    class Program
    {
        static void Main(string[] args)
        {
            Context con = new Context("/");
            Console.WriteLine(con.ContextInterface(88,11));
        }
    }
    class Context
    {
        Operation operation = null;
        public Context(string oper) //根据字符串,实例化策略
        {
            switch (oper)
            {
                case "+":
                    this.operation = new Operation_add();
                    break;
                case "-":
                    this.operation = new Operation_sub();
                    break;
                case "*":
                    this.operation = new Operation_mul();
                    break;
                case "/":
                    this.operation = new Operation_div();
                    break;
            }
        }
        public int ContextInterface(int NumA, int NumB)
        {
            return operation.Result(NumA, NumB);//调用具体的算法
        }
    }
    /// <summary>
    /// 抽象类
    /// </summary>
    abstract class Operation
    {
        /// <summary>
        /// 返回操作数结果
        /// </summary>
        /// <returns></returns>
        public abstract int Result(int numA, int numB);
    }

    class Operation_add : Operation
    {
        public override int Result(int numA, int numB)
        {
            return numA + numB;
        }
    }
    class Operation_sub : Operation
    {
        public override int Result(int numA, int numB)
        {
            return numA - numB;
        }
    }
    class Operation_mul : Operation
    {
        public override int Result(int numA, int numB)
        {
            return numA * numB;
        }
    }
    class Operation_div : Operation
    {
        public override int Result(int numA, int numB)
        {
            if (numB > 0)
            {
                return numA / numB;
            }
            else
            {
                Console.WriteLine("除数必须为正数");
                return 0;
            }
        }
    }

}//namespace

 

1.简单工厂模式只是解决对象的创建问题;

2.Strategy:定义了算法家族,分别封装起来,让他们之间可以互相替换,让算法的变化,不会影响到使用者--不懂???。

3.说实话,这两个模式的区别不是很清楚;记录在此只是为了方便今后的学习。