Results 1 to 13 of 13
Thread: Design/code reusage problem
- 01-21-2010, 01:32 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
Design/code reusage problem
Here is my issue.
I have a class. Lets call it GeneralClass. This class has a method, lets call it methodInQuestion(), which I need to change. Here's an example of how the code behaves now:
I need to change it to something like this:Java Code:public void GeneralClass() { methodInQuestion(ParentParameterType a) { doGeneralThing(); }
The problem is however, that this is a poor design solution, since this specific behavior occurs very seldom, but my check would occur each time when this method is used. Instead of changing my general class, I created a new ChildParemeterType class and have overridden the method. That is a much better design solution, however, given that I have to copy-paste quite a large method for just one line of code, this solves my design problem only to create a reusage problem.Java Code:public void GeneralClass() { methodInQuestion(ParentParameterType a) { if(a instanceof ChildParameterType) { doSpecificThing(); } doGeneralThing(); }
So my question is: how can I solve this problem in a way that I don't have to copy-paste and can reuse the code from my methodInQuestion() method.
Any advice or comments are appreciated.
AndrewLast edited by turanan; 01-21-2010 at 03:11 PM.
- 01-21-2010, 01:56 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What is "GeneralType", what is "MoreSpecificSubClassType", what is "a", and how do all of these relate to "GeneralClass".
See, it might help to see the real code. At least the real declaration and definition of the real a, and the real class declarations of the real GeneralClass, GeneralType, MoreSpecificSubClassType, and the real declarations of doGeneralThing, doSpecificThing, and the real declaration of methodInQuestion and the real code of the "instanceof" involved.
- 01-21-2010, 02:00 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 90
- Rep Power
- 0
not very sure. but probably we could make use of strategy design pattern.
- 01-21-2010, 02:14 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
There are several options: if a has one of a fixed set of types (classes) a double dispatch (i.e. a visitor) can do the job; otherwise make an interface with a single method and have all types of a implement that interface; that way your GeneralClass never has to change.
kind regards,
Jos
- 01-21-2010, 02:37 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
Thank you for you reply!
This may help to understand the code:
I'll rename some of the classes and variables to make the code clearer:
I need to change it to something like this:Java Code:public void GeneralClass() { methodInQuestion(ParentParameterType a) { doGeneralThing(); }
ChildParameterType extends ParentParameterTypeJava Code:public void GeneralClass() { methodInQuestion(ParentParameterType a) { if(a instanceof ChildParameterType) { doSpecificThing(); } doGeneralThing(); }
a is basically a parameter of methodInQuestion().The reference to this object is of type ParentParameterType, but the object itself is of the type, that is a subclass of ParentParameterType. The behavior used to be the same for all children of ParentParameterType, but now, specifically if a is of that one specific ChildParameterType subtype, we must make some specific actions.
Here is how a could be declared and initialized:
ParentParameterType a = new ChildParameterType();
now this ChildParameterType is that specific subclass of ParentParameterType, for which I want the specific behaviour
I'm sorry that I can't post the real code, since it is too sensitive, and I really doubt that it would be clearer than the example.Last edited by turanan; 01-21-2010 at 03:10 PM.
- 01-21-2010, 02:45 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Can't "MoreSpecificSubClassType" simply override the "doGeneralThing" method definition? Or how do those methods come into it? Also, is "GeneralClass" abstract? Whether it is or not, you could also simply extend it for the instances where "MoreSpecificSubClassType" would be expected, etc, etc.
- 01-21-2010, 02:49 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
Thanks for the reply!
Interface will basically do the same thing as I have done with creating the subclass of GeneralType and overriding this one method. Interface would be better, if this specific behavior had to be used by several classes, but this is not the case.
I am not familiar with the double dispatch approach, so maybe this could help.
Andrew
- 01-21-2010, 03:07 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-21-2010, 03:32 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 19
- Rep Power
- 0
- 01-21-2010, 10:25 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
Why are you concerned about making an if check? Unless you are very, very concerned about performance this seems like something that just wouldn't make a large impact on things.
- 01-22-2010, 09:21 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The only concern I would have for an if check like that is that it tends to be a sign you've got your model wrong.
- 01-22-2010, 10:19 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
It is; a visitor pattern hides that 'if' but you need a fixed set of classes for that otherwise you have to change the visitor (and its implementation) all the time. IMHO a simple interface for those 'a' classes could do the job; i.e. every class is free to implement whatever it wants. The other side of the medal is that those classes are implementing the functionality instead of that 'GeneralClass'; I don't consider that a disadvantage.
kind regards,
JosLast edited by JosAH; 01-22-2010 at 11:54 AM.
- 01-22-2010, 10:27 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Netbeans 6.8 GUI design problem
By newbiejava in forum NetBeansReplies: 0Last Post: 01-14-2010, 02:02 AM -
Java Design Problem
By hencre in forum Advanced JavaReplies: 2Last Post: 02-25-2009, 07:08 AM -
Problem with code
By jvasilj1 in forum New To JavaReplies: 5Last Post: 02-02-2008, 08:34 AM -
Problem with code
By oregon in forum New To JavaReplies: 3Last Post: 08-05-2007, 05:57 PM -
Problem with zero in my code
By fernando in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks