多访问者模式的Java代码
时间: 2024-02-19 07:57:37 浏览: 30
多访问者模式(Multiple Visitor Pattern)是一种设计模式,它可以让多个访问者(Visitor)访问同一个对象结构(Object Structure),并且能够在不改变对象结构的前提下,增加新的访问者。
以下是一个简单的Java代码示例:
```
// 抽象元素
interface Element {
void accept(Visitor visitor);
}
// 具体元素1
class ConcreteElement1 implements Element {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
// 具体元素2
class ConcreteElement2 implements Element {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
// 抽象访问者
interface Visitor {
void visit(ConcreteElement1 element);
void visit(ConcreteElement2 element);
}
// 具体访问者1
class ConcreteVisitor1 implements Visitor {
public void visit(ConcreteElement1 element) {
System.out.println("ConcreteVisitor1 visit ConcreteElement1");
}
public void visit(ConcreteElement2 element) {
System.out.println("ConcreteVisitor1 visit ConcreteElement2");
}
}
// 具体访问者2
class ConcreteVisitor2 implements Visitor {
public void visit(ConcreteElement1 element) {
System.out.println("ConcreteVisitor2 visit ConcreteElement1");
}
public void visit(ConcreteElement2 element) {
System.out.println("ConcreteVisitor2 visit ConcreteElement2");
}
}
// 对象结构
class ObjectStructure {
private List<Element> elements = new ArrayList<>();
public void attach(Element element) {
elements.add(element);
}
public void detach(Element element) {
elements.remove(element);
}
public void accept(Visitor visitor) {
for (Element element : elements) {
element.accept(visitor);
}
}
}
public class Main {
public static void main(String[] args) {
ObjectStructure objectStructure = new ObjectStructure();
objectStructure.attach(new ConcreteElement1());
objectStructure.attach(new ConcreteElement2());
ConcreteVisitor1 visitor1 = new ConcreteVisitor1();
ConcreteVisitor2 visitor2 = new ConcreteVisitor2();
objectStructure.accept(visitor1);
objectStructure.accept(visitor2);
}
}
```
在这个示例中,我们定义了两个具体元素(ConcreteElement1和ConcreteElement2),分别实现了Element接口,并且实现了accept方法,用于接受Visitor访问。
我们还定义了两个具体访问者(ConcreteVisitor1和ConcreteVisitor2),分别实现了Visitor接口,并且实现了visit方法,用于访问具体元素。
最后,我们定义了一个对象结构(ObjectStructure),用于管理元素,并且实现了accept方法,用于接受Visitor访问。
在Main类中,我们创建了一个对象结构,并且向其中添加了两个具体元素。然后,我们创建了两个具体访问者,并且分别调用对象结构的accept方法,让它们访问具体元素。
这样,就实现了多访问者模式。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)