简体   繁体   English

使用抽象类而不是策略设计模式中的接口

[英]Using an abstract class instead of the interface in the Strategy design pattern

I'm just learning Java and design patterns and I am trying to get my head around when to use interfaces and abstract classes. 我只是在学习Java和设计模式,而我正试图在使用接口和抽象类时思考。 I am wondering in the Strategy design pattern, why is it preferable to use an interface for the behaviour/algorithm subclasses rather than an abstract superclass? 我想在策略设计模式中,为什么最好使用行为/算法子类的接口而不是抽象的超类?

Is it simply because there is no need for an abstract class because each behaviour/algorithm subclasses have their own implementation and therefore an abstract superclass would just provide extra functionality that won't be used? 是不是因为不需要抽象类,因为每个行为/算法子类都有自己的实现,因此抽象超类只提供不会使用的额外功能?

Wouldn't an abstract class mean that there is an extra possibilty to use this extra functionality in the future if needed, for instance adding a method to the abstract superclass which is shared by the behaviour/algorithm subclasses if this is needed. 如果需要,抽象类不会意味着将来可能会额外使用此额外功能,例如向抽象超类添加一个方法,如果需要,该方法由行为/算法子类共享。 Is there any reason why this is a bad idea? 有什么理由说这是个坏主意吗?

Or is there another reason? 还是有其他原因吗?

You should use a common interface for all your strategies and also create an abstract class that implements it. 您应该为所有策略使用通用接口,并创建实现它的抽象类。 So, although all your strategies share an abstract class currently, your system is extendable because you can create unrelated strategies to this abstract one in the future. 因此,尽管您的所有策略目前都共享一个抽象类,但您的系统是可扩展的,因为您可以在将来为此抽象类创建不相关的策略。

Now using only some methods of the superclass is a decision you should make. 现在只使用超类的一些方法是你应该做出的决定。 It may be worthy or not. 这可能是值得的。

note:: If you are using something similar to the Template Method Pattern you should check how to use hooks. note ::如果您使用类似于模板方法模式的东西,您应该检查如何使用钩子。

Before Java 8, interfaces had one big issue: it was impossible to add additional functionality (ie add new methods) without breaking every implementation of that interface. 在Java 8之前,接口有一个大问题:在不破坏该接口的每个实现的情况下,无法添加其他功能(即添加新方法)。 Abstract base classes were a way to avoid this problem. 抽象基类是避免这个问题的一种方法。

Java 8 introduced default methods , which mostly addresses that issue. Java 8引入了默认方法 ,主要解决了这个问题。 So with a modern Java system, interfaces provide much more flexibility without the downside. 因此,使用现代Java系统,接口提供了更大的灵活性而没有缺点。

From an implementation point of view you can use both, interface and abstract class. 从实现的角度来看,您可以同时使用接口和抽象类。 The difference is in the purpose of use. 不同之处在于使用目的。

  • The main purpose of interfaces is to provide methods (ie communication points) to other classes. 接口的主要目的是为其他类提供方法(即通信点)。 Ie decoupling and inversion of dependency . 依赖性的 解耦反转
  • The main purpose of abstract classes is to help you build an inheritance structure upon them. 抽象类的主要目的是帮助您在它们上构建继承结构。

In the strategy pattern that means, if you use an AbstractState to define a common code base, you could still put an IState interface between the Context and the AbstractState to decouple them more. 在策略模式中,如果您使用AbstractState来定义公共代码库,您仍然可以在ContextAbstractState之间放置一个IState接口来更多地分离它们。

There's no hard rule between using an interface or an abstract pattern. 使用接口或抽象模式之间没有硬性规则。 Use which ever is best in your situation. 在您的情况下使用最好的。

If you have multiple Strategy implementations that are very similar of course you can use abstract classes or even subclass one of the Strategies. 如果您有多个非常相似的策略实现,那么您可以使用抽象类甚至是其中一个策略的子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM