访问者模式可以包含一些状态吗?

发布于 2025-01-04 03:20:10 字数 977 浏览 0 评论 0原文

假设此模型类别:

public class Car extends Vehicle implements Visitable {
  .....
  void accept(VehicleVisitor visitor){
    visitor.visit(this);
  }  
  .....
}

使用访问者,因为允许授予某些车辆的决定是很晚才做出的(在创建汽车类别之后)。

还有一个特定的 Visitor 层次结构,其基类名为 VehicleEvaluatorVisitor,继承自 CarVisitor,其目标是告知车辆是否值得奖励:

public class VehicleEvaluatorVisitor implements VehicleVisitor {

  boolean mustBeAwarded;

  public visit(Car car){
    ....
    mustBeAwarded= true;   //after some conditional
  }

  .... //other visit methods

  boolean mustVehicleBeAwarded(){
    return mustBeAwarded;
  }

}

此设计的目标是允许客户端迭代任何车辆集合,以便知道哪辆车必须获奖获奖者:

  ...
    VehicleEvaluatorVisitor visitor = new VehicleEvaluatorVisitor ();
    for(Vehicle vehicle : vehicules){
      vehicle.accept(visitor);
      boolean mustToBeAwarded = visitor.mustVehicleBeAwarded();
      .....
  ...

我的问题是:这是一个可以接受的设计吗? (当然,汽车和奖励概念只是一个理论上的例子)

Assume this model class :

public class Car extends Vehicle implements Visitable {
  .....
  void accept(VehicleVisitor visitor){
    visitor.visit(this);
  }  
  .....
}

Use of visitor because the decision to permit certains vehicles to be awarded was made very later (after the creation of Car class).

And a specific hierarchy of Visitor with a base class named VehicleEvaluatorVisitor inheriting from CarVisitor whose goal is to inform whether vehicle merits awards or not :

public class VehicleEvaluatorVisitor implements VehicleVisitor {

  boolean mustBeAwarded;

  public visit(Car car){
    ....
    mustBeAwarded= true;   //after some conditional
  }

  .... //other visit methods

  boolean mustVehicleBeAwarded(){
    return mustBeAwarded;
  }

}

The goal of this design is to allow client to iterate through any collections of Vehicle in order to know which vehicle must to be awarded :

  ...
    VehicleEvaluatorVisitor visitor = new VehicleEvaluatorVisitor ();
    for(Vehicle vehicle : vehicules){
      vehicle.accept(visitor);
      boolean mustToBeAwarded = visitor.mustVehicleBeAwarded();
      .....
  ...

My question is : Is this an acceptable design ?
(Of course, Car and award concept is just a theorical example)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2025-01-11 03:20:10

对访客有一个状态是可以的。

但在大多数情况下,它过于复杂。

考虑让访问者具有泛型,您的代码将变成:

访问者界面

public interface VehicleVisitor<T> {
    ...
    T visit(Car car);
    ...
}

带有汽车类

public class Car extends Vehicle implements Visitable {
    ...
    <T> T accept(VehicleVisitor<T> visitor){
        return visitor.visit(this);
    }
    ...
}

和访问者实现

public class VehicleEvaluatorVisitor implements VehicleVisitor<Boolean> {
    
    public Boolean visit(Car car){
        ...
        return true;
        ...
    }
    
}

It's okay to have a state for a visitor.

But in most cases it is overcomplicated.

Consider having a visitor with a generic and your code will turn into:

Visitor interface

public interface VehicleVisitor<T> {
    ...
    T visit(Car car);
    ...
}

With Car class

public class Car extends Vehicle implements Visitable {
    ...
    <T> T accept(VehicleVisitor<T> visitor){
        return visitor.visit(this);
    }
    ...
}

And visitor implementation

public class VehicleEvaluatorVisitor implements VehicleVisitor<Boolean> {
    
    public Boolean visit(Car car){
        ...
        return true;
        ...
    }
    
}
柠檬色的秋千 2025-01-11 03:20:10

关于设计模式的书清楚地说明了状态,访问者可以累积状态(第 336 页“后果”部分第 5 点)。剩下的就是实现细节;-)

The book on Design Pattern cleary state, that Visitors can accumulate state (point 5 in section "Consequences", page 336). The rest is an implementation detail ;-)

怎樣才叫好 2025-01-11 03:20:10

我看不出这个设计有什么问题。它看起来像是访问者模式的合理用例。

您已将奖励评估逻辑封装在访问者内部,而不是将其暴露在 Car 类或客户端类中。这是使用访问者模式的目的之一。当然,如果需要,您以后还可以添加更多类型的访问者。

I can't see any problem with this design. It looks like a reasonable use case for the visitor pattern.

You've encapsulated the award evaluation logic inside the visitor, instead of exposing it in either the Car class or the client class. This is one of the aims of using the visitor pattern. And, of course, you'll be able to add more types of visitor later if you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文