6

I've a Java class that does something like this

public class MyObjectUtil {

    private final MyObject myObject;

    public MyObjectUtil (MyObject myObject){            
        this.myObject = myObject;    
    }

    public boolean isSomeInfoPresentValid() {
        return myObject.isSomething();
    }

    public void modifyMyObjectInSomeWay(String blah) {
        myObject.modify(blah);
    }

}

Every time I've to instantiate my Utility class to interact with a specific instance of MyObject.

myObject is present as a Session obj

MyObjectUtil myObjUtil = new MyObjectUtil(myObject);
myObjUtil.modifyMyObjectInSomeWay("blah");
boolean valid = myObjUtil.isSomeInfoPresentValid();

To achieve the same result I could have static functions that do the same operations using myObject every time as a method param.

public class MyObjectUtil {

    public static boolean isSomeInfoPresentValid(MyObject myObject) {
        return myObject.isSomething();
    }

    public static void modifyMyObjectInSomeWay(MyObject myObject, String blah) {
        myObject.modify(blah);
    }

}

I don't know which way I should go.
I'm using the Util class in my controllers and sometimes I need to call few methods against the same myObject instance, so to me the 1st solution seems the more correct. At the same time in this way as I've many request against my controllers I can end up creating a lot of MyObjectUtil instances everytime as myObject is related to a specific http request.

Which way should I go in my case and how should I choose in other cases?

MyObject is used in specific ways by some Utilities Classes like MyObjectXXXUtil MyObjectYYYUtil. I would like to mantain these Util methods (that modify myObject and check specific status) out of the specific MyObject implementation, as they are not specific to that. Many Util functions can interact with MyObject in a certain way.

spike07
  • 809
  • 2
  • 12
  • 21

2 Answers2

7

If the methods in MyObjectUtil are completely dependent on MyObject then I would just put them in MyObject. A utility class is useful when you have a lot of classes that can use the methods.

JOTN
  • 6,120
  • 2
  • 26
  • 31
  • I agree with that thanks. MyObject is used in specific ways by some Utilities Classes like MyObjectXXXUtil MyObjectYYYUtil. I would like to mantain these Util methods (that modify myObject and check specific status) out of the specific MyObject implementation, as they are not specific to that. Many Util functions can interact with MyObject in a certain way. – spike07 Dec 13 '10 at 15:12
  • 1
    what if the util is for a native java object like TimeZone? What would i do in that case? Would I create a class called TimeZoneUtils or should I create a class that extends TimeZone? – Arvind Sridharan Aug 23 '13 at 06:12
2

Here are some other threads that have discussed this before:

Java Utility Class vs. Service

and

Java Abstract Class or Static Utility Class Design Choice

For me it all depends on state. If the utility class doesn't need state then I go for the static methodology.

Also the methods you describe could arguably exist on the object itself as valid actions to perform on that domain object.

Community
  • 1
  • 1
Martijn Verburg
  • 3,287
  • 21
  • 26