tags:

views:

85

answers:

3

I need the following functionality

Given two sorted lists, merge them

I have this skeleton Java code:

public class MergeLists{
   public List merge(List l1, List l2){
      List l3;
      // merge l1, l2 in to l3
      return l3;
   }

   public static void main(){
      // populate list1 and list2
      MergeLists ml = new MergeLists();
      List l3 = ml.merge(l1,l2);
   }
}

Is this single method class the right approach? I feel like the almost-empty class is staring at me to say that this is bad design. I initially had List L3 as private member of MergeLists but then I thought, merge(l1,l2) can be called multiple times with the same object, which required l3 to be local to merge(l1,l2). I read that using static method is even worse for code re-usability. Please advise. Thank you.

+3  A: 

In this case, since you have no true member data, making the single method a static method inside the class would be the appropriate design choice:

public class ListUtils
{
    public static List Merge(List l1, Listl2)
    {
        List l3 = new List();
        // merge l1 and l3 into l3
        return l3;
    }
}

You can then use the code without having to create an instance of your class (especially when it serves no purpose):

List l1 = new List();
List l2 = new List();
// Fill the lists

List merged = ListUtils.Merge(l1, l2);
Justin Niessner
You can't have top-level static classes in java.
aioobe
@aioobe - Sorry. Got keyword happy. Fixed.
Justin Niessner
Thank you Justin and Erick. I am trying to reconcile the static approach with the chosen answer to this question which essentially says using static is a bad idea: http://stackoverflow.com/questions/205689/class-with-single-method-best-approach
snk
@user426716 - Pay attention to the accepted answer's point of "Only a Sith deals in absolutes". He mentions that, in some cases, a class with only static methods makes sense. Your case is one of those.
Justin Niessner
Thank you Justin.
snk
@Justin +1 For Star Wars reference ^^
Helper Method
+3  A: 

You can do this, but I think you want the merge method to be static. This will make sure that you don't have to instantiate it before calling the method. You can just do this:

List l3 = MergeLists.merge(l1,l2);

Additionally, if this is the only method and it's static, you can make the class abstract which means it cannot be instantiated.

Erick Robertson
... or make the constructor private
Stroboskop
There is no constructor.
Erick Robertson
I usually make my util class final and add a unique private no-op constructor. This is to forbids instantiation AND derivation.
gawi
@Erick: there certainly is :) The implicit one.
BalusC
Please explain how to make the implicit constructor private. Hint: you might be able to emulate this by adding a certain keyword to the class.
Erick Robertson
A: 

Static methods are not necessarily bad - it just depends on the context in which they are being used. Examples off the top of my head where this happens:

File.separator; // a static representation of the file separator for your platform.
File.listRoots();  // list root filesystems

Now, the case where you are simply applying your listutils has been covered already (see other answers), however, you might be doing more, for example:

class SortedList implements List<T>

Where all added items are automatically sorted into place - as such, it doesn't make sense for the item to be static because you want the results to be stored in this instance. If you try this under eclipse you'll find you need to override quite a few methods anyway including add and addAll which would be equivalent to a merge.

So, I'd say it depends on what you're doing in the long run and how the object should act.

Ninefingers