Results 1 to 13 of 13
  1. #1
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default 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:

    Java Code:
    public void GeneralClass() {
        methodInQuestion(ParentParameterType a) {
            doGeneralThing();
    }
    I need to change it to something like this:

    Java Code:
    public void GeneralClass() {
        methodInQuestion(ParentParameterType a) {
                if(a instanceof ChildParameterType) {
                    doSpecificThing();
                }
                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.

    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.
    Andrew
    Last edited by turanan; 01-21-2010 at 03:11 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    not very sure. but probably we could make use of strategy design pattern.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cbani View Post
    not very sure. but probably we could make use of strategy design pattern.
    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

  5. #5
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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.
    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:
    Java Code:
    public void GeneralClass() {
        methodInQuestion(ParentParameterType a) {
            doGeneralThing();
    }
    I need to change it to something like this:

    Java Code:
    public void GeneralClass() {
        methodInQuestion(ParentParameterType a) {
                if(a instanceof ChildParameterType) {
                    doSpecificThing();
                }
                doGeneralThing();
    }
    ChildParameterType extends ParentParameterType
    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.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by turanan View Post
    I am not familiar with the double dispatch approach, so maybe this could help.
    Double dispatch is also known under the name 'visitor pattern' (as I wrote) and is one of the patterns mentioned in the GoF book.

    kind regards,

    Jos

  9. #9
    turanan is offline Member
    Join Date
    Mar 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Double dispatch is also known under the name 'visitor pattern' (as I wrote) and is one of the patterns mentioned in the GoF book.

    kind regards,

    Jos
    Thank you, I will read it up.

  10. #10
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    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.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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.
    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,

    Jos
    Last edited by JosAH; 01-22-2010 at 11:54 AM.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Yes.
    To me it implies that the wrong class is doing the work, and it probably ought to be done by the class/object you're calling instanceof on. Hence your interface suggestion.

Similar Threads

  1. Netbeans 6.8 GUI design problem
    By newbiejava in forum NetBeans
    Replies: 0
    Last Post: 01-14-2010, 02:02 AM
  2. Java Design Problem
    By hencre in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2009, 07:08 AM
  3. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 08:34 AM
  4. Problem with code
    By oregon in forum New To Java
    Replies: 3
    Last Post: 08-05-2007, 05:57 PM
  5. Problem with zero in my code
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •