5

I have three objects; Action, Issue and Risk. These all contain a nunber of common variables/attributes (for example: Description, title, Due date, Raised by etc.) and some specific fields (risk has probability). The question is:

  1. Should I create 3 separate classes Action, Risk and Issue each containing the repeat fields.

  2. Create a parent class "Abstract_Item" containing these fields and operations on them and then have Action, Risk and Issue subclass Abstract_Item. This would adhere to DRY principal.

7 Answers 7

5

My Perspective

Let's say, if you used inheritance. Over a period you have new attributes that are common to only Action and Issue but not Risk. How will you handle this? If you put them under parent then Risk is inheriting stuff that is irrelevant (Liskov Substituon Principle knocking?). If you put then in Action and Risk separately then you are breaking DRY, the initial reason why you started inheritance. Point is Inhertence for re-use is bad. If there is no "is-a" then better not use it and when you are in doubt then there is no real "is-a".

My Preference

There are other ways of achieving DRY as shown in below example code. With this addition of new properties my be another Common2, addition of new behavior is new CommonBehavior2 if they are not applicable to all 3 classes; if they are then just change existing Common and CommonBehavior

public class Common implements CommonBehavior
{
    String Description;
    String title;

    public void f() {}
}

public interface CommonBehavior
{
    void f();
}

public class Action implements CommonBehavior
{
    private Common delegate;

    public void f()
    {
        delegate.f();
    }
}

Also look at my answer to a similar question with another practical example Design pattern to add a new class

3
  • Thank you for throwing in delegation and the good presentation. What do you think of the other way round (see beyond, Task -> Topic). The question is, is the "due date" really part of Action (your delegation) or an artifact of the workflow that does play a role not within dealing with Action, only in the workflow context embedding.
    – mtraut
    Dec 31, 2010 at 9:14
  • Thank you for showing Delegate so clearly. What I have for Action, Issue and Risk is indeed a set of common behaviour. Using the delegate approach will make the workings of the classes much clearer when reading the start of the code. Taking example of "Task" below, Action may be a task but Risk is not. The intention of the classes becomes very clear as Action is now: "Action implements CommonBehaviour, Task, MeetingIem", where Risk is: "Risk implements CommonBehaviour, MeetingItem".
    – poulenc
    Jan 1, 2011 at 15:51
  • I am glad you understood it. I suggest you also go through my response here to a similar question stackoverflow.com/questions/4512184/… Jan 1, 2011 at 18:04
2

Yes, adhering to DRY is usually a very good idea except if the classes have very, very different uses (i.e. both apples and cars may be red, still I wouldn't derive both of them from a base class called ProbablyRed). In your case, however, I'd definitely go for a base class since the implementations you describe (Action, Issue, Risk) all seem to be some kind of business rule with very similar semantics.

0

You seem to be answering this yourself. As you say, DRY.

0

The abstract parent class sounds like the way to go. It will also make it possible or easier (depending on your language) to implement and use functions which act on any of the three items. For example, you could have a "Get a list of all items raised by {user}" function.

0

Another facet may become visible if you look at the use cases, probably dealing with different subgroups of the properties.

If for example "label", "due date" and "raised" are used in a todo like application and other properties of "Action" and "Risk" will be prevalent when working on that task, you might consider an aggregation of lets say Task (label, due date,...) and Topic which is a polymorphic reference to things like Action, Issue or whatever will come up someday

0

I guess I have answered the same question here

When to create a class vs setting a boolean flag?

0

Don't subclass just because objects share some data or operations. Consider composition as the default way to follow DRY.

In your particular case, only create a parent class if your objects are actually related, i.e. there's a semantic "is a" relationship to the parent class. For example, if Action, Issue, and Risk are all Ticket objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.