java code in other file

Discussion in 'Java' started by vertigo, Apr 9, 2004.

  1. vertigo

    vertigo Guest

    Hello
    I have quite complicated class, and i would like to move
    one method's body of this class to other file ?
    Is it possible ? How ?

    Thanx
    Michal
    vertigo, Apr 9, 2004
    #1
    1. Advertising

  2. vertigo

    Bjorn Abelli Guest

    "vertigo" wrote...

    > I have quite complicated class, and i would like
    > to move one method's body of this class to other file ?
    > Is it possible ? How ?


    If you mean to separate the one class into several sourcefiles, I don't
    think thats possible.

    On the other hand, who knows what's coming up in the future (I believe that
    this option will be available in next version of .NET/C# for instance).

    However, if you have a too complicated class, there's a possiblity that your
    design of it could improve through abstraction, and hence actually separate
    the methods between more classes.

    "Static" methods are often possible to separate to other classes, but with
    instance methods the only way is through some kind of abstraction.

    To give a *small* example:

    Say you have this class:

    class A
    {
    void first() { ... }
    void second() { ... }
    }

    You could separate the methods with this design:

    abstract class Abstract
    {
    void first() { ... }
    }

    class A extends Abstract
    {
    void second() { ... }
    }

    Without looking at the actual code I can't really tell, but in my experience
    a complicated class or method is often the result of not using patterns for
    low coupling and high cohesion, e.g. polymorphism.

    just my 2c

    // Bjorn A
    Bjorn Abelli, Apr 9, 2004
    #2
    1. Advertising

  3. vertigo

    Roedy Green Guest

    On Fri, 09 Apr 2004 12:16:20 +0200, vertigo <> wrote
    or quoted :

    >I have quite complicated class, and i would like to move
    >one method's body of this class to other file ?
    >Is it possible ? How ?


    If a class is getting too big, best to think about how to simplify.

    Techniques you might use.


    1. a core class with a derived class. The core class has accessors.
    The derived class has the business logic. This makes it easier to
    later create other variants sharing a common core.


    2. Split off some of the fields as separate object with a reference to
    it. If for example you had a Dog class with a huge amount of detail on
    the dog's stomach, you could split off a Stomach class, even though
    all dogs have exactly one stomach.


    3. Split off the static methods into their own class.

    4. Have you created a "Processor" class with a switch to determine
    "food" or "word" mode? If so, create two separate classes.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 10, 2004
    #3
  4. vertigo

    Jim Guest

    Not to detract from Roedy's useful suggestions, but I believe the short
    answer is no (reason being someone forgot to include a preprocessing step
    with Java.)

    One solution would be to put the complete function in interest in it's own
    file as the sole method in a new class, the remainder of the original in its
    own file as another class, then have one extend the other.

    Hope it helps.

    Jim Cant


    "Roedy Green" <> wrote in message
    news:...
    > On Fri, 09 Apr 2004 12:16:20 +0200, vertigo <> wrote
    > or quoted :
    >
    > >I have quite complicated class, and i would like to move
    > >one method's body of this class to other file ?
    > >Is it possible ? How ?

    >
    > If a class is getting too big, best to think about how to simplify.
    >
    > Techniques you might use.
    >
    >
    > 1. a core class with a derived class. The core class has accessors.
    > The derived class has the business logic. This makes it easier to
    > later create other variants sharing a common core.
    >
    >
    > 2. Split off some of the fields as separate object with a reference to
    > it. If for example you had a Dog class with a huge amount of detail on
    > the dog's stomach, you could split off a Stomach class, even though
    > all dogs have exactly one stomach.
    >
    >
    > 3. Split off the static methods into their own class.
    >
    > 4. Have you created a "Processor" class with a switch to determine
    > "food" or "word" mode? If so, create two separate classes.
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Jim, Apr 12, 2004
    #4
    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.

Share This Page