设计模式之策略模式(包含与简单工厂模式的区别,实现了超市结帐系统)

今天看了策略模式,不知觉的和前天学的简单工厂模式作了对比,也百度了一些博客,最后的出一个结论,简单工厂模式主要负责初始化各类对象,而策略模式则是更多的对方法进行封装。同一个例子,若是使用简单工厂模式,则是根据需求返回不一样的对象,而使用策略模式的话,就是根据需求来返回不一样的方法。ide

下面的例子分别使用简单工厂模式和策略模式和写的超市结帐系统,两者功能实现上没有区别。spa

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 简单工厂模式与策略模式
{
    public class Cash
    {
        double price;
        int num;
        public double Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
        public int Num
        {
            get
            {
                return num;
            }
            set
            {
                num = value;
            }
        }
        public virtual string GetCash()
        {
            return "error";
        }
    }
    public class CashA:Cash
    {
        public override string GetCash()
        {
            return (Price * Num).ToString();
        }
    }
    public class CashB : Cash
    {
        public override string GetCash()
        {
            return (Price * Num*0.8).ToString();
        }
    }
    public class CashC : Cash
    {
        public override string GetCash()
        {
            return (Price * Num * 0.5).ToString();
        }
    }
    public class CashFactory          //工厂类
    {
        public static Cash GetCashObject(string str)
        {
            switch (str)
            {
                case "正常收费": return new CashA();
                case "8折收费": return new CashB();
                case "5折收费": return new CashC();
                default: return new Cash();
            }
        }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double total=0;
        private void button1_Click(object sender, EventArgs e)
        {
            double totalPrice;
            Cash p = CashFactory.GetCashObject(comboBox1.SelectedItem.ToString());
            p.Num = int.Parse(textBox2.Text);
            p.Price = double.Parse(textBox1.Text);
            totalPrice=double.Parse(p.GetCash());
            total+=totalPrice;
            listBox1.Items.Add("单价:"+textBox1.Text+"数量:"+textBox2.Text+"总价为"+totalPrice.ToString());
            label4.Text = total.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            label4.Text = null;
            for (int i = 0; i < listBox1.Items.Count;i++ )
            {
                listBox1.Items.RemoveAt(i);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new object[] { "正常收费", "8折收费", "5折收费" });
            comboBox1.SelectedIndex = 0;
        }
    }
}
复制代码

下面是使用策略模式写的超市结帐系统。code

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 简单工厂模式与策略模式
{
    public class Cash
    {
        double price;
        int num;
        public double Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
        public int Num
        {
            get
            {
                return num;
            }
            set
            {
                num = value;
            }
        }
        public virtual string GetCash()
        {
            return "error";
        }
    }
    public class CashA:Cash
    {
        public override string GetCash()
        {
            return (Price * Num).ToString();
        }
    }
    public class CashB : Cash
    {
        public override string GetCash()
        {
            return (Price * Num*0.8).ToString();
        }
    }
    public class CashC : Cash
    {
        public override string GetCash()
        {
            return (Price * Num * 0.5).ToString();
        }
    }
    public class CashContext
    {
        public Cash p;
        public CashContext(string str)
        {
            switch(str)
            {
                case "正常收费": p = new CashA(); break;
                case "8折收费": p = new CashB(); break;
                case "5折收费": p = new CashC(); break;
            }
        }
        public string GetResult()
        {
            return p.GetCash();
        }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double total=0;
        private void button1_Click(object sender, EventArgs e)
        {
            double totalPrice=0;
           // Cash p = CashFactory.GetCashObject(comboBox1.SelectedItem.ToString());
            CashContext p = new CashContext(comboBox1.SelectedItem.ToString());
            p.p.Num = int.Parse(textBox2.Text);
            p.p.Price = double.Parse(textBox1.Text);
            totalPrice = double.Parse(p.p.GetResult());

            total+=totalPrice;
            listBox1.Items.Add("单价:"+textBox1.Text+"数量:"+textBox2.Text+"总价为"+totalPrice.ToString());
            label4.Text = total.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            label4.Text = null;
            for (int i = 0; i < listBox1.Items.Count;i++ )
            {
                listBox1.Items.RemoveAt(i);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(new object[] { "正常收费", "8折收费", "5折收费" });
            comboBox1.SelectedIndex = 0;
        }
    }
}
复制代码

求大神点拨,感受用起来区别不大,两者还能够相互融合使用。orm