访问器模式(Visitor)
“行为变化”类模式
在组件的构建过程中,组件行为的变化经常导致组建本身剧烈的变化。“行为变化”类模式将组件的行为和组件本身进行解耦,从而支持组件行为的变化,实现两者之间的松耦合。
访问器模式(Visitor)简介:
表示一个作用于某个对象结构中各元素(子类)的操作。使得可以在不改变各元素(子类)的前提下来定义或者添加扩展作用于这些元素的新操作。
访问器模式(Visitor)结构:
C++代码
Element对象及其结构下的各元素(子类)ConcreteElementA、ConcreteElementB
//file: Element.h
#pragma once
class Visitor;
class Element
{
public:
Element();
virtual ~Element();
virtual void accept(Visitor& visitor) = 0;
};
class ConcreteElementA : public Element {
void accept(Visitor& visitor) override;
};
class ConcreteElementB : public Element {
void accept(Visitor& visitor) override;
};
//file: Element.cpp
#include "pch.h"
#include "Element.h"
#include "Visitor.h"
Element::Element() {}
Element::~Element() {}
void ConcreteElementA::accept(Visitor& visitor){
visitor.visitElementA(*this);
}
void ConcreteElementB::accept(Visitor& visitor){
visitor.visitElementB(*this);
}
Visitor类作用于该Element对象结构下各元素(子类)的操作
//file: Visitor.h
#pragma once
#include "Element.h"
class Visitor
{
public:
Visitor();
virtual ~Visitor();
virtual void visitElementA(Element& element) = 0;
virtual void visitElementB(Element& element) = 0;
};
class Visitor1 : public Visitor {
public:
virtual void visitElementA(Element& element) override;
virtual void visitElementB(Element& element) override;
};
class Visitor2 : public Visitor {
public:
virtual void visitElementA(Element& element) override;
virtual void visitElementB(Element& element) override;
};
//file: Visitor.cpp
#include "pch.h"
#include "Visitor.h"
#include <iostream>
using namespace std;
Visitor::Visitor() {}
Visitor::~Visitor() {}
void Visitor1::visitElementA(Element & element){
cout << "Visitor 1 is visiting ElementA ." << endl;
}
void Visitor1::visitElementB(Element & element){
cout << "Visitor 1 is visiting ElementB ." << endl;
}
void Visitor2::visitElementA(Element & element){
cout << "Visitor 2 is visiting ElementA ." << endl;
}
void Visitor2::visitElementB(Element & element){
cout << "Visitor 2 is visiting ElementB ." << endl;
}
客户端代码:
// VisitorPattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include "pch.h"
#include "Element.h"
#include "Visitor.h"
#include <iostream>
int main()
{
Element* elementB = new ConcreteElementB();
Visitor* pVisitor1 = new Visitor1();
elementB->accept(*pVisitor1);
return 0;
}
运行输出:
C#代码
Element对象及其结构下的各元素(子类)ConcreteElementA、ConcreteElementB
public abstract class Element
{
public abstract void Accept(Visitor visitor) ;
}
public class ConcreteElementA : Element
{
public override void Accept(Visitor visitor)
{
visitor.visitElementA(this);
}
}
public class ConcreteElementB : Element
{
public override void Accept(Visitor visitor)
{
visitor.visitElementB(this);
}
}
Visitor类作用于该Element对象结构下各元素(子类)的操作
public abstract class Visitor
{
public abstract void visitElementA(Element element);
public abstract void visitElementB(Element element);
}
public class Visitor1 : Visitor
{
public override void visitElementA(Element element)
{
Console.WriteLine("Visitor1 is visiting ElementA .");
}
public override void visitElementB(Element element)
{
Console.WriteLine("Visitor1 is visiting ElementB .");
}
}
public class Visitor2 : Visitor
{
public override void visitElementA(Element element)
{
Console.WriteLine("Visitor2 is visiting ElementA .");
}
public override void visitElementB(Element element)
{
Console.WriteLine("Visitor2 is visiting ElementB .");
}
}
客户端代码
class Program
{
static void Main(string[] args)
{
Element e = new ConcreteElementB();
Visitor v1 = new Visitor1();
e.Accept(v1);
Console.Read();
}
}
运行输出:
REF
书籍:
设计模式与游戏开发、大话设计模式
GitHub: