CSharp: Visitor Pattern
/// <summary> /// Summary description for Employee. /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Employee { int sickDays, vacDays; float salary; string name; /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="salary"></param> /// <param name="vDays"></param> /// <param name="sDays"></param> public Employee(string name, float salary, int vDays, int sDays) { this.name = name; this.salary = salary; sickDays = sDays; vacDays = vDays; } /// <summary> /// /// </summary> public string Name { get { return name; } set { name = value; } } /// <summary> /// /// </summary> public float Salary { get { return salary; } set { salary = value; } } /// <summary> /// /// </summary> public int SickDays { get { return sickDays; } set { sickDays = value; } } /// <summary> /// /// </summary> public int VacDays { get { return vacDays; } set { vacDays = value; } } /// <summary> /// /// </summary> /// <returns></returns> public string getName() { return name; } /// <summary> /// /// </summary> /// <returns></returns> public int getSickDays() { return sickDays; } /// <summary> /// /// </summary> /// <returns></returns> public int getVacDays() { return vacDays; } /// <summary> /// /// </summary> /// <returns></returns> public float getSalary() { return salary; } /// <summary> /// /// </summary> /// <param name="v"></param> public virtual void accept(Visitor v) { v.visit(this); } /// <summary> /// /// </summary> /// <returns></returns> public string toString() { return name + "," + salary.ToString() + "," + sickDays.ToString() + "," + vacDays.ToString() + ""; } }
/// <summary> /// Summary description for Visitor. /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public abstract class Visitor { /// <summary> /// /// </summary> /// <param name="emp"></param> public abstract void visit(Employee emp); /// <summary> /// /// </summary> /// <param name="bos"></param> public abstract void visit(Boss bos); }
/// <summary> /// Summary description for Manager.\ /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Manager : Employee { private int bonusDays; /// <summary> /// /// </summary> /// <param name="nm"></param> /// <param name="slry"></param> /// <param name="vDays"></param> /// <param name="sDays"></param> public Manager(string nm, float slry, int vDays, int sDays) : base(nm, slry, vDays, sDays) { } /// <summary> /// /// </summary> /// <param name="bd"></param> public void setBonusDays(int bd) { bonusDays = bd; } /// <summary> /// /// </summary> /// <returns></returns> public int getBonusDays() { return bonusDays; } /// <summary> /// /// </summary> /// <param name="v"></param> public override void accept(Visitor v) { v.visit(this); } }
/// <summary> /// Summary description for Boss. /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class Boss : Employee { private int bonusDays; /// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="salary"></param> /// <param name="vdays"></param> /// <param name="sdays"></param> public Boss(string name, float salary, int vdays, int sdays) : base(name, salary, vdays, sdays) { } /// <summary> /// /// </summary> /// <param name="bdays"></param> public void setBonusDays(int bdays) { bonusDays = bdays; } /// <summary> /// /// </summary> /// <returns></returns> public int getBonusDays() { return bonusDays; } /// <summary> /// /// </summary> /// <param name="v"></param> public override void accept(Visitor v) { v.visit(this); } }
/// <summary> /// Summary description for VacationVisitor. /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class VacationVisitor : Visitor { private int totalDays; /// <summary> /// /// </summary> public VacationVisitor() { totalDays = 0; } /// <summary> /// /// </summary> /// <returns></returns> public int getTotalDays() { return totalDays; } /// <summary> /// /// </summary> /// <param name="emp"></param> public override void visit(Employee emp) { totalDays += emp.getVacDays(); } /// <summary> /// /// </summary> /// <param name="bos"></param> public override void visit(Boss bos) { totalDays += bos.getVacDays(); } }
调用:
/// <summary> /// Visitor Pattern 访问者模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class VisitorPatternForm : Form { private List<Employee> empls; private Employee empl; private Boss bos; /// <summary> /// /// </summary> private void init() { int i = 0; empls = new List<Employee>(); empl = new Employee("Susan Bear", 55000, 12, 1); empls.Add(empl); empl = new Employee("Adam Gehr", 150000, 9, 0); empls.Add(empl); empl = new Employee("Fred Harris", 50000, 15, 2); empls.Add(empl); empl = new Employee("David Oakley", 57000, 12, 2); empls.Add(empl); empl = new Employee("Larry Thomas", 100000, 20, 6); empls.Add(empl); bos = new Boss("Leslie Susi", 175000, 16, 4); empls.Add(empl); bos.setBonusDays(12); empls.Add(bos); bos = new Boss("Laurence Byerly", 35000, 17, 6); bos.setBonusDays(17); empls.Add(bos); } /// <summary> /// /// </summary> public VisitorPatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void VisitorPatternForm_Load(object sender, EventArgs e) { this.dataGridView1.DataSource = empls; } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btCompute_Click(object sender, EventArgs e) { VacationVisitor vac = new VacationVisitor(); VacationVisitor bvac = new VacationVisitor(); for (int i = 0; i < empls.Count; i++) { empls[i].accept(vac); //get the employee empls[i].accept(bvac); } lsVac.Items.Add("Total vacation days=" + vac.getTotalDays().ToString()); lsVac.Items.Add("Total boss vacation days=" + bvac.getTotalDays().ToString()); this.dataGridView1.DataSource = empls; } }
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)