The gist of this question is about extending a class, minimizing jam-packing everything into a single class, and maximizing code re-use. After reading this question, please feel free to edit the title or description to make it more succinct. Though the post looks long, I am just trying to be thorough by using a lot of examples.
Suppose I have a class:
class UsedByManyPeople
{
// ...has many fields
};
As the name implies, this class is used by a lot of developers. I have to add 2 features to this class:
Both of them are specific to my department's needs.
At first I thought about simply adding 2 new methods to UsedByManyPeople.Thus, the class will now look like:
class UsedByManyPeople
{
// ...has many fields
public:
SomeOtherType const convert() const;
std::string const getFileName() const;
};
However, the 2 features are actually specific to my department's use case, and other departments do not even have the class definition of SomeOtherType nor do they care about the getFileName().
Clearly, the above approach is not a good approach (?).
How would you extend this class?
Alternatives that came to my mind:
For example,
class ExtUsedByManyPeople : public UsedByManyPeople
{
public:
SomeOtherType const convert() const;
std::string const getFileName() const;
};
For example,
class UsedByManyPeopleToSomeOtherTypeConverter
{
public:
static SomeOtherType const convert(UsedByManyPeople const&);
};
class UsedByManyPeopleFileName
{
public:
static std::string const getFileName(UsedByManyPeople const&);
};
For example,
class UsedByManyPeopleHelper
{
public:
static SomeOtherType const convert(UsedByManyPeople const&);
static std::string const getFileName(UsedByManyPeople const&);
};
Inheritance is not the way to achieve code reuse You might decide to pull the method up in the class hierarchy in order to make it available in all derived classes. The other case is that you identify shared code between two or more classes and decide to create a super class in order to reuse code between the classes.
Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Especially if the methods are specific to your departments use of the class you should implement them as in: Create a single Helper class, with all the methods inside.
There are several reasons for this:
UsedByManyPeople
shouldn't
be responsible for converting itself
into another type. This violates
SOLIDthe last option won't affect anyone else, and isolates your one-off needs into a single class
You shouldn't change the existing UsedByManyPeople class. If your department has preferences on what patterns to use, then stay within your department's style. Otherwise, create the helper class for conversions. If you see multiple conversions down the road you may want to create a class for each conversion. For the getFileName extension, you might want to derive a class (if this is in fact a useful extension) or add it to your helper.
Again, your department's standard (if one exists) should guide you on this. If you don't have a standard, now's the time to create one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With