了解C#集合相关知识请看以前的文章
优点:当商场增加了优惠方案时,直接定义新的具体策略类,使用Add(键值,实例化新的具体策略类)方法添加即可
Form1.cs(在窗体中的程序)
使用到的命名空间
DictionaryBase基类需要使用到using System.Collections;
using System;
using System.Collections;
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;
一共有四种类:
Context类,用于维护一个对Strategy对象的引用
Strategy类,策略类,定义所有支持的算法的公共接口
ConcreteStrategy类,具体策略类,封装了具体的算法或行为,继承Strategy类
分别对应以下程序的:CashContext类;CashSuper类;CashNormal等类
(对应商场收银主要分了三种收费模式:正常收费、满300返100、打8折)
CashSupers类,继承DictionaryBase类实现IDictionary接口,通过键值进行索引
策略类及具体策略类