Jeremy G
Jeremy G

Reputation: 568

Apply design pattern to reduce code duplication

I have the following class that all other Activities extend from. Here is some pseudocode:

public class CommonActivity extends Activity
{
    //class member variables

    protected void setupInterface(String activityName)
    {
        //a bunch of setup code here
        Button select = new Button;
        Button create = new Button;
        Button delete = new Button;
    }
    protected void resetScreen()
    {
        selectButton.highlight();
        createButton.unhighlight();
        deleteButton.unhighlight();
    }
}

I have five or six other classes that extend CommonActivity, and do something like the following:

 public class SectionActivity extends CommonActivity
{
    //class member variables

    protected void onCreate()
    {
        setupInterface("sectionActivity");
    }
}

Now I have another Activity that extends from CommonActivity that is just a tiny bit different than the other activities. I found myself writing the following code:

public class ExActivity extends CommonActivity
{
    //class member variables

    protected void onCreate()
    {
        setupInterface2("sectionActivity");
    }
}

and then adding the following methods to CommonActivity (in addition to what was already there):

protected void setupInterface2(string activityName)
    {
        //a bunch of setup code here
        Button select = new Button;
    }
protected void resetScreen2()
    {
        selectButton.highlight();
    }

Now I find myself wondering the best way to eliminate the code duplication. The comments in the code that say "a bunch of setup code" will never change among activities. Should I be using some sort of design pattern here? I toyed around with the idea of the Template Pattern, but then each of the five classes that use identical code for setupInterface would have that duplicate code in their respective classes while ExActivity would have its own implementation (hopefully that sentence even made sense). I also thought about using the Strategy Pattern to encapsulate the unique code in setupInterface into their own class. That seems reasonable to me, but I feel like there might be something more elegant. I am even thinking at this point that I should just remove setupInterface2 and resetScreen2 from CommonActivity and simply override those methods in ExActivity. That seems to be the simplest solution. Any suggestions here would be great!

Upvotes: 2

Views: 1281

Answers (1)

Seeta Somagani
Seeta Somagani

Reputation: 787

Instead of thinking about the problems in terms of design patterns, I'd suggest describing your classes clearly so that you can implement the patterns that exist naturally among them. When you have 6 classes extending from one class where 5 of them have similar behavior and one doesn't, perhaps the inheritance model of your classes is not right. Maybe it makes sense to compose the 5 classes that are similar with a class that implements the common logic and the black sheep can implement its special logic by itself.

Just a thought, but I'd first describe the classes clearly in a single, simple sentence each and then implement any patterns that might come out of those naturally than trying to fit them into one or another pre-defined patterns.

Upvotes: 7

Related Questions