object oriented..

G

graniteraju

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?
 
D

Dhananjay

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
 
C

Chris Smith

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
 
O

Oliver Wong

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
 
E

Eric Sosman

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

VisionSet

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

James McGill

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.
}
}
 
C

Chris Uppal

Eric said:
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
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top