该实例基于WPF实现,直接上代码,下面为三层架构的代码。
目录
一 Model
二 View
三 ViewModel
一 Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.策略者模式
{//2,定义环境角色: 持有抽象角色类的对象public class ContentOperation{//持有抽象角色类的对象private IncomeTax incomeTax;public ContentOperation(IncomeTax incomeTax){this.incomeTax = incomeTax;}//返回实际计算好的税收public double GetTax(double income){return incomeTax.CalculteTax(income);}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.策略者模式
{//定义算法策略: 企业所得税的算法public class EnterpriseTax : IncomeTax{public double CalculteTax(double income){return (income - 3500) > 0 ? (income - 3500) * 0.04 : 0.000;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.策略者模式
{//1,抽象策略类:定义所得税计算策略public interface IncomeTax{double CalculteTax(double income);}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.策略者模式
{//定义算法策略: 个体户税收计算方式internal class IndividualTax : IncomeTax{public double CalculteTax(double income){return (income - 12000) > 0 ? (income - 12000) * 0.5 : 0.0;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 设计模式练习.Model.策略者模式
{//3,定义相关算法: 个人所得税的计算方式internal class PersonTax : IncomeTax{public double CalculteTax(double income){return income * 0.15;}}
}
二 View
<Window x:Class="设计模式练习.View.策略者模式.StrategyWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:设计模式练习.View.策略者模式"mc:Ignorable="d"Title="StrategyWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Button Content="个人所得税计算" Grid.Row="0" Command="{Binding personCommand}"/><Button Content="企业所得税计算" Grid.Row="1" Command="{Binding enterpriseCommand}"/><Button Content="个体户所得税计算" Grid.Row="2" Command="{Binding IndividualCommand}"/></Grid><TextBlock Text="{Binding Result}" Grid.Column="1" VerticalAlignment="Center" FontSize="30" TextWrapping="Wrap"/></Grid>
</Window>
三 ViewModel
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.策略者模式;namespace 设计模式练习.ViewModel.策略者模式
{partial class StrategyWindow_ViewModel : ObservableRecipient{[ObservableProperty]private string result;[RelayCommand]private void person(){//计算个人所得税PersonTax personTax = new PersonTax();double srevenue = 20000.98; //个人收入ContentOperation content = new ContentOperation(personTax);Result = $"个人收入:{srevenue},个人支付税额:{content.GetTax(srevenue)}";}[RelayCommand]private void enterprise(){//计算企业所得税EnterpriseTax enterprise = new EnterpriseTax();double srevenue = 200009086.23; //企业收入ContentOperation content = new ContentOperation(enterprise);Result = $"企业收入:{srevenue},企业支付税额:{content.GetTax(srevenue)}";}[RelayCommand]private void Individual(){//计算个体户所得税IndividualTax ind = new IndividualTax();double srevenue = 200096.23; //企业收入ContentOperation content = new ContentOperation(ind);Result = $"个体户收入:{srevenue},个体户支付税额:{content.GetTax(srevenue)}";}}
}