建模问题(接口与抽象类)
我有些疑问何时使用抽象类以及是否需要始终编写接口。一个例子:
我将有一系列自定义实体,所有这些实体都需要实现 SomeMethod(),其中大多数需要实现 AnotherMethod() 方法。
SomeMethod() 将是特定于实体的,每个实体将有不同的代码。
AnotherMethod() 被大多数人实现,但不是全部,并且代码对于所有人来说都是相同的。
这是如何建模的?我的想法是每个新实体必须实现 SomeMethod() 并能够使用 AnotherMethod()。
谢谢, 戈兰
I have some doubts when to use abstract class and if I need to always code interface. An example:
I have will have series of custom entities, and all of them need to implement SomeMethod() and most of them need to implement AnotherMethod() method.
SomeMethod() will be entity specific, each entity will have different code.
AnotherMethod() is implemented by most, but not all, and the code is the same for all.
How is this modeled? My idea is that each new entity must implement SomeMethod() and is able to use AnotherMethod().
Thanks,
Goran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AnotherMethod 可能应该在抽象类中实现,这样您就不会到处重复代码。
如果 SomeMethod 是相关功能,则可以将其保留在同一个抽象类中而不进行实现,从而迫使子级实现它。如果该功能与 AnotherMethod 无关,您可以将其放在自己的接口中。
AnotherMethod should likely be implemented in an abstract class so you don't repeat the code all over the place.
If SomeMethod is related functionaloty, it could be left in the same abstract class without an implementation, forcing children to implement it. If the functionality is not related to AnotherMethod, you could put it in its own interface.
你是对的,对于
SomeMethod()
来说,使用抽象父类和抽象方法是一个好主意。您还可以使用接口,具体取决于方法的含义。例如,如果不同的类代表不同的动物并且方法是Move(坐标目的地)
,则抽象父类更好。另一方面,如果不同的类没有任何共同点,并且方法是SerializeToJSON()
,那么您应该更好地使用接口。如果
AnotherMethod()
由某些类实现,那么您可以再次使用抽象父类(具有非抽象受保护/公共方法)。当然,不要从此父类继承不必实现AnotherMethod()
的类。You're right, for
SomeMethod()
, using abstract parent class with abstract method is a good idea. You can also use interfaces, depending on the meaning of the method. For example, if different classes represent different animals and the method isMove(Coordinate destination)
, an abstract parent class is better. If, on the other hand, different classes have nothing in common and the method isSerializeToJSON()
, you should better use interfaces.If
AnotherMethod()
is implemented by some of the classes, again, you can use an abstract parent class (with a non-abstract protected/public method). Of course, don't inherit from this parent the classes which do not have to implementAnotherMethod()
.接口和抽象类之间的一个很大的区别是,抽象类可以有一些实现,其中接口是严格的契约和数据类型。在您给出的示例中,您可以使用接口来要求实现 SomeMethod 和 AnotherMethod,但您无法为 AnotherMethod 编写任何代码,因为该接口只有一个方法签名。
在抽象类中,您可以将 SomeMethod 定义为抽象,因此需要从它继承的类的实现,但您也可以创建 AnotherMethod 的实现并拥有单个实现,因为您说它对于许多类来说都是相同的。
A big difference between interfaces and abstract classes is that the abstract class can have some implementation where the interface is strictly a contract and data type. In the examples you give, you could use an interface to require the implementation of both SomeMethod and AnotherMethod but you wouldn't be able to write any code for AnotherMethod since the interface would just have a method signature.
In an abstract class you could define SomeMethod as abstract and therefore require an implementation from classes which inherit from it but you could also create the implementation of AnotherMethod and have a single implmentation since you say that it will be the same for a lot of your classes.
考虑用聚合替换继承是一个很好的情况。
我会将
AnotherMethod()
提取到其他类,例如AnotherMethodRunner
,并将getAnotherMethodRunner()
添加到基本接口。如果AnotherMethod()
是派生类的属性,它将有一个,如果不是 - 它将返回null
或空对象。我个人通常采用非空抽象基类作为更精确的接口提取的调用。
A good situation to think of replacing inheritance with aggregation.
I'd extract
AnotherMethod()
to other class, say,AnotherMethodRunner
, and add agetAnotherMethodRunner()
to a base interface. IfAnotherMethod()
is a property of derived class, it will have one, if not - it will returnnull
or Null Object.I personally usually take a nonempty abstract base class as a call to more precise interface extraction.