3

This is more of a best practice / performance related question specific to Android.(I understand this could be a generic discussion but I wanted it to be narrowed down taking into account the Android environment).

Lets say I have a method (which checks for conditions and redirect the user to various activities). This method is used by a lot of classes.

I'm unable to decide whether to add this method in a utility class, make it static and call it from every activity that requires it.

Or

Create a base activity, add the method in there, and any activity that needs to use this method, inherit from the base activity. (Assuming we're Ok with single inheritance in this particular case).

Thoughts ?

EDIT These are the related SO posts I have checked related to this. Couldn't form a decision based on these

Utility classes are evil?

If a "Utilities" class is evil, where do I put my generic code?

Community
  • 1
  • 1
RmK
  • 1,308
  • 13
  • 27
  • 1
    what i would recommend is to invoke the utility to do your work and then create a callback method in your activity to respond to the result, your utility method should not know about the ui-elements – Sarthak Mittal Jun 24 '16 at 07:57
  • @SarthakMittal Thanks. But what I'm interested to know is what helped you in making this decision to go with utility class rather than using a base activity, moving the methods in there and inheriting this class by any of the activity using this method ? – RmK Jun 24 '16 at 09:24
  • 2
    i generally keep the inheritance procedure as the last resort because we can only extend one class, it's better to use interfaces if only some classes are gonna use the method, although, if all of your activities are going to need the methods then it's alright to create a base class – Sarthak Mittal Jun 24 '16 at 10:05
  • Agree. Single inheritance is definitely something to consider. But like I mentioned in question, I was wondering if that wasn't an issue, what other factors would affect my decision to go for Utility class. Anyway, this link has been pretty useful to come to a conclusion - http://javarevisited.blogspot.com/2013/06/why-favor-composition-over-inheritance-java-oops-design.html – RmK Jun 24 '16 at 11:10

1 Answers1

1

There would also be the possibility to inject the class in the constructor when it is needed. When you use this approach you could easily mock this class behind an interface and it is possible to create tests with a mock.

As example some pseudo code:

Your Utility class:

public class Utility implements IUtility {
    @Override
    public void add() { ... }

    @Override
    public void remove() { ... }
}

Your Utility using class:

public class UtilityUsingClass {
    private IUtility utility;
    public UtilityUsingClass(IUtility utility) {
        this.utility = utility;
    }

    public void myMethod() {
        // Use Utility class
        utility.add();
        ...
    }
}

In the test it can look like this:

@Test
public void testMyMethod() {
    UtilityUsingClass testClass = new UtilityUsingClass(new UtilityMock());
    testClass.myMethod();

    // assert stuff
}

The UtilityMock:

public class UtilityMock implements IUtility {
    @Override
    public void add() { // Implements mock logic ... }

    @Override
    public void remove() { // Implements mock logic ... }
}
Kevin Wallis
  • 3,585
  • 3
  • 22
  • 39
  • Thanks. I'm not sure I quite follow what you meant there. – RmK Jun 24 '16 at 09:26
  • Great. That clarifies your answer. What I'm really interested in is - what helped you in making this decision to go with utility class rather than using a base class, moving the methods in there and any child class inheriting the methods from this base class ? – RmK Jun 24 '16 at 09:42
  • 1
    there are many reasons some of them are listed on this website (http://javarevisited.blogspot.com/2013/06/why-favor-composition-over-inheritance-java-oops-design.html) also the ability to test and to change the injected classes is very fine to use – Kevin Wallis Jun 24 '16 at 09:52
  • and also the usage of an IOC container is given. – Kevin Wallis Jun 24 '16 at 09:53