object oriented..

Discussion in 'Java' started by graniteraju@gmail.com, May 5, 2006.

  1. Guest

    I have a method in 4 diffenrent classes. the method in all the classes
    is the same except for some minor things. I want to replace the method
    in all the classes by a single method. how do I do that?
     
    , May 5, 2006
    #1
    1. Advertising

  2. Dhananjay Guest

    consider using Containment, this may solve ur problem.
    Try this way..

    Class Container{
    public static returntype commonMethod(args){
    }
    }

    Class 1{
    specializedMethod(){
    Container.commonMethod(args)
    }
    }

    Class 2{
    specializedMethod(){
    Container.commonMethod(args)
    }
    }



    .......



    Regards
    Dhananjay
     
    Dhananjay, May 5, 2006
    #2
    1. Advertising

  3. Chris Smith Guest

    <> wrote:
    > I have a method in 4 diffenrent classes. the method in all the classes
    > is the same except for some minor things. I want to replace the method
    > in all the classes by a single method. how do I do that?


    Define "except for some minor things". There may be any number of good
    answers, depending on what those minor things are. You could refactor
    common pieces to a single method, or make use in a couple of ways of OO
    design patterns called Template Method or Strategy, for starters. But
    those could be the wrong way for your situation, depending...

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, May 5, 2006
    #3
  4. Oliver Wong Guest

    <> wrote in message
    news:...
    >I have a method in 4 diffenrent classes. the method in all the classes
    > is the same except for some minor things. I want to replace the method
    > in all the classes by a single method. how do I do that?
    >


    Are they static or instance methods?

    In the former case, they can just delegate to each other. In the latter
    case, the 4 classes would probably need to inherit from a common ancestor.

    - Oliver
     
    Oliver Wong, May 5, 2006
    #4
  5. Eric Sosman Guest

    wrote On 05/05/06 14:54,:
    > I have a method in 4 diffenrent classes. the method in all the classes
    > is the same except for some minor things. I want to replace the method
    > in all the classes by a single method. how do I do that?


    If the classes are descended from a common superclass,
    put the method in the superclass.

    If the method is static, remove it from three of the
    classes and have their code refer to it in the fourth
    class as SurvivingClass.method(42). Or remove it from
    all four and put it in a new non-instantiable Utilities
    class, and write Utilities.method(42).

    If the methods are instance methods and the classes
    have no common ancestor (and cannot be made to have a
    common ancestor), perhaps your design needs rearranging.
    Maybe each of the four classes, instead of having a bunch
    of ints and Strings and whatnot, should have an instance
    of a new fifth class containing all these things and being
    the home for the magic method.

    If none of the above holds, you probably don't have
    "a" method in four different classes, but four different
    methods with a superficial similarity. The similarities
    are just coincidences of the implementations as they stand
    today and may disappear as the four classes continue to
    develop independently, so it would be a mistake to try to
    combine them.

    --
     
    Eric Sosman, May 5, 2006
    #5
  6. VisionSet Guest

    <> wrote in message
    news:...
    > I have a method in 4 diffenrent classes. the method in all the classes
    > is the same except for some minor things. I want to replace the method
    > in all the classes by a single method. how do I do that?
    >


    You have it working I imagine so you have your solution.
    Well, that is one end of a design continuum, the other end is to either give
    us the full details of your problem domain and have us solve it, or arrive
    at it yourself after much learning, trial & error etc.
    The problem is, that the kind of question you are asking, is the very nub of
    good OO design, and impossible to answer with a 4 sentence question, since
    there are so many ways of solving it.

    --
    Mike W
     
    VisionSet, May 5, 2006
    #6
  7. James McGill Guest

    On Fri, 2006-05-05 at 11:54 -0700, wrote:
    > I have a method in 4 diffenrent classes. the method in all the classes
    > is the same except for some minor things. I want to replace the method
    > in all the classes by a single method. how do I do that?
    >


    Break the method into at least two steps, and divide the common part
    from the part that is different for each use case. This technique is
    called "functional decomposition."

    Put the common method into an abstract class from which all of your four
    different classes derive. Each of these four classes has its own
    implementation of the different function, which is called from the
    common function in the superclass. This is one form of "method
    polymorphism."

    public abstract class BaseType {

    public void methodOne(){
    // Do some common work
    //...
    // Do some specialized work
    methodTwo(args);
    }
    public abstract void methodTwo(Object args);
    }


    public class DerivedType {
    public void methodTwo(Object args){
    // each class implements this
    // and methodOne will call it according to the
    // runtime type.
    }
    }
     
    James McGill, May 6, 2006
    #7
  8. Chris Uppal Guest

    Eric Sosman wrote:

    > If none of the above holds, you probably don't have
    > "a" method in four different classes, but four different
    > methods with a superficial similarity. The similarities
    > are just coincidences of the implementations as they stand
    > today and may disappear as the four classes continue to
    > develop independently, so it would be a mistake to try to
    > combine them.


    Just wanted to add some support for this option.

    The point of OO is /not/ that it reduces code duplication, but that it produces
    coherent units of behaviour. The key question to ask in these situations is
    /why/ are the methods the same ? Once you have understood that (and the answer
    might be "it's just a coincidence") then you'll have the guidance you need to
    tell whether there's a new object there trying to get out, or a bit utility
    code which could be better factored, or what.

    -- chris
     
    Chris Uppal, May 6, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Hathaway
    Replies:
    0
    Views:
    424
    Robert Hathaway
    Jul 29, 2003
  2. Robert Hathaway
    Replies:
    0
    Views:
    423
    Robert Hathaway
    Jul 29, 2003
  3. Robert Hathaway
    Replies:
    1
    Views:
    464
    Robert J Hathaway III
    Jul 29, 2003
  4. Replies:
    2
    Views:
    438
    Bruno Desthuilliers
    May 26, 2008
  5. rolo
    Replies:
    3
    Views:
    189
    Robert Klemme
    Apr 9, 2004
Loading...

Share This Page