访问者模式与Java 8默认方法 [英] Visitor pattern with Java 8 default methods

查看:136
本文介绍了访问者模式与Java 8默认方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

访问者模式(双重调度)是其自身权限中非常有用的模式,但是如果任何新成员添加到继承层次结构中,这是一个有效的点,它经常被仔细检查破解接口。



但是在Java中引入了默认方法之后8,现在我们可以在界面中定义默认实现,客户端接口不会中断,客户端可以适当地优雅地采用更改的接口。

  interface Visitor {
public void visit(Type1 type);
public void visit(Type2 type);

//第一个版本的访问者发布后添加
default public void visit(NewType type){
//某些默认实现
}

$ / pre

现在使用默认方法,如果新的类型 NewType,则不再破坏客户端代码将来会推出。



这是否使访问者更加可接受和有用?

解决方案

问题包含一个访问者必须是接口的隐含断言。由于访问者模式不是特定于Java的,所以不要求这样的实现。



事实上,世界使用访问者抽象类,或使用界面,但提供 abstract 实现类同时。



虽然此评论在编译时检测到未处理的案例的可能性是有效的,这仅适用于每个访问者总是为每个访问方法提供实现。当你有很多情况(这可能会导致其他开发人员为他们的访问者编写自己的 abstract 基础类),这可能是一个很大的代码膨胀。



如上所述,并不是每个人都以这种方式使用访问者模式。许多实现使用抽象类来提供空的访问方法或访问方法,委托另一个访问方法采取更抽象的类型。对于这些实现,添加新的类型从来不是一个问题。



而且,为了回答您的问题,以某种方式使用访问者模式不强制每个访问者为每个方法提供一个实现,在接口中使用 default 方法是一个选项。但是,这并不会使得访问者模式更加可接受和有用,因为它并没有真正的问题。使用抽象访问者类的选项始终存在。


Visitor pattern (double dispatch) is a very useful pattern in its own rights, but it has often been scrutinized of breaking interfaces if any new member is added to the inheritance hierarchy, which is a valid point.

But after the introduction of default methods in Java 8, now that we can define default implementation in interfaces, the client interfaces will not break and clients can gracefully adopt the changed interface as appropriate.

interface Visitor{
   public void visit(Type1 type);
   public void visit(Type2 type);

   //added after the first version of visitor is released
   default public void visit(NewType type){
        //some default implementation
   }
}

Now with default methods no more breakage of client code if new type NewType is introduced in future.

Does this make Visitor more adoptable and useful?

解决方案

Your question contains the implicit assertion that a Visitor has to be an interface. Since the Visitor pattern is not Java specific, it does not mandate such an implementation.

In fact, there are a lot of uses around the world using an abstract class for the Visitor or using an interface but providing an abstract implementation class at the same time.

While this comment has a valid point of the possibility to detect unhandled cases at compile time this applies only to the case where every visitor always has to provide implementations for every visit method. This can be quite a code bloat when you have a lot of cases (And may cause other developers to write their own abstract base class for their visitors).

As said, not everyone uses the Visitor pattern this way. A lot of implementations use abstract classes for providing empty visit methods or visit methods which delegate to another visit method taking a more abstract type. For these implementations, adding a new type never was an issue.

And, to answer your question, when using the Visitor pattern in a way not forcing every Visitor to provide an implementation for every method, using default methods in interfaces is an option. But it does not make the Visitor pattern "more adoptable and useful" as there never was a real problem with it. The option to use an abstract visitor class always existed.

这篇关于访问者模式与Java 8默认方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