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?
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?