訪問者(Visitor Pattern )模式

2021-09-05 20:12:23 字數 3799 閱讀 3452

主要用途是對聚集採取某種操作,統一執行。

訪問者模式適用於資料結構相對未定的系統,它把資料結構和作用於結構上的操作之間的耦合解脫開,使得操作集合可以相對自由地演化。

比如雇員物件,我們先只有名稱、年齡、工資這些屬性,可以先定義訪問者類來分離結構和操作

the classes and/or objects participating in this pattern are:

concretevisitor(incomevisitor, vacationvisitor)

element(element)

concreteelement(employee)

objectstructure(employees)

using system;

using system.collections.generic;

namespace dofactory.gangoffour.visitor.realworld

///

//////

visitor design pattern.

///class

//////

///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.readkey();

///

///the 'visitor' inte***ce

///inte***ce

ivisitor

void visit(element element);

///

///a 'concretevisitor' class

///class

incomevisitor : ivisitor

public

void visit(element element)

employee employee = element as

employee;

// provide 10% pay raise

employee.income *= 1.10;

console.writeline(" 's new income: ",

employee.gettype().name, employee.name,

employee.income);

///

///a 'concretevisitor' class

///class

vacationvisitor : ivisitor

public

void visit(element element)

employee employee = element as

employee;

// provide 3 extra vacation days

console.writeline(" 's new vacation days: ",

employee.gettype().name, employee.name,

employee.vacationdays);

///

///the 'element' abstract class

///abstract

class

element

public

abstract

void accept(ivisitor visitor);

///

///the 'concreteelement' class

///class

employee : element

private

string _name;

private

double _income;

private

int _vacationdays;

// constructor

public employee(string name, double income,

int vacationdays)

this._name = name;

this._income = income;

this._vacationdays = vacationdays;

// gets or sets the name

public

string name

get

set

// gets or sets income

public

double income

get

set

// gets or sets number of vacation days

public

int vacationdays

get

set

public

override

void accept(ivisitor visitor)

visitor.visit(this);

///

///the 'objectstructure' class

///class

employees

private

list

_employees = new

list

();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();

// three employee types

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)

訪問者模式

訪問者模式 visitor pattern 訪問者模式是物件的行為模式。訪問者模式的目的是封裝一些施加於某種資料結構元素之上的操作。一旦這些操作需要修改的話,接受這個操作的資料結構則可以保持不變。一 問題 集合是大多數的系統都要處理的一種容器物件,它儲存了對其它物件的引用。一般情況下,在集合上採取的...

訪問者模式

行為模式中的訪問者模式 訪問者模式是物件的行為模式。訪問者模式的目的是封裝一些施加於某種資料結構元素之上的操作,一旦這些操作需要修改的話,接受這個操作的資料結構則可以保持不變。動機 類層次結構中可能經常由於引入新的操作,從而將型別變得脆弱。在軟體構建過程中,由於需求的改變,某些類層次結構中常常需要增...

訪問者模式

1.說明 namespace visitorspattern 象棋類 public abstract class chess public class redchess chess public class blackchess chess 在贏的情況下的狀態 public class win ac...