Results 1 to 3 of 3

Thread: Design question

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default Design question

    Suppose that you have 2 classes you deal with, class A and class B.
    Both classes work with Collections and need to make sure that certain data structure contains no duplicates.
    Both classes contain methods (slightly different) that take care of it. Method is 1 line long (take blah, return set)

    While working fine, from design perspective, i wonder if such an approach is acceptable. Should there perhaps be a class C which contains those methods? The only concern i have- is how prudent it is to create an object of class C inside both A and B for such a small task as gaining access to one method?

    Second part of the question extends the scenario a bit. Suppose you have 10 classes A-J, each of which at different times needs to sort, get unique set, format a string ... blah blah blah .. do stuff that does not have anything to do with the main purpose of that class.

    Is it acceptable from design perspective to perhaps have a common utils class which in tuen will take care of all of these requests?

    Again, in this example object of that class will be created 10 times and called G-d knows how many times. Is this economical?

    Thank you.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Utility classes usually have static methods, just so you can avoid creating an instance of that class every time you need a small snippet of it's functionallity. So, instead of this:
    Java Code:
    public class Something {
      ArrayUtils au;
    
      public void someMethod() {
        au.doSomething(someParameter);
      }
    }
    you can take the static aproach:
    Java Code:
    public class Something {
      public void someMethod() {
        ArrayUtils.doSomething(someParameter);
      }
    }
    So, no istantiation needed. For an example of this, take a look at the Integers parseInt() method. Why would you create an instance of integer, just to get the int value of a String?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 01:21 PM
  3. question on j2ee design pattern
    By Minu in forum Advanced Java
    Replies: 1
    Last Post: 01-06-2009, 07:50 PM
  4. Design question
    By kfunk in forum New To Java
    Replies: 4
    Last Post: 10-10-2008, 03:33 AM
  5. design question comments wanted - essential class init()
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 07-22-2008, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •