wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Refactoring/Design questions: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Refactoring/Design questions:" Watch "Refactoring/Design questions:" New topic
Author

Refactoring/Design questions:

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Any help I can get with program design and such would be great, you can learn code all day but until you use it it is hard to figure out how to structure a larger program.

I am writing a (kinda) simple program that does the following: Helps a user create items, add them to a meal, and then put meals and items on a grocery list that can help with shopping. Obviously that is way dumbed down. So the user won't have to retype in items every time the master list of items, meals, and grocerylists are all saved to the HD using serialization for the time being.

Here is my basic design and some reasons behind it: I am trying to seperate all the functionality out to individual parts and keep loose coupling (I emphasize the word "trying"). This is hard to put into text so bare with me.

- First I created an abstract class called Listable which is any object that is allowed to be added to a GroceryList.
- A GroceryList is basically a glorified HashMap which accepts a String as key and a Listable as the value.
- A GroceryList will hold Meal and Component objects.
- A Meal is basically a glorified Set of Items.
- An Item is just a Component + a quantity.
- And finally a Component is just a few fields like name, cost, etc that represents something you would buy at the store.

So basically:
- A GroceryListManager HAS-A GroceryList<Listable>, a Set<Meal>, and a Set<Component>.
- Meal extends Listable and HAS-A Set<Item>
- Item extends Component
- Component extends Listable
- and finally Listable implements Serializable

The reason I have a Component and an Item is that I want to save a Component which may have data like: name = "Sausage", section = "meat", cost = 2.50f, etc and I want to save that information without saving quantity = 3lb, which only makes since in a meal. Therefor I keep a master list of Components which are in a Set (unique by name) and only have Items data saved to the HD inside of a Meal (where a quantity makes since). I also keep GroceryList as a HashMap because since I am adding different types of Listables and they are not mutually comparable I can't use a Set and I want to add/remove items based on their name so I use that as the key values.

I think I did a pretty bad job describing my code, lol. I also left out as much as I could that I don't think makes as much difference. For example a bunch of enum classes with methods, a persistance layer controller object, the consol/user interface controller, etc. I am most concerned with the structure of my business logic and have reformed it 3-4 times wasting huge amounts of time and I still feel that it is wrong and certain parts are forced into working. I'm sure once I press post I will think of 10 things I forgot to mention, so please ask questions, lol.

Any and all help is appreciated. I think this will help me become a better coder more than anything else.


SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Not a single reply.... is my post to unclear? Is there a way to post an attachment so I can attach my class diagram?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Brian Legg:
Not a single reply.... is my post to unclear? Is there a way to post an attachment so I can attach my class diagram?


Hi Brian,

This isn't the kind of thing we normally talk about in the JiG(Beginner) forum -- it's way beyond beginner level, I'd say. We do have our OO, Patterns, UML and Refactoring forum, where I think this kind of design discussion fits better. I'll move this over there for you (follow the link above.)

As far as helping you out: you haven't really asked any questions, or explained what sorts of things you're having trouble with, so I don't really know where to start. There are both high-level (class diagram) and low-level (implementation) issues mentioned here, both of which we could offer some advice on; depends what you're looking for. There's also the question as to whether this is a learn-to-program kind of question, or a how-is-this-done-in-the-real-world? kind of question. The answers would also be different.

So let us know what kind of help you're most interested in, and we'll see if we can get a discussion going.


[Jess in Action][AskingGoodQuestions]
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Ha!! I was literally just posting on here to ask a moderator to move it to this forum! I had just gone back and re-read the different topics offered and saw this one.

Thanks Ernest!

As far as helping you out: you haven't really asked any questions, or explained what sorts of things you're having trouble with, so I don't really know where to start. There are both high-level (class diagram) and low-level (implementation) issues mentioned here, both of which we could offer some advice on; depends what you're looking for. There's also the question as to whether this is a learn-to-program kind of question, or a how-is-this-done-in-the-real-world? kind of question. The answers would also be different.

So let us know what kind of help you're most interested in, and we'll see if we can get a discussion going.


