开发者

Design question: Java Class with single method ok?

开发者 https://www.devze.com 2023-01-15 22:10 出处:网络
I need the following functionality Given two sorted lists, merge them I have this skeleton Java code: public class MergeLists{

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 l开发者_Python百科ike 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.


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.


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);


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.


Problem statement:

Given two sorted lists, merge them

How to go about designing? - Start with analysing the problem statement. - oh it has "list" - a noun - oh it has an action "merge"

  • so "merge" ing is an action to be done on a List object. so it should be part of List.

  • since there is a constraint of specific language Java, whos library already has List class, need to create another my.example.List?

  • Have a method merge( my.example.List other list)

  • myfirstList.merge(mySecondList)

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

If your question is "I want to merge two list" (which may not be a 'design' question), then perhaps the solution would be 'use xyz utility class that already has that functionality'

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号