Results 1 to 18 of 18
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default Correct architecture when breaking up a class?

    Howdy all.

    I have a question that is more architectural today.

    Basically I want to make my classes as easy to maintain and as logical laid out as I possibly can. I currently have one class that is used to do an almost identical operation 3 times. Each time it does something slightly different. The only real differences are that a handful of variables get values assigned slightly differently and one object gets created from a different class. Right now I have a simple class that just takes in a parameter that gets processed by an "else if" ladder to do the appropriate action.

    For example:

    Java Code:
    if (parameter == "x")
    {
       // create object needed for the "x"
    }
    else if (parameter == "y")
    {
       // create object needed for the "y"
    }
    ...
    I do this in just a handful of places throughout the code but it seems to make the code a bit slower (even though the class is only called once with the parameter "x" this sort of 'else if' ladder is often contained in a loop that is iterated through lots and lots of times). What should I do to effectively break this up? I imagine eliminating these else-ifs and breaking this thing up would make the class a lot faster and would make it more flexible later should I wish to add other kinds of calls. Otherwise I need to keep extending this else-if or create a case statement to handle all the possibilities in every instance and it is going to get silly.

    Inheritance? This to me seems like the smartest option but I thought I should ask for the input of someone with more experience. Some variables would need to change but realistically most of the logic would remains unchanged between the 3 different calls (x, y and z).

    Make an interface? Seems silly because I'm not changing the logic enough to warrant totally open-ended abstract methods. I would need to maintain 3 separate versions (and possibly later more) of the same code.

    I hope I have explained myself adequately. If I haven't please let me know what I can do to further clarify.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Depends...:)

    There is rarely a hard and fast rule for these things, but depending on what "parameter" is, and whether x, y and z would make sense to abstract out, then yes, break them out. This sort of if/else switching based on an unchanging attribute of an object does tend to point to some potential class restructuring.

    In your case, if there is a lot of shared logic you could use an abstract class, or you could go for the...other one...oh bugger...I'm going senile. What's the bloody term?

    COMPOSITION! That's it...

    Ahem...You could go for composition, "parameter" becomes an interface, with methods representing what you do in those if/else blocks, and x,y,z become implementations.

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Right now what I mean by parameter is that it literally is a parameter being handed to the classe's central method by another class. The class then changes it's behaviour slightly depending on which parameter is receives. The changes are small. Like... change a single line or assign a slightly different variable or create a different internal object that is used in the same way but generates different results. I'm talking about changing maybe 10 lines of logic at most depending on the value of the parameter.

    The problem is with an abstract class you can't have actual code and 90% of the code stays the same which is why I was thinking inheritance because it would make maintenance so much easier than needing to change 3 identical implementations every single time I make a small change. Normally I wouldn't bother and I would just leave it all together because it is working. It is just that there are so many iterations that these else-ifs are creating serious problems. Things just take too long.

    I think I will try inheritance with overrides for the few methods that change.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Another option to consider is to extend your code using design pattern(s). For example, your problem above could lend itself to use of the Command Pattern I think, much like you would change the behavior of a Swing JButton by adding Actions or ActionListeners to it.

    edit: sorry, ActionListeners are better described as examples of the observer pattern. A better example of command pattern in Java would be using a Runnable object to change the behavior of a Thread.
    Last edited by Fubarable; 09-26-2010 at 01:34 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    If a parameter value makes the difference of the behaviour, let the Parameter handle the different behaviour:

    Java Code:
    public interface Parameter {
       public void handle( ... );
    }
    Different implementations of the Parameter interface handle the actual behaviour of the 'main' class object:

    Java Code:
    public class Main {
       ...
       public void method(Parameter x) {
          ...
          x.handle( ... );
          ...
       }
    }
    kind regards,

    Jos

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    If a parameter value makes the difference of the behaviour, let the Parameter handle the different behaviour:...
    Hm, smells like the Command Pattern. :)

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

    Default

    Quote Originally Posted by Fubarable View Post
    Hm, smells like the Command Pattern. :)
    Yes it is, I just worded it in terms of the OP's original question (most likely in a feeble attempt to make it more understandable ;-)

    kind regards,

    Jos

  8. #8
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If a parameter value makes the difference of the behaviour, let the Parameter handle the different behaviour:

    Java Code:
    public interface Parameter {
       public void handle( ... );
    }
    Different implementations of the Parameter interface handle the actual behaviour of the 'main' class object:

    Java Code:
    public class Main {
       ...
       public void method(Parameter x) {
          ...
          x.handle( ... );
          ...
       }
    }
    kind regards,

    Jos
    I'm still thinking it might be best to use inheritance with an override on the handful of methods that are slightly altered. It just seems like a massive waste for a single line in a whole method or the addition of one 'if' statement. It would definitely be faster though.

    I'm not 100% sure I understand what you're saying here I'm going to need to look up this command pattern to learn a bit more about it. It looks as though you store the key changes in program logic as methods inside the object 'x' and then call them when needed. That way you can make 'x' an interface to ensure that all objects whether 'x' 'y' or 'z' all behave appropriately in the right situation with the right methods. Have I got that about right?

    The problem is in one instance I assign values based upon previous logic in the class that generates multiple values. Depending upon the parameter I assign different values to a certain variable. I don't know how I would do that with this command pattern. I suppose create a separate version of that whole system (both the part that generates the values and the part that assigns them) for each object (e.g. 'x', 'y' and 'z').

    This system makes sense actually because then only the variable sections of code get separated and the logic that remains unchanged stays in a central class.

  9. #9
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Yes it is, I just worded it in terms of the OP's original question (most likely in a feeble attempt to make it more understandable ;-)

    kind regards,

    Jos
    LOL.

    Sorry about the first post I was trying to make it as simple as possible :o

    My day to day Java knowledge is not that bad but as an architect I'm totally useless. I normally end up sticking everything in a few massive classes with like 2000 lines each. Which is fine for me because I know exactly what is going on. The problem comes along when someone else needs to read it and I get moaned at. I feel it is time for me to improve this skill (or lack thereof). Is there any reading you guys would recommend for this sort of thing? I know that architecture is very much a personal thing but I really would like to arrange my code a little more... intuitively. Surely there are some rough guidelines?
    Last edited by porchrat; 09-26-2010 at 02:47 PM.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by porchrat View Post
    .... I know that architecture is very much a personal thing but I really would like to arrange my code a little more... intuitively. Surely there are some rough guidelines?
    It's more than a personal thing; object-oriented design a whole field in and of itself. You might want to consider checking out:

    Code Complete
    The Pragmatic Programmer
    Head First Object-Oriented Analysis and Design
    Head First Design Patterns
    Design Patterns: Elements of Reusable Object-Oriented Software: also known as the "Gang-of Four" book
    Object-Oriented Analysis and Design with Applications (3rd Edition)


    edit: Ha! More links than Jos! Take that you Grolsch-chugging Netherlander! :)
    Last edited by Fubarable; 09-26-2010 at 03:06 PM.

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

    Default

    Quote Originally Posted by Fubarable View Post
    edit: Ha! More links than Jos! Take that you Grolsch-chugging Netherlander! :)
    It is Sunday and I'm lazy and I didn't want to scare away the OP with a load of crappy links (like you probably did ;-) And above all: I replied first, so there :-P

    kindest regards,

    Jos

    ps. hm, Grolsch, good idea! ;-)

  13. #13
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Thanks for those helpful suggestions guys. I can see I have a lot of reading to do. I'm glad to see there are resources available to help out with this.

    With this command patter thing do I have it right when I say this:

    It looks as though you hand an object 'x' as a parameter and store the key changes in program logic as a collection of methods inside the object 'x' and then call them when needed. That way you can make 'x' an interface to ensure that all objects whether 'x' 'y' or 'z' all behave appropriately in the right situation with the required methods. Have I got that about right?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by porchrat View Post
    With this command patter thing do I have it right when I say this:

    It looks as though you hand an object 'x' as a parameter and store the key changes in program logic as a collection of methods inside the object 'x' and then call them when needed.
    As far as I know, a collection of methods is not a standard part of the command pattern. Perhaps your confusing command with observer (as I initially did), since with the latter, the observeable often holds a collection of observers that are notified if an event or change in state occurs.

  15. #15
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    As far as I know, a collection of methods is not a standard part of the command pattern. Perhaps your confusing command with observer (as I initially did), since with the latter, the observeable often holds a collection of observers that are notified if an event or change in state occurs.
    Is there anything wrong with what I've just described though? Architecturally I mean.

    I'm almost certain it would solve my problem. Maintenance would be easy. The code that never changes can just stay in that central class and that class just needs to take in an object of each of those 'x' 'y' and 'z' states each time it is called. It could totally work.

    By observer pattern you mean listeners and whatnot. I suppose in a way they would interact in a way similar to an observer pattern except the places at which the additional code is used is going to be far less dynamic. No button pushing or anything like that required just like a straight batch run. At certain key points at which code needs to be different you just call the appropriate method in the 'x' 'y' or 'z' object and let it sort out the differences.
    Last edited by porchrat; 09-26-2010 at 03:50 PM.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by porchrat View Post
    Is there anything wrong with what I've just described though? Architecturally I mean.
    I don't think that there's anything intrinsically wrong, but as they say, the devil is in the details.



    I'm almost certain it would solve my problem. Maintenance would be easy. The code that never changes can just stay in that central class and that class just needs to take in an object of each of those 'x' 'y' and 'z' states each time it is called. It could totally work.
    Then by all means, give it a go!



    By observer pattern you mean listeners and whatnot. I suppose in a way they would interact in a way similar to an observer pattern except the places at which the additional code is used is going to be far less dynamic. No button pushing or anything like that required just like a straight batch run. At certain key points at which code needs to be different you just call the appropriate method in the 'x' 'y' or 'z' object and let it sort out the differences.
    Yes, by observer I mean listeners and whatnot. It sounds like you've been struck by the muse, and when that happens you have no choice but to create. Happy coding!

  17. #17
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Awesomeness. Thanks guys (or gals if any of you are members of the fairer sex) for the contributions it has helped a lot. I will give it a go and update this thread when I've tried it out.

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by porchrat View Post
    Awesomeness. Thanks guys (or gals if any of you are members of the fairer sex) for the contributions it has helped a lot. I will give it a go and update this thread when I've tried it out.
    No, no members of the fairer sex here (my member of that sex is being a member of the lazy sex right now, taking a nap on my couch ;-) realize that your problem is well defined and needs probably the application of one single pattern; normally more patterns are applied and sometimes the choice is not so clear cut and a problem can be 'solved' by applying different patterns. Each with their own pros and cons ... have fun with them.

    kind regards,

    Jos

    ps. also pay attention to Martin Fowler's refactorings, they're astonishing now and then but the 'catalog' of them is boring ...

Similar Threads

  1. breaking out of while loop
    By mac in forum New To Java
    Replies: 5
    Last Post: 05-18-2010, 03:21 PM
  2. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 07:03 PM
  3. Breaking for-loops with listeners?
    By CBarry in forum New To Java
    Replies: 3
    Last Post: 04-22-2009, 03:38 AM
  4. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 06:39 PM
  5. Breaking from nested switch
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 02-02-2008, 08:28 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
  •