设计模式之策略模式(包含与简单工厂模式的区别,实现了超市结账系统)
今天看了策略模式,不知觉的和前天学的简单工厂模式做了对比,也百度了一些博客,最后的出一个结论,简单工厂模式主要负责初始化各种对象,而策略模式则是更多的对方法进行封装。同一个例子,如果使用简单工厂模式,则是根据需求返回不同的对象,而使用策略模式的话,就是根据需求来返回不同的方法。
下面的例子分别使用简单工厂模式和策略模式和写的超市结账系统,二者功能实现上没有区别。
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; } } }
下面是使用策略模式写的超市结账系统。
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; } } }
求大神点拨,感觉用起来区别不大,二者还可以相互融合使用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话