CSharp设计模式读书笔记(24):访问者模式(学习难度:★★★★☆,使用频率:★☆☆☆☆)
模式角色与结构:

示例代码:
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", ));
list.AddEmployee(new FulltimeEmployee("Tony", ));
list.AddEmployee(new ParttimeEmployee("Nancen", ));
list.AddEmployee(new ParttimeEmployee("Bibo", )); 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<IEmployee> employees = new List<IEmployee>();
} 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;
}
}
CSharp设计模式读书笔记(24):访问者模式(学习难度:★★★★☆,使用频率:★☆☆☆☆)的更多相关文章
- Head First 设计模式读书笔记(1)-策略模式
一.策略模式的定义 策略模式定义了算法族,分别封装起来,让它们之间可以互换替换,此模式让算法的变化独立使用算法的客户. 二.使用策略模式的一个例子 2.1引出问题 某公司做了一套模拟鸭子的游戏:该游戏 ...
- CSharp设计模式读书笔记(23):模板方法模式(学习难度:★★☆☆☆,使用频率:★★★☆☆)
模板方法模式:定义一个操作中算法的框架,而将一些步骤延迟到子类中.模板方法模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 模式角色与结构: 实现代码: using System; ...
- CSharp设计模式读书笔记(22):策略模式(学习难度:★☆☆☆☆,使用频率:★★★★☆)
策略模式(Strategy Pattern):定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 模式角色与结构: ...
- CSharp设计模式读书笔记(21):状态模式(学习难度:★★★☆☆,使用频率:★★★☆☆)
模式角色与结构: 示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换.状态模式一定程度上违背开闭原则) using System; using System.Collectio ...
- CSharp设计模式读书笔记(18):中介者模式(学习难度:★★★☆☆,使用频率:★★☆☆☆)
中介者模式(Mediator Pattern):用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互,中介者模式又称为 ...
- CSharp设计模式读书笔记(17):迭代器模式(学习难度:★★★☆☆,使用频率:★★★★★)
迭代器模式(Iterator Pattern):提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor). 模式角色与结构: 实现代码: using System; us ...
- CSharp设计模式读书笔记(15):命令模式(学习难度:★★★☆☆,使用频率:★★★★☆)
命令模式(Command Pattern):将一个请求封装为一个对象,从而让我们可用不同的请求对客户进行参数化:对请求排队或者记录请求日志,以及支持可撤销的操作.命令模式是一种对象行为型模式,其别名为 ...
- CSharp设计模式读书笔记(14):职责链模式(学习难度:★★★☆☆,使用频率:★★☆☆☆)
职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象 ...
- CSharp设计模式读书笔记(13):代理模式(学习难度:★★★☆☆,使用频率:★★★★☆)
代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问. 模式角色与结构: 示例代码: using System; using System.Collections.Generi ...
随机推荐
- mybatis至mysql插入一个逗号包含值误差
mybatis至mysql插入形如"11,22,33"当误差.我使用了错误的原因是美元符号镶嵌sql.正确的做法是使用# 有时间去看看mybatis的$和#差异. 版权声明:本文 ...
- Directx11学习笔记【三】 第一个D3D11程序
在先前的解决方案中新建一个新的Win32项目FirstD3D11Demo.在写代码之前,我们必须先添加dx11所需要的库.为了链接dx库,右键项目选择属性->vc++目录,在包含目录中添加你所安 ...
- 如何找到w3wp与w3svc的对应关系
在生产环境中,一般会有多个IIS进程在运行,这里面可能是有Web Garden的设置,也可能是有多个application pool在运行.而我们经常在c:\inetpub\logs目录下面,看到很多 ...
- oracle在imp订单具体解释
oracle在imp订单具体解释 Oracle导入实用程序(Import utility)同意从数据库中提取数据,和写入数据到一个操作系统文件项目.imp所用的基本格式:imp[username[/p ...
- 为应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制
服务器经常产生“应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制.进程 ID 是 '2068'.”的错误,导致iis处于假死状态,经了解是IIS应用程序池的设置问题.解决 ...
- ueditor使用注意事项
1.js问题的介绍 第一ueditor型材 <script type="text/javascript" src="ueditor1_4_3-utf8-jsp/ue ...
- ORA-38760: This database instance failed to turn on flashback database 第三篇
ORA-38760: This database instance failed to turn on flashback database 第三篇 第一篇 第二篇 问题现象: 在数据库a ...
- JAVA基金会 (三)反射 反思的深度分析
上一页已经推出反映的一些基本概念,这主要是通过一个例子反映谈的过程,以及样品的实际应用. 这个样例是这种设计思路:从一个属性文件里读取一段字符串,然后,依据该字符串生成相应的类实例对象:这之后另一个增 ...
- ASP.NET5 Beta8
ASP.NET5 Beta8 ASP.NET5 beta8现已上都的NuGet作为一个工具升级到Visual Studio2015!此版本极大地扩展.NET核心对OS X和Linux所支持的范围.您现 ...
- SQL Server 内存泄露(memory leak)——游标导致的内存问题
原文:SQL Server 内存泄露(memory leak)--游标导致的内存问题 转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/07/01/sql ...