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 ...
随机推荐
- EL字符串表达式和常用功能用途拦截
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ${wj ...
- vuejs 相关资料
官网 http://vuejs.org/ 中文网站 http://cn.vuejs.org/ Vue.js——60分钟快速入门 http://www.cnblogs.com/keepfool/p/56 ...
- 黑马day11 脏读数据&解
数据库: create table account ( id int primary key auto_increment, name varchar(20), money double ); ins ...
- Objective C Runtime 开发介绍
简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...
- 白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连续7-电话问卷调查
[例7-3 文本编辑框创建一个简单的调查问卷] 01 <!DOCTYPEhtml> 02 <html> 03 <head> 04 ...
- [Unity3D] 有关公告板实现的误区
最直接实现一个公告板,我认为我们应该这样做: usingUnityEngine; publicclassBillboard :MonoBehaviour { voidUpdate() { transf ...
- Chapter 1 Securing Your Server and Network(5):使用SSL加密会话
原文:Chapter 1 Securing Your Server and Network(5):使用SSL加密会话 原文出处:http://blog.csdn.net/dba_huangzj/art ...
- OpenGL学习日记-2015.3.13——多实例渲染
实例化(instancing)或者多实例渲染(instancd rendering)是一种连续运行多条同样渲染命令的方法.而且每一个命令的所产生的渲染结果都会有轻微的差异. 是一种很有效的.有 ...
- 数据结构(C达到)------- 双链表
双链表中的每个节点包含两个指针域,指针域包含其后继节点的内存地址,还有一个指针所存储的存储器地址其领域前驱节点. 双向链表结点的类型描写叙述: //双向链表的类型描写叙述 typedef int El ...
- HDU 4832(DP+计数问题)
HDU 4832 Chess 思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行.然后相乘累加起来就是答案 代码: #include <stdio.h> #include < ...