C#设计模式:策略模式Stragety Pattern代码实现
首先感谢大佬分享!【C#设计模式】19.策略模式(Stragety Pattern)【行为型】
基础实现
using System; /// <summary> /// 策略模式的实现 /// </summary> namespace StrategyPattern { /// <summary> /// 环境角色---相当于Context类型 /// </summary> public sealed class SalaryContext { private ISalaryStrategy _strategy; public SalaryContext(ISalaryStrategy strategy) { this._strategy = strategy; } public ISalaryStrategy ISalaryStrategy { get { return _strategy; } set { _strategy = value; } } public void GetSalary(double income) { _strategy.CalculateSalary(income); } } /// <summary> /// 抽象策略角色---相当于Strategy类型 /// </summary> public interface ISalaryStrategy { /// <summary> /// 工资计算 /// </summary> /// <param name="income">底薪</param> void CalculateSalary(double income); } //策略角色ConcreteStrategyA:程序员的工资 public sealed class ProgrammerSalary : ISalaryStrategy { public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(" + 8000 + ")+加班费+项目奖金(10%)"); } } //ConcreteStrategyB:普通员工的工资 public sealed class NormalPeopleSalary : ISalaryStrategy { public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(3000)+加班费"); } } //ConcreteStrategyC:CEO的工资 public sealed class CEOSalary : ISalaryStrategy { public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(20000)+项目奖金(20%)+公司股票"); } } /// <summary> /// 正式调用 /// </summary> public class Client { public static void Main(String[] args) { //普通员工的工资 Console.WriteLine("【普通员工的工资】"); SalaryContext context = new SalaryContext(new NormalPeopleSalary()); context.GetSalary(3000); //程序员的工资 Console.WriteLine("【程序员的工资】"); context.ISalaryStrategy = new ProgrammerSalary(); context.GetSalary(6000); //CEO的工资 Console.WriteLine("【CEO的工资】"); context.ISalaryStrategy = new CEOSalary(); context.GetSalary(6000); Console.Read(); } } }
进阶实现
using System; using System.Collections.Generic; /// <summary> /// 策略模式的实现_进阶版 /// </summary> namespace StrategyPattern_Advanced { /// <summary> /// 工作分类 /// </summary> public enum JobTypeEnum { Programmer, NormalPeople, CEO } /// <summary> /// 抽象策略角色---相当于Strategy类型 /// </summary> public interface ISalaryStrategy { /// <summary> /// 工资计算 /// </summary> /// <param name="income">底薪</param> void CalculateSalary(double income); } /// <summary> /// 抽象环境角色 /// </summary> public interface ISalaryContext { void GetSalary(JobTypeEnum jobType, double income); } /// <summary> /// 环境角色---相当于Context类型 /// </summary> public sealed class SalaryContext : ISalaryContext { private IDictionary<JobTypeEnum, ISalaryStrategy> SalaryStrategy { get; set; } public SalaryContext() { SalaryStrategy = new Dictionary<JobTypeEnum, ISalaryStrategy>(){ {JobTypeEnum.Programmer,new ProgrammerSalary()}, {JobTypeEnum.NormalPeople,new NormalPeopleSalary()}, {JobTypeEnum.CEO,new CEOSalary()} }; } public void GetSalary(JobTypeEnum jobType, double income) { this.SalaryStrategy[jobType].CalculateSalary(income); } } //策略角色ConcreteStrategyA:程序员的工资 public sealed class ProgrammerSalary : ISalaryStrategy { public JobTypeEnum JobType { get { return JobTypeEnum.Programmer; } } public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(" + 8000 + ")+加班费+项目奖金(10%)"); } } //ConcreteStrategyB:普通员工的工资 public sealed class NormalPeopleSalary : ISalaryStrategy { public JobTypeEnum JobType { get { return JobTypeEnum.NormalPeople; } } public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(3000)+加班费"); } } //ConcreteStrategyC:CEO的工资 public sealed class CEOSalary : ISalaryStrategy { public JobTypeEnum JobType { get { return JobTypeEnum.CEO; } } public void CalculateSalary(double income) { Console.WriteLine("我的工资是:基本工资(" + income + ")底薪(20000)+项目奖金(20%)+公司股票"); } } /// <summary> /// 正式调用 /// </summary> public class Client { public static void Main(String[] args) { ISalaryContext salaryContext = new SalaryContext(); //普通员工的工资 Console.WriteLine("【普通员工的工资】"); salaryContext.GetSalary(JobTypeEnum.NormalPeople, 3000); //程序员的工资 Console.WriteLine("【程序员的工资】"); salaryContext.GetSalary(JobTypeEnum.NormalPeople, 6000); //CEO的工资 Console.WriteLine("【CEO的工资】"); salaryContext.GetSalary(JobTypeEnum.NormalPeople, 9000); } } }
代码备份至文件: