【发布时间】:2009-08-09 11:03:27
【问题描述】:
我似乎没有在访问者模式的使用场景中找到这一点(或者我可能不明白)。它也不是分层的。
让我们使用一个身份验证示例。 UserAuthenticator 对用户提供的凭据进行身份验证。它返回一个结果对象。结果对象包含身份验证的结果:身份验证成功,未成功,因为未找到用户名,未成功,因为使用了非法字符等。客户端代码可能会求助于条件来处理此问题。 在伪代码中:
AuthResult = Userauthenticator.authenticate(Username, Password)
if AuthResult.isAuthenticated: do something
else if AuthResult.AuthFailedBecauseUsernameNotFound: do something else
else if etc...
访客模式适合这里吗? :
Authresult.acceptVisitor(AuthVisitor)
Authresult 然后根据结果调用 AuthVisitor 上的方法:
AuthVisitor.handleNotAuthenticatedBecauseUsernameNotFound
【问题讨论】:
标签: oop design-patterns visitor-pattern conditional