Base class vs Utility class
Asked Answered
D

4

8

Which of the two should be preferred?

There are some methods which are called by class A, B and C.

Should those methods be encapsulated in a class D (base of A, B and C) ?

OR

Should those methods be encapsulated in a class U and other classes creats it's object to use the methods as required.

On what basis decision should be taken?

Thanks.

Donothing answered 15/3, 2011 at 13:11 Comment(3)
Is there any conceptual relationship between classes A, B, and C?Chockfull
How about making an interface and implement that by A,B,C?Afghanistan
@p: only that they construct data used by class D. A requires different inputs than other classes. @Srinivas: the implementation of those methods is exactly same for A, B and C.Donothing
S
12

You should make a static utility class.

Only use inheritance if it's actually meaningful—if A, B, and C actually are a D.

Sequin answered 15/3, 2011 at 13:12 Comment(5)
yes, i guess. D has methods that invoke some algorithms of three types. A, B and C construct data specific to algorithm and call those common methods for execution.Donothing
@Azo: Detail always helps. In that case, perhaps you should use inheritance, with an abstract method overridden by A, B, and C.Sequin
I totally agree with your second point, but I would only advise the use of a static utility class where the utility methods have no state (i.e no static variables) and perform no operations with side-effects (file IO or database activity etc). If you're unit testing A, B and C, it's (almost) impossible to decouple them from a static utility class with mock objects. It's much better to use a non-static utility class that is passed to A/B/C via constructor injection. Then you code is highly configurable and acquires behaviours via aggregation.Outgrowth
@Azodius - Have a look at the Strategy design pattern (en.wikipedia.org/wiki/Strategy_pattern). You have an abstract base class, and subclasses that customise the algorithm. Sounds like exactly what you need.Outgrowth
@Xcal: You're right, but you're making unjustified assumptions about the quality of his codebase.Sequin
U
3

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. 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)

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.

If they're doing things with other types that are specific to that other type (e.g. pretty-printing a datetime) consider making them extension methods for that type.

Unorganized answered 15/3, 2011 at 13:16 Comment(1)
+1: You beat me to it. As SLaks mentions it also seems the OP isn't aware of static functions. Perhaps worth mentioning that as well for the utility class.Subotica
U
2

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
  2. have A, B, C instantiate the appropriate utility classes

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.

Usurp answered 15/3, 2011 at 13:15 Comment(0)
I
-1

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

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.

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.

Instinct answered 15/3, 2011 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.