策略者模式-C#实现

该实例基于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


    
        
            
            
        
        
            
                
                
                
            
            

策略者模式-C#实现_第1张图片

三 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)}";
        }
    }
}

你可能感兴趣的:(c#,开发语言)