I am more interested in "fixing" my class on a high-level which involves the classes and their relationships. For instance, I want to store a master list of all items you can buy at a store but do not want to save their quantities for that information only makes since in a particular meal. The way I have addressed this issue is to create 2 seperate classes: Component (an item you can buy at a store), and an Item (a Component with a quantity like 2 pounds or 5 bags). Currently my Item class extends Component and adds a quantity field and getters/setters.... I could also have Item have an instance of a Component, or even combine the 2 classes somehow and just hide the quantity field. These are the types of ideas I can't seem to get an answer about on my own and am looking for help. To answer your final question I am more interested in the "learn-to-program" correctly idea more than a "how-is-this-done-in-the-real-world" idea.

I realize there is no "right answer" here so any ideas or discussion is very appreciated. Thanks!!
[ November 25, 2008: Message edited by: Brian Legg ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I would say the "Item has-a Component" design would be better than the "Item is-a Component" you're using now. You can explain this on both a conceptual level (Can you add 2 tablespoons of "2 cups of Flour" to a recipe? Not really. An Item and a Component are different concepts) and a practical level (if Item extends Component, then a List<Component> could contain an Item -- probably not a good idea.)
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Originally posted by Ernest Friedman-Hill:
(if Item extends Component, then a List<Component> could contain an Item -- probably not a good idea.)


Good point!!

Another problem I am having is this declaration:



A GroceryList is intended to be a Map of Listables but it also needs to wrap the name field so that a user can distiguish one GroceryList from the next. Well, the above definition gives me the following warning: "The type parameter Listable is hiding the type Listable". That's about as unhelpful a message as I've ever seen, lol. Anyways, I thought about using a generic, like GroceryList<T> but I ONLY want Listables. Also, it would be ideal for GroceryList to extend Listable so that a GroceryList could actually contain a smaller GroceryList (makes since in the real world) but if I try to do that I get a compiler error, "Cannot refer to the type parameter Listable as a supertype".

Again, any help is appreciated and I will change Item to HAVE-A Component.

[ November 25, 2008: Message edited by: Brian Legg ]

EDIT: For some reason it won't show that GroceryList has <Listable> next to it in the code block... wierd.

[ November 25, 2008: Message edited by: Brian Legg ]

EDIT: Ok, I don't know what is going on but it will not show any open or close side-ways V thigys anywhere in my post!!

[ November 25, 2008: Message edited by: Brian Legg ]

EDIT: Hope this fixes it...

[ November 25, 2008: Message edited by: Brian Legg ]

EDIT: Got it... copy/pasted some code to show em!
[ November 25, 2008: Message edited by: Brian Legg ]
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Shoot!

Have a problem with that design change Ernest!

Since GroceryList, Meal, and Item can all be added to a GroceryList they all 3 must extend Listable. I originally had Listable as an interface but since all 3 of the objects that would implement it all had a name field and getters/setters I figured I would extract that data out of them. You can't have an instance variable in an interface so that changed Listable to an abstract class which then made all my 3 classes that implemented it unable to extend any other classes. Caught in a snowball effect!!

EDIT: Forgot to post my actual problem with the design change.

Problem is that if an Item HAS-A Component AND an Item MUST extend Listable to be added to a GroceryList then an Item will have 2 names potentially. A Component MUST have a name to make any since at all AND an ITEM must have a name. The ITEM name is held in Listable at the moment but if Component no longer extends Listable then it will lose its name field. If I add the name field manually to Component then my Item class now has 2 names. Hope that made since.
[ November 25, 2008: Message edited by: Brian Legg ]
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Ok, last post on here for a bit I swear! Here is a pic of the class diagram (if it shows up):

Ok, it didn't work out right, so if you'd like to see it please go to this link: ClassDiagram
[ November 25, 2008: Message edited by: Brian Legg ]
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
WooT!! Ok, I think I've got all my issues taken care of.

Listable is back to being an interface which extends Serializable.
GroceryList, Meal, and Item all implement Listable.
Item has a Component which implements Serializable.
Each one has their own name except for an Item which can ONLY be created by passing it a Component which already has a name.
If you set/get Items name field it just accesses it's Component.name field.
Finally a User class contains a GroceryList, a Set<Meal>, and a Set<Component>.

No errors or warnings in any of the code and it all makes logical since now. Thanks a lot Ernest! If you have any other ideas/suggestions let me know.
[ November 25, 2008: Message edited by: Brian Legg ]
 
jQuery in Action, 2nd edition
 
subject: Refactoring/Design questions:
 
Similar Threads
getting null values, while fetching data
Bad practise? JavaBean where setX() actually should be addX()?
Programatically identifying iterating nodes...
dynamic JSF forms, ajax, validation, etc
Assignment Help