gpt4 book ai didi

java - 具有多次访问实现的访问者模式

转载 作者:行者123 更新时间:2023-12-01 18:04:34 25 4
gpt4 key购买 nike

我需要对组合模式进行各种操作。我使用复合模式来实现访问者模式。

界面:

public interface Visitor {
void visit(DayPart dayPart);
void visit(DayParting dayParting);
void visit(TimePart timePart);
}

复合类中的访问者方法:

public void accept(Visitor visitor) {
visitor.visit(this);
Iterator<AbstractComposite> itr = iterator();
Parting children;
while (itr.hasNext()) {
children = (Parting) itr.next();
children.accept(visitor);
}
}

叶子上的访问者方法:

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

访问者类实现:

public class myVisitor implements Visitor {
@Override
public void visit(DayPart dayPart) {
//do something (or not)
}

@Override
public void visit(DayParting dayParting){
// do something (or not)
}

@Override
public void visit(TimePart timePart){
//do something (or not)
}
}

问题是我需要对组合进行各种操作,并且通过这种设计,我需要为每次“访问”我需要做的事情创建一个新类,这是非常不切实际的。

所以,我想用内部类做这样的事情(我输入通用名称)。

public abstract class VisitorImpl implements Visitor {
//Hook methods
@Override
public void visit(Composite composite) {
//hok method
}

@Override
public void visit(Composite2 composite2) {
//hook method
}

@Override
public void visit(Leaf leaf) {
//hook method
}

public static Visitor ParticularVisitor(){
return new ParticularVisitor();
}
private static class ParticularVisitor extends VisitorImpl {
@Override
public void visit(Composite composite) {
//do something real.
}
@Override
public void visit(Composite2 composite2) {
//do something real.
}

@Override
public void visit(Leaf leaf) {
//do something real.
}
}
private static class ParticularVisitor_2 extends VisitorImpl {
public ParticularVisitor_2(){
}
@Override
public void visit(Leaf leaf){
//Just do something with this
}
}
}

这是解决我的问题的好方法吗?有什么改进吗?

最佳答案

Visitor Design Pattern

Intent

  • Represent an operation to be performed on the elements of an object structure.
  • Visitor lets you define a new operation without changing the classes of the elements on which it operates.
  • The classic technique for recovering lost type information.
  • Do the right thing based on the type of two objects.
  • Double dispatch

Problem

Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid "polluting" the node classes with these operations. And, you don't want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation.

来自:Sourcemaking

所以,你的“问题”

"The problem is that i need do various operations over the Composition and with this design i need to do a new class for every "visit" what i need to do and this is pretty impractical."

不完全是一个问题,问题是 Visitor 应该如何工作。

One operation to be performed = one visitor

如果您的操作之间没有关系,那就是您可以使用 Visitor 做的最好的事情。

关于java - 具有多次访问实现的访问者模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37688355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com