简体   繁体   English

基类与实用类

[英]Base class vs Utility class

Which of the two should be preferred? 应该首选哪两个?

There are some methods which are called by class A, B and C. 有一些方法由A,B和C类调用。

Should those methods be encapsulated in a class D (base of A, B and C) ? 这些方法是否应该封装在D类(A,B和C的基础)中?

OR 要么

Should those methods be encapsulated in a class U and other classes creats it's object to use the methods as required. 如果将这些方法封装在类U和其他类创建中,则它的对象是根据需要使用这些方法。

On what basis decision should be taken? 在什么基础上应该做出决定?

Thanks. 谢谢。

You should make a static utility class. 你应该创建一个static实用程序类。

Only use inheritance if it's actually meaningful—if A , B , and C actually are a D . 如果实际上有意义的话,只有使用继承 - 如果ABC实际上 D

I'd base the decision on what the methods are doing, if they're doing things specific to classes A, B and C then they should be in the base class. 我根据方法的作用做出决定,如果他们正在做特定于A,B和C类的事情,那么他们应该在基类中。 This helps keep code clean, by hiding class-related functionality away from the rest of the system. 这有助于保持代码清洁,方法是将类相关功能隐藏在系统的其他部分之外。 (of course, I'm assuming that A, B and C either already inherit from D, or are obviously related) (当然,我假设A,B和C已经从D继承,或者显然是相关的)

If they're doing things with other types, that aren't inherent in what A, B and C do, then in order to maximise opportunities for reuse they should be in a utility class. 如果他们正在使用其他类型的东西,这不是A,B和C所固有的,那么为了最大化重用的机会,他们应该在实用程序类中。

If they're doing things with other types that are specific to that other type (eg pretty-printing a datetime) consider making them extension methods for that type. 如果他们正在做的与其他类型的特定于其他类型的东西(比如漂亮的印刷日期时间)考虑邀请他们担任类型的扩展方法。

I would tend away from inheritance unless there's an obvious is-a relationship. 除非有明显关系,否则我会偏离继承。 I suspect from your description above that this is not the case. 我怀疑你上面的描述并非如此。 My preferred solutions would be: 我首选的解决方案是:

  1. inject an instance of a utility class into your A, B, C 实用程序类的实例注入 A,B,C
  2. have A, B, C instantiate the appropriate utility classes 让A,B,C实例化适当的实用程序类

The advantage of injecting the class is that you can provide different implementations trivially. 注入类的优点是可以简单地提供不同的实现。 This is especially useful for testing. 这对测试特别有用。 Singletons or classes with static methods tend to cause problems for the same reason - you can't easily override or substitute them. 使用静态方法的单身人士或类会因同样的原因而导致问题 - 你不能轻易地覆盖或替换它们。

Use Base Class If you are going to write some logic only depending on the base class - then it makes sense to create a base class. 使用基类如果要根据基类编写一些逻辑 - 那么创建基类是有意义的。 In that case your derived class should be completely substitutable for your base class. 在这种情况下,派生类应该完全可替代您的基类。 There should not be any switch logic to check the derived type and act accordingly. 不应该有任何切换逻辑来检查派生类型并相应地采取行动。 See Liskov Substitution Principle: http://en.wikipedia.org/wiki/Liskov_substitution_principle 见Liskov替代原则: http//en.wikipedia.org/wiki/Liskov_substitution_principle

Use Utility Class Some languages have the limitation that they do not support multiple inheritance. 使用实用程序类某些语言具有不支持多重继承的限制。 If you already have a meaningful base class then you need to go for the utility class. 如果您已经有一个有意义的基类,那么您需要去实用程序类。 Also, when you are using inheritance your are creating tight coupling between from the dervied class to the base class. 此外,当您使用继承时,您正在创建从dervied类到基类之间的紧密耦合。

I would go for base class if the derived class is naturally substitutable for its base class. 如果派生类自然可以替代它的基类,我会选择基类。 If the purpose is just to share some reusable code between classes, then it makes more sense to go with utility class. 如果目的只是在类之间共享一些可重用的代码,那么使用实用程序类更有意义。

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

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