当前位置: 首页 > news >正文

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

返回首页

前言

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

此章节介绍策略模式。


1、策略模式

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

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

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

1.1、UML图

在这里插入图片描述
在这里插入图片描述

1.2、示例代码

// 版本一:未使用面向对象思想
//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();
http://www.lryc.cn/news/264013.html

相关文章:

  • 条款6:若不想使用编译器自动生成的函数,就该明确拒绝
  • 零基础也能制作家装预约咨询小程序
  • Mybatis的插件运⾏原理,如何编写⼀个插件?
  • C++复合数据类型:字符数组|读取键盘输入|简单读写文件
  • Windows11环境下配置深度学习环境(Pytorch)
  • 泛型深入理解
  • Linux内核模块
  • Java 栈和队列的交互实现
  • HarmonyOS应用开发者高级认证满分指南
  • CSharp中Blazor初体验
  • Linux下新建用户,并进行授权
  • STM32为基础的模拟I2C通用8bit和16bit读取以及多字节读取
  • 算法训练营Day19
  • C++数据结构——二叉搜索树详解
  • ros2机器人在gazebo中移动方案
  • 学习Java第74天,Ajax简介
  • 【Java面试题】在Java中String,Stringbuffer,StringBuilder的区别?
  • 让AIGC成为你的智能外脑,助力你的工作和生活
  • ubuntu12.04 源
  • openssl数据压缩
  • SQLturning:定位连续值范围起点和终点
  • 饥荒Mod 开发(十七):手动保存和加载,无限重生
  • Skywalking系列之最新版9.2.0-JavaAgent本地构建
  • olap/clickhouse-编译器优化与向量化
  • RK3399平台开发系列讲解(内核入门篇)网络协议的分层
  • Idea远程debugger调试
  • MATLAB - Gazebo 仿真环境
  • selenium自动化webdriver下载及安装
  • 网络基础介绍
  • Java中四种引用类型(强、软、弱、虚)