博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSharp设计模式读书笔记(24):访问者模式(学习难度:★★★★☆,使用频率:★☆☆☆☆)...
阅读量:6672 次
发布时间:2019-06-25

本文共 3511 字,大约阅读时间需要 11 分钟。

模式角色与结构:

示例代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CSharp.DesignPattern.VisitorPattern{    class Program    {        static void Main(string[] args)        {            ObjectStructure list = new ObjectStructure();            list.AddEmployee(new FulltimeEmployee("Mike", 2000));            list.AddEmployee(new FulltimeEmployee("Tony", 3000));            list.AddEmployee(new ParttimeEmployee("Nancen", 800));            list.AddEmployee(new ParttimeEmployee("Bibo", 500));            FinanceVisitor fVisitor = new FinanceVisitor();            HrVisitor hVisitor = new HrVisitor();            list.Accept(fVisitor);            list.Accept(hVisitor);        }    }    class ObjectStructure    {        public void Accept(Visitor visitor)        {            foreach (IEmployee element in employees)            {                element.Accept(visitor);            }        }        public void AddEmployee(IEmployee employee)        {            employees.Add(employee);        }        public void RemoveEmployee(IEmployee employee)        {            employees.Remove(employee);        }        private List
employees = new List
(); } class Visitor { public abstract void Visit(FulltimeEmployee fulltime); public abstract void Visit(ParttimeEmployee contract); } class FinanceVisitor : Visitor { public void Visit(FulltimeEmployee fulltime) { Console.Write("The wage of fulltime is ..."); } public void Visit(ParttimeEmployee contract) { Console.Write("The wage of parttime is ..."); } } class HrVisitor : Visitor { public void Visit(FulltimeEmployee fulltime) { Console.Write("The worktime of fulltime is ..."); Console.Write("The overtime of fulltime is ..."); Console.Write("The timeoff of fulltime is ..."); } public void Visit(ParttimeEmployee contract) { Console.Write("The worktime of fulltime is ..."); } } interface IEmployee { public void Accept(Visitor visitor) { } String Name { set; get; } Int32 Wage { set; get; } } class FulltimeEmployee : IEmployee { public FulltimeEmployee(String name, Int32 wage) { this._name = name; this._wage = wage; } public void Accept(Visitor visitor) { visitor.Visit(this); } public void OperationFulltime() { } public string Name { get { return _name; } set { _name = value; } } public int Wage { get { return _wage; } set { _wage = value; } } private String _name; private Int32 _wage; } class ParttimeEmployee : IEmployee { public ParttimeEmployee(String name, Int32 wage) { this._name = name; this._wage = wage; } public void Accept(Visitor visitor) { visitor.Visit(this); } public void OperationParttime() { } public string Name { get { return _name; } set { _name = value; } } public int Wage { get { return _wage; } set { _wage = value; } } private String _name; private Int32 _wage; }}

 

转载于:https://www.cnblogs.com/thlzhf/p/3993810.html

你可能感兴趣的文章
PHPCMS一个BUG
查看>>
APP云测试
查看>>
3-unit3 高速缓存DNS
查看>>
spark mllib 协同过滤算法,基于余弦相似度的用户相似度计算
查看>>
openwrt 基于qmi的 3G|4G拨号
查看>>
俞敏洪励志语
查看>>
开源|基于TensorFlow的聊天机器人-ErGo
查看>>
lucene4.0入门1
查看>>
Svn结合hook实现自动更新及多Project管理更新
查看>>
sgu 222
查看>>
让spring-data-jpa解放你的DAO
查看>>
58沈剑:架构师的平凡之路
查看>>
Hibernate问题-read-write缓存策略
查看>>
sql中实现汉字的拼音首字母查询
查看>>
Android 动态布局 (代码布局)
查看>>
MYSQL备份和恢复
查看>>
spark安装:在hadoop YARN上运行spark-shell
查看>>
Docker存储驱动之ZFS简介
查看>>
根据sql,一键生成excle 格式, 再通过 zip包压缩为zip
查看>>
PL/SQL Developer 添加数据
查看>>