Java Design Patterns

J

JDany

I am starting out with Java and I really like it so far.

One thing that I learned fairly quickly is that java separates the data
from the controls by using models. But what if I want to use a single
data object in multiple controls?

Here's the senario:

I have a vector of project information, each project has a name and a
vector of dates and times.

I want to display the project names in a list, and the dates and times
associated with the project in a table. so when the user clicks on
each one of the projects their times get automaticaly updated.

Now here's the first aproach I took.

I created a project manager class that encapsulated the project vector
and extended AbstractTableModel and implemented the ListModel interface.
Now this was a pain since I couldn't do muliple inheritance (to extend
from Table and List Abstract models) so ended up with a clean Table model
but some extra (required) list model methods that i had to cut and paste
from AbstracListModel source file.

Second approach i took was this:
I created my project manager class and encapsulated the project vector
like before but this time i created two inner classes a projecTableModel
class and projectListModel class, in the constructor of each one i pass
a reference to the actual project vector so they can get access to it.
I later found out that any inner class can access the enclosing
properties without having to pass them.

Now the second approach was much cleanner and made it easy to maitain but
i am wordering if this is the best way to do this.

Is there a website or good book that presents you with good java
desing patterns? or do you just try what ever look best?
 
R

Roedy Green

Is there a website or good book that presents you with good java
desing patterns


See http://mindprod.com/jgloss/designpatterns.html

I think the way you do this is try out alternate design and SEE which
works best. Often it is a tradeoff of some sort. Eventually you
collect a stereotypical way of coding.

If you actually try it both ways, it will sink it at a deeper level
WHY you should do it one way or another. This knowledge will stand you
in good stead when you come up against something for which you can't
find a recipe.

When I was going to university I hung out with a girl who was studying
home economics. She told me one day they had baked a large number of
cakes, with too few, too many eggs, with too little too much milk etc
etc. Now they were in a position to adjust any recipe that was not
working out right since they could recognise the symptoms.
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top