设计模式 - 2) 策略模式
发布日期:2021-05-13 19:47:04 浏览次数:9 分类:博客文章

本文共 8693 字,大约阅读时间需要 28 分钟。

// ������������public abstract class CashSuper{    public abstract double acceptCash(double money);}// ������������public class CashNomal : CashSuper{    public override double acceptCash(double money)    {        return money;    }}// ��� 8 ���public class CashRebate : CashSuper{    private double moneyRebate = 1d;    public CashRebate(string rebate)    {        moneyRebate = double.Parse(rebate);    }    public override double acceptCash(double money)    {        return money * moneyRebate;    }}// ������public class CashReturn : CashSuper{    private double moneyCondition = 0.0d;    private double moneyReturn = 0.0d;    public CashReturn(string condition, string re)    {        moneyCondition = double.Parse(condition);        moneyReturn = double.Parse(re);    }    public override double acceptCash(double money)    {        if (money < moneyCondition) return money;        return money - Math.Floor(money / moneyCondition) * moneyReturn;    }}// ���������������������"������"���public class CashContext{    public CashSuper cs = null;    public CashContext(string type)    {        switch (type)        {            case "������":                cs = new CashNomal();                break;            case "8 ���":                cs = new CashRebate("0.8");                break;            case "��� 200 - 50":                cs = new CashReturn("200", "50");                break;            default:                break;        }    }    public double GetResult(double money)    {        if (cs == null) return 0;        return cs.acceptCash(money);    }}// ������������"������"���������������������������������������������������������������������������������������������CashContext CashContextObj = new CashContext(SelectedCostType.Name);TotalPrice = CashContextObj.GetResult(double.Parse(CurPrice) * double.Parse(CurMul));

������������������������������������������������������������������������������ CashContext ������ CashContext(string type) ���������������������������������������������

���������

xmal.cspublic partial class Window7 : Window{ public Window7() { InitializeComponent(); this.DataContext = new Window7ViewModel() {}; AddHandler(Button.ClickEvent, new RoutedEventHandler((this.DataContext as Window7ViewModel).Element_Click)); }}public class BindingProxy : Freezable{ protected override Freezable CreateInstanceCore() { return new BindingProxy(); } public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));}public class DelegateCommand : ICommand{ public Action ExecuteCommand = null; public Func
CanExecuteCommand = null; public event EventHandler CanExecuteChanged; public DelegateCommand() { } public DelegateCommand(Action
act) { ExecuteCommand = act; } public bool CanExecute(object parameter) { if (CanExecuteCommand != null) { return CanExecuteCommand(parameter); } else { return true; } } public void Execute(object parameter) { if (ExecuteCommand != null) this.ExecuteCommand(parameter); } public void RaiseCanExecuteChanged() { if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } }}public class NotifyPropertyChanged : INotifyPropertyChanged{ public event PropertyChangedEventHandler PropertyChanged; /// /// ������������������ /// /// ������������ public void OnProperty(string propertyname) { PropertyChangedEventHandler propertychanged = PropertyChanged; if (propertychanged != null) { propertychanged(this, new PropertyChangedEventArgs(propertyname)); } } /// /// ������������������ /// ///
/// ���������������lambda��������� public void RaisePropertyChange
(Expression
> func) { var body = func.Body as MemberExpression; if (body != null) { var propertyinfo = body.Member; if (propertyinfo != null) { PropertyChangedEventHandler propertychanged = PropertyChanged; if (propertychanged != null) { propertychanged(this, new PropertyChangedEventArgs(propertyinfo.Name)); } } } }}public class Window7ViewModel : NotifyPropertyChanged{ private string _CurPrice; public string CurPrice { get { return _CurPrice; } set { _CurPrice = value; RaisePropertyChange(() => CurPrice); } } private string _CurMul; public string CurMul { get { return _CurMul; } set { _CurMul = value; RaisePropertyChange(() => CurMul); } } private double _TureTotalPrice = 0.0d; ///
/// ��������������������� /// public double TureTotalPrice { get { return _TureTotalPrice; } set { _TureTotalPrice = value; RaisePropertyChange(() => TureTotalPrice); } } private double _TotalPrice = 0.0d; ///
/// ��������� /// public double TotalPrice { get { return _TotalPrice; } set { _TotalPrice = value; RaisePropertyChange(() => TotalPrice); } } public ObservableCollection
_VList; ///
/// ������������ /// public ObservableCollection
VList { get { if (_VList == null) { _VList = new ObservableCollection
(); } return _VList; } set { _VList = value; RaisePropertyChange(() => VList); } } public ObservableCollection
_CostTypeList; ///
/// ������������������ /// public ObservableCollection
CostTypeList { get { if (_CostTypeList == null) { _CostTypeList = new ObservableCollection
(); } return _CostTypeList; } set { _CostTypeList = value; RaisePropertyChange(() => CostTypeList); } } public CostType SelectedCostType { get; set; } public DelegateCommand DelCommand { get; set; } public Window7ViewModel() { DelCommand = new DelegateCommand(new Action
(DelItem)); CostTypeList.Add(new CostType() { Name = "������"}); CostTypeList.Add(new CostType() { Name = "8 ���"}); CostTypeList.Add(new CostType() { Name = "��� 200 - 50"}); } private void DelItem(object obj) { try { VItem item = obj as VItem; VList.Remove(item); CashContext CashContextObj = new CashContext(item.Rebate); TotalPrice -= CashContextObj.GetResult(double.Parse(item.Price) * double.Parse(item.Mul)); TureTotalPrice -= double.Parse(item.Price) * double.Parse(item.Mul); } catch (Exception ex) { } } public void Element_Click(object sender, RoutedEventArgs e) { try { UIElement element = (UIElement)e.Source; if (element == null) element = (UIElement)e.OriginalSource; if (element == null) return; switch (element.Uid) { case "Sure": if (SelectedCostType == null) { MessageBox.Show("���������������"); return; } CashContext CashContextObj = new CashContext(SelectedCostType.Name); TotalPrice += CashContextObj.GetResult(double.Parse(CurPrice) * double.Parse(CurMul)); TureTotalPrice += double.Parse(CurPrice) * double.Parse(CurMul); VList.Add(new VItem() { Price = CurPrice, Mul = CurMul,Rebate = SelectedCostType.Name }); ResetCur(); break; case "Reset": VList.Clear(); ResetCur(); TotalPrice = 0; TureTotalPrice = 0; break; default: break; } } catch (Exception ex) { MessageBox.Show("������������" + ex); } } private void ResetCur() { CurPrice = string.Empty; CurMul = string.Empty; }}/// /// ������������������/// public class VItem : NotifyPropertyChanged{ private string _Price; public string Price { get { return _Price; } set { _Price = value; RaisePropertyChange(() => Price); } } private string _Mul; public string Mul { get { return _Mul; } set { _Mul = value; RaisePropertyChange(() => Mul); } } private string _Rebate; public string Rebate { get { return _Rebate; } set { _Rebate = value; RaisePropertyChange(() => Rebate); } }}/// /// ������������/// public class CostType{ public string Name { get; set; } public double Cost { get; set; }}
上一篇:设计模式 - 3) 单一、开放封闭、依赖倒置、里氏置换
下一篇:设计模式 - 1) 简单工厂

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年04月25日 10时54分30秒