for our asp.net web application v 4.0, we are in process of defining a class that contains methods that are common across the application. to achieve this there are 2 Suggestions within our team.. one to create a base class, define the methods in that and derive all the other classes from that base class.. the other one is to create a seperate class (not a base class) and instantiate that common class in other classes when required to access the common methods. Please guide me in identifying the best approach..
-
6Your question is rather vague. Both approaches have their time and place. Can you give some concrete examples of what this common functionality does? – JLRishe Mar 19 '14 at 12:56
-
What kind of methods are we talking about? Creating a universal base class is usually a code smell - if you give a specific example of a class and the proposed base class you'll get better answers. – D Stanley Mar 19 '14 at 13:00
-
This can hardly be answered without knowing what exactly you want to achieve. The base class approach should be avoided whenever the single responsibility principle gets violated because of it. – Carsten Mar 19 '14 at 13:00
-
Create a abstract class put those methods there and inherit other classes from that. – Jalpesh Vadgama Mar 19 '14 at 13:01
-
Its depends on the relationship between the classes . If it is is-a relationship then go with first approach. If it Has-a relation ship use second approach. – Binson Eldhose Mar 19 '14 at 14:38
-
possible duplicate of [Prefer composition over inheritance?](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – nawfal Jun 12 '14 at 13:39
2 Answers
I'd only go the base class route if there is a real is-a relationship between the base class and the derived classes. One reason is that a class can only inherit from a single base class. I'd use this relationship in a sensible way. Just sharing some helper methods is not a scenario worth blocking this relationship.
If you want to use some helper methods in several classes, composition is the better way as you describe in the 2nd approach. Instead of creating the objects in the classes, you should think about whether you can inject the instances into the classes (see this link for details on dependency injection), e.g.:
public class HelperClass
{
public virtual void HelperMethod()
{
// ...
}
}
public class ClassThatUsesHelper
{
private readonly HelperClass _helper;
public ClassThatUsesHelper(HelperClass helper)
{
_helper = helper;
}
public void DoSomething()
{
_helper.HelperMethod();
}
}
By injecting the helper class you decouple the classes so that you can substitute the helper class by a different implementation that shares the same interface. ClassThatUsesHelper
works with any class that is derived from HelperClass
(or HelperClass
itself of course). So if you need to decorate the helper method or need a special implementation in some cases, this is possible without any problem.
Using composition also enables you to test the helper methods separately.
However, if it is about very basic helper methods, you might also think about having a static class with static helper methods. Please note that you introduce a strong dependency between the classes and that you cannot adjust the implementation easily.
public static class HelperClass
{
public static void HelperMethod()
{
// ...
}
}
public class ClassThatUsesHelper
{
public void DoSomething()
{
HelperClass.HelperMethod();
}
}

- 20,838
- 4
- 31
- 55
Your question is vague, but if you need a method which all objects in your program will need to have access to, that uses their member variables, then I wold recommend creating an abstract class upon which your objects are based.
If you need a means of performing some sort of calculation from anywhere in your code, just create a public static method in a class meant for the purpose. MyMathClass.InterestingFourierTransform()
, for example.

- 1,171
- 7
- 17