设计模式之策略模式

来源:互联网 发布:使命召唤ol辅助淘宝 编辑:程序博客网 时间:2024/06/05 15:52

以下是策略模式的工资支付的C#代码,谨供借鉴,欢迎提出您的意见害羞

很多企业的工资支付方式是很灵活的,可支付方式是比较多的,比如:人民币现金支付、美元现金支付、银行转账到工资帐户、银行转账到工资卡;一些创业型的企业为了留住骨干员工,还可能有:工资转股权等等方式 
随着公司的发展,会不断有新的工资支付方式出现,这就要求能方便的扩展;另外工资支付方式不是固定的,是由公司和员工协商确定的,也就是说可能不同的员工采用的是不同的支付方式,甚至同一个员工,不同时间采用的支付方式也可能会不同,这就要求能很方便的切换具体的支付方式。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication19{    abstract class Salary    {        public abstract void Salary1(Content a);    }  class RMBCash:Salary    {        public override void Salary1(Content a)        {            Console.WriteLine("给{0}使用人民币现金方式支付工资{1}元",a.Name,a.Salary2);        }    }    class DollarCash:Salary    {               public override void Salary1(Content a)        {            Console.WriteLine("给{0}使用美元现金方式支付{1}元",a.Name,a.Salary2);        }    }    class Card:Salary    {   public override void Salary1(Content a)        {            Content1 c = (Content1)a;        Console.WriteLine("给{0}使用银行卡,卡号{2}支付{1}元",c.Name,c.Salary2,c.Number);    }    }    public class Content    {        private Salary s = null;        private string name;        private double salary2;        public string Name        {            get { return name; }            set { name = value; }        }        public double Salary2        {            get { return salary2; }            set { salary2 = value; }        }        public Content(string type,string name,double salary2)        {            this.name = name;            this.salary2 = salary2;            switch(type)            {                case "人民币现金支付":RMBCash r=new RMBCash();s=r;break;                case"美元现金支付":DollarCash d=new DollarCash();s=d;break;                case "银行卡支付": Card c = new Card(); s = c; break;            }        }        public void GetResult()        {            s.Salary1(this);        }    }    class Content1:Content    {        private string number;        public string Number        {            get { return number;}            set { number = value; }        }        public Content1(string type,string name,double salary2,string number):base(type,name,salary2)        {            this.Number = number;        }    }    class Program    {        static void Main(string[] args)        {            Content zhangsan = new Content("人民币现金支付","张三", 1000);            zhangsan.GetResult();            Content1 Tom=new Content1("银行卡支付","Tom",8000,"12121212");            Tom.GetResult();        }    }}

1 0
原创粉丝点击