访问者模式可以包含一些状态吗?
假设此模型类别:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对访客有一个状态是可以的。
但在大多数情况下,它过于复杂。
考虑让访问者具有泛型,您的代码将变成:
访问者界面
带有汽车类
和访问者实现
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
With Car class
And visitor implementation
关于设计模式的书清楚地说明了状态,访问者可以累积状态(第 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 ;-)
我看不出这个设计有什么问题。它看起来像是访问者模式的合理用例。
您已将奖励评估逻辑封装在访问者内部,而不是将其暴露在 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.