java code in other file

V

vertigo

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
 
B

Bjorn Abelli

...
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
 
R

Roedy Green

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.
 
J

Jim

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top