程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#設計模式之Visitor設計模式(訪問者設計模式)(3)

C#設計模式之Visitor設計模式(訪問者設計模式)(3)

編輯:關於C語言

四、一個實際應用Visitor模式的例子

以下的例子演示了Employee對象集合允許被不同的Visitor(IncomeVisitor與VacationVisitor)訪問其中的內容。

// Visitor pattern -- Real World example 
using System;
using System.Collections;
// "Visitor"
abstract class Visitor
{
 // Methods
 abstract public void Visit( Element element );
}
// "ConcreteVisitor1"
class IncomeVisitor : Visitor
{
 // Methods
 public override void Visit( Element element )
 {
  Employee employee = ((Employee)element);
  // Provide 10% pay raise
  employee.Income *= 1.10;
  Console.WriteLine( "{0}'s new income: {1:C}",
   employee.Name, employee.Income );
 }
}
// "ConcreteVisitor2"
class VacationVisitor : Visitor
{
 public override void Visit( Element element )
 {
  Employee employee = ((Employee)element);
  // Provide 3 extra vacation days
  employee.VacationDays += 3;
  Console.WriteLine( "{0}'s new vacation days: {1}",
   employee.Name, employee.VacationDays );
 }
}
// "Element"
abstract class Element
{
 // Methods
 abstract public void Accept( Visitor visitor );
}
// "ConcreteElement"
class Employee : Element
{
 // FIElds
 string name;
 double income;
 int vacationDays;
 // Constructors
 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; }
 }
 // Methods
 public override void Accept( Visitor visitor )
 {
  visitor.Visit( this );
 }
}
// "ObjectStructure"
class Employees
{
 // FIElds
 private ArrayList employees = new ArrayList();
 // Methods
 public void Attach( Employee employee )
 {
  employees.Add( employee );
 }
 public void Detach( Employee employee )
 {
  employees.Remove( employee );
 }
 public void Accept( Visitor visitor )
 {
  foreach( Employee e in employees )
   e.Accept( visitor );
 }
}
/**//// <summary>
/// VisitorApp test
/// </summary>
public class VisitorApp
{
 public static void Main( string[] args )
 {
  // Setup employee collection
  Employees e = new Employees();
  e.Attach( new Employee( "Hank", 25000.0, 14 ) );
  e.Attach( new Employee( "Elly", 35000.0, 16 ) );
  e.Attach( new Employee( "Dick", 45000.0, 21 ) );
  // Create two visitors
  IncomeVisitor v1 = new IncomeVisitor();
  VacationVisitor v2 = new VacationVisitor();
  // Employees are visited
  e.Accept( v1 );
  e.Accept( v2 );
 }
}

-
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved