【从零开始学习--设计模式--策略模式】

返回首页

前言

感谢各位同学的关注与支持,我会一直更新此专题,竭尽所能整理出更为详细的内容分享给大家,但碍于时间及精力有限,代码分享较少,后续会把所有代码示例整理到github,敬请期待。

此章节介绍策略模式。


1、策略模式

在策略模式中,一个类的行为或其算法可以在运行时更改。

在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的context对象。策略对象改变context对象的执行算法。

定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。

1.1、UML图


1.2、示例代码

C# 复制代码
// 版本一:未使用面向对象思想
//if (string.IsNullOrEmpty(tbPrice.Text) || string.IsNullOrEmpty(tbCount.Text))
//{
//    MessageBox.Show("单价或数量不能为空");
//    return;
//}
//double total = Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text);
//_total += total;
//rtbList.AppendText("单价:" + tbPrice.Text + " 数量:" + tbCount.Text + " 合计:" + total + "\n");
//lbTotal.Text = _total.ToString();

// 版本二(增加打折):重复代码过多、未使用面向对象思想
//if (string.IsNullOrEmpty(tbPrice.Text) || string.IsNullOrEmpty(tbCount.Text))
//{
//    MessageBox.Show("单价或数量不能为空");
//    return;
//}
//double total = 0;
//switch (cbxType.SelectedIndex)
//{
//    case 0:
//        total = Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text);
//        break;
//    case 1:
//        total = Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text) * 0.8;
//        break;
//    case 2:
//        total = Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text) * 0.7;
//        break;
//    case 3:
//        total = Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text) * 0.5;
//        break;
//}
//_total += total;
//rtbList.AppendText("单价:" + tbPrice.Text + " 数量:" + tbCount.Text + " " + cbxType.SelectedItem + " 合计:" + total + "\n");
//lbTotal.Text = _total.ToString();

// 版本三:简单工厂模式
//if (string.IsNullOrEmpty(tbPrice.Text) || string.IsNullOrEmpty(tbCount.Text))
//{
//    MessageBox.Show("单价或数量不能为空");
//    return;
//}
//Factory.CashSuper cash = Factory.CashFactory.CreateFactory(cbxType.SelectedItem.ToString());
//double total = cash.acceptCash(Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text));
//_total += total;
//rtbList.AppendText("单价:" + tbPrice.Text + " 数量:" + tbCount.Text + " " + cbxType.SelectedItem + " 合计:" + total + "\n");
//lbTotal.Text = _total.ToString();

版本三:策略模式
//if (string.IsNullOrEmpty(tbPrice.Text) || string.IsNullOrEmpty(tbCount.Text))
//{
//    MessageBox.Show("单价或数量不能为空");
//    return;
//}
//Strategy.CashContext cashContext = null;
//switch (cbxType.SelectedItem)
//{
//    case "正常收费":
//        cashContext = new Strategy.CashContext(new Strategy.CashNormal());
//        break;
//    case "打八折":
//        cashContext = new Strategy.CashContext(new Strategy.CashRebate(0.8));
//        break;
//    case "满300返100":
//        cashContext = new Strategy.CashContext(new Strategy.CashReturn(300, 100));
//        break;
//    default:
//        break;
//}
//double total = cashContext.GetResult(Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text));
//_total += total;
//rtbList.AppendText("单价:" + tbPrice.Text + " 数量:" + tbCount.Text + " " + cbxType.SelectedItem + " 合计:" + total + "\n");
//lbTotal.Text = _total.ToString();

// 版本四:策略模式、简单工厂模式
if (string.IsNullOrEmpty(tbPrice.Text) || string.IsNullOrEmpty(tbCount.Text))
{
    MessageBox.Show("单价或数量不能为空");
    return;
}
Strategy.CashContextFactory ccf = new Strategy.CashContextFactory(cbxType.SelectedItem.ToString());
double total = ccf.GetResult(Convert.ToDouble(tbPrice.Text) * Convert.ToDouble(tbCount.Text));
_total += total;
rtbList.AppendText("单价:" + tbPrice.Text + " 数量:" + tbCount.Text + " " + cbxType.SelectedItem + " 合计:" + total + "\n");
lbTotal.Text = _total.ToString();
相关推荐
周某某~11 分钟前
二.单例模式‌
java·单例模式·设计模式
十五年专注C++开发27 分钟前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
hstar952730 分钟前
三十五、面向对象底层逻辑-Spring MVC中AbstractXlsxStreamingView的设计
java·后端·spring·设计模式·架构·mvc
pengyu1 小时前
【Java设计原则与模式之系统化精讲:壹】 | 编程世界的道与术(实战指导篇)
java·后端·设计模式
Chef_Chen1 小时前
从0开始学习R语言--Day20-ARIMA与格兰杰因果检验
开发语言·学习·r语言
凌辰揽月2 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
小吕学编程2 小时前
策略模式实战:Spring中动态选择商品处理策略的实现
java·开发语言·设计模式
pan_junbiao2 小时前
Spring框架的设计模式
java·spring·设计模式
whoarethenext3 小时前
C++ OpenCV 学习路线图
c++·opencv·学习
恰薯条的屑海鸥3 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十四期-XXE模块)
网络·学习·安全·web安全·渗透测试