惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
小众软件
小众软件
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
Last Week in AI
Last Week in AI
H
Help Net Security
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
腾讯CDC

博客园 - 点点滴滴

C#操作IIS的代码 恢复误删数据(SQL Server 2000)--Log Explorer 如何让ClickOnce进行手动更新(含代码) - 点点滴滴 - 博客园 BackgroundWorker 组件 获取VS.NET 自带的数据库连接对话框的数据库连接 搜索一个局域网中所有的SQL Server服务器 Application.DoEvent() 在C#使用XML注释 用IDisposable接口释放.NET资源 正确的重载operator 很好的debug有理由不用吗 C#调用API访问其它进程 抽象 虚方法 接口 的区别 ASP.NET AJAX 路线图 ASP.NET AJAX 概述 安装ASP.NET AJAX Template Method Strategy State
Visitor
点点滴滴 · 2006-10-29 · via 博客园 - 点点滴滴

// Visitor pattern -- Real World example


using System;
using System.Collections;

namespace DoFactory.GangOfFour.Visitor.RealWorld
{
  
  // MainApp startup application

class MainApp
  {
    static void Main()
    {
      // Setup employee collection
      Employees e = new Employees();
      e.Attach(new Clerk());
      e.Attach(new Director());
      e.Attach(new President());

// Employees are 'visited'
      e.Accept(new IncomeVisitor());
      e.Accept(new VacationVisitor());

// Wait for user
      Console.Read();
    }
  }

// "Visitor"

interface IVisitor
  {
    void Visit(Element element);
  }

// "ConcreteVisitor1"

class IncomeVisitor : IVisitor
  {
    public void Visit(Element element)
    {
      Employee employee = element as Employee;

// Provide 10% pay raise
      employee.Income *= 1.10;
      Console.WriteLine("{0} {1}'s new income: {2:C}",
        employee.GetType().Name, employee.Name,
        employee.Income);
    }
  }

// "ConcreteVisitor2"

class VacationVisitor : IVisitor
  {
    public void Visit(Element element)
    {
      Employee employee = element as Employee;
      
      // Provide 3 extra vacation days
      Console.WriteLine("{0} {1}'s new vacation days: {2}",
        employee.GetType().Name, employee.Name,
        employee.VacationDays);
    }
  }

class Clerk : Employee
  {
    // Constructor
    public Clerk() : base("Hank", 25000.0, 14)
    {
    }
  }

class Director : Employee
  {
    // Constructor
    public Director() : base("Elly", 35000.0, 16)
    {  
    }
  }

class President : Employee
  {
    // Constructor
    public President() : base("Dick", 45000.0, 21)
    {  
    }
  }

// "Element"

abstract class Element
  {
    public abstract void Accept(IVisitor visitor);
  }

// "ConcreteElement"

class Employee : Element
  {
    string name;
    double income;
    int vacationDays;

// Constructor
    public Employee(string name, double income,
      int vacationDays)
    {
      this.name = name;
      this.income = income;
      this.vacationDays = vacationDays;
    }

// Properties
    public string Name
    {
      get{ return name; }
      set{ name = value; }
    }

public double Income
    {
      get{ return income; }
      set{ income = value; }
    }

public int VacationDays
    {
      get{ return vacationDays; }
      set{ vacationDays = value; }
    }

public override void Accept(IVisitor visitor)
    {
      visitor.Visit(this);
    }
  }

// "ObjectStructure"

class Employees
  {
    private ArrayList employees = new ArrayList();

public void Attach(Employee employee)
    {
      employees.Add(employee);
    }

public void Detach(Employee employee)
    {
      employees.Remove(employee);
    }

public void Accept(IVisitor visitor)
    {
      foreach (Employee e in employees)
      {
        e.Accept(visitor);
      }
      Console.WriteLine();
    }
  }
}