Design Question: Java class with one ok method?

I need the following functionality

Given two sorted lists, merge them 

I have Java skeleton 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 a class right approach? I feel like an almost empty class is looking at me to say that this is a bad design. Initially, I had the L3 list as a private member of MergeLists, but then I thought that merging (l1, l2) could be called several times with the same object, which required that l3 be local to merge (l1, l2). I read that using a static method is even worse for code reuse. Please advise. Thanks.

+4
source share
4 answers

In this case, since you do not have true member data, the only way the static method does the only method inside the class will 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; } } 

Then you can use the code without creating an instance of your class (especially if this is impractical):

 List l1 = new List(); List l2 = new List(); // Fill the lists List merged = ListUtils.Merge(l1, l2); 
+3
source

You can do this, but I think you want the merge method to be static . This will make sure that you do not need to instantiate before calling the method. You can simply do this:

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

In addition, if this is the only method and it is static, you can make the class abstract , which means that it cannot be created.

+4
source

Static methods are not necessarily bad - it depends only on the context in which they are used. Examples from the top of the head where this occurs:

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

Now the case when you just apply your listutils has already been considered (see other answers), however you can do more, for example:

 class SortedList implements List<T> 

When all the added elements are automatically sorted in place - as such, it does not make sense for the element to be static, because you want the results to be stored in this instance. If you try this under eclipse, you will find that you need to override many methods, including add and addAll , which will be equivalent to merge .

So, I would say it depends on what you do in the end and how the object should act.

0
source

Description of the problem:

Given two sorted lists, combine them

How to do it? - Start by analyzing the problem. - Oh, he has a "list" - a noun - oh has the action "merge"

  • therefore, "merge" ing is the action to be performed on the List object. therefore, it must be part of the list.

  • since there is a restriction on a specific Java language, the whos library already has a List class, do I need to create another my.example.List file?

  • You have a merge method (my.example.List another list)

  • myfirstList.merge (mySecondList)

If you want multiple lists to be combined to create another new list, you can create a util class that accepts var-args MyListUtil {static List getMergedList (List ... listOfLists)

If your question is “I want to combine the two lists” (it may not be a “design” question), then perhaps the solution will be “use the xyz utility class that already has this functionality”

0
source

All Articles