建模问题(接口与抽象类)

发布于 2024-10-20 04:39:11 字数 323 浏览 0 评论 0原文

我有些疑问何时使用抽象类以及是否需要始终编写接口。一个例子:

我将有一系列自定义实体,所有这些实体都需要实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-10-27 04:39:11

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.

探春 2024-10-27 04:39:11

你是对的,对于 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 is Move(Coordinate destination), an abstract parent class is better. If, on the other hand, different classes have nothing in common and the method is SerializeToJSON(), 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 implement AnotherMethod().

樱花坊 2024-10-27 04:39:11

接口和抽象类之间的一个很大的区别是,抽象类可以有一些实现,其中接口是严格的契约和数据类型。在您给出的示例中,您可以使用接口来要求实现 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.

怼怹恏 2024-10-27 04:39:11

考虑用聚合替换继承是一个很好的情况。

我会将 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 a getAnotherMethodRunner() to a base interface. If AnotherMethod() is a property of derived class, it will have one, if not - it will return null or Null Object.

I personally usually take a nonempty abstract base class as a call to more precise interface extraction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文