aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Design Question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Design Question..." Watch "Design Question..." New topic
Author

Design Question...

Alex Silva
Greenhorn

Joined: Feb 11, 2002
Posts: 7
Hey guys,
Can you please tell me what you think about this design? I spent most of my day yesterday coming up with a design that would make me happy and I think this is the one I think is closest to the specifications... However, I am still not sure if it is really what Sun is looking for.
Ok. Here it is.
1- I have a interface called DataStore. This interface defines all the public methods in Data, and all the methods throw RemoteException. The Data class implements this interface.
2- On the other side, I have a RemoteDataStore interface that extends both DataStore and Remote.
3- I have an object called DataServer that extends UnicastRemoteObject and implements the RemoteDataStore interface. All this class does is adapt the Data object on the server, so when you call the methods on this class, you are calling the methods in Data.
4- Now, as the base of this "architecture", there is a DataClient, that will manage the calls to Data (client mode) or DataServer (remote mode). So, the GUI calls the methods in DataClient, which also implements DataStore, to ensure that the client can call the same public methods from Data.
How does this look? Am I missing something? Am I taking an extra step somewhere here? Or is this good?
Any help would be greatly appreciated!!
Thanks!!!
Alex
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
Hi Alex,
I would let your DataStore interface throw Exception instead of RemoteException
/Enrico
Alex Silva
Greenhorn

Joined: Feb 11, 2002
Posts: 7
Hi Enrico,
Thanks for the reply. So instead of throwing DatabaseException (some Data methods throw that exception) and RemoteException, I should throw only Exception, right? I don't think I completely understand the benefits of using that approach. Could you please clarify that, Enrico?
Thanks!
Alex
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
Hi Alex,
What I did was letting the methods in the interface throw Exception. The Data class itself you don’t need to change, because a class that implements an interface can throw an exception more specific than in the interface but not wider.
In network mode the calls to the database, passed throw remote class which throws Data class exception + RemoteException. In local mode it only throws Data exceptions.
When you decide in which mode to run in, you want to use the interface as reference, something like this:
DataInterface data;
if (local)
Data = new LocalClass();
else
Data = (RemoteClass) Name.lookup(bla bla)

Hope it helps and that it is understandable.
Otherwise, search this forum. It’s a lot of discussions about this here.
It’s different way of doing this. But I think I should say that it’s not a good idea to this:
“1- I have a interface called DataStore. This interface defines all the public methods in Data, and all the methods throw RemoteException. The Data class implements this interface.”
Because this means that the Data class, which doesn’t know anything about RMI will throw java.rmi.RemoteExceptions. Avoid that!
/Enrico
Alex Silva
Greenhorn

Joined: Feb 11, 2002
Posts: 7
Hi Enrico,
Thanks again for your reply. Yes, that sure helps. See, that was one of the main points that made me question the design: why would the Data class be aware of RemoteExceptions when it really shouldn't. And if the DataStore interface throws Exception, I can "customize" the implementation, depending on the database mode.
Thanks a lot!
Alex Silva
Greenhorn

Joined: Feb 11, 2002
Posts: 7
Anybody else has any ideas to share on this?
Thanks,
Alex
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
Hi,
I agree that only the RMI interface/classes should be concerned with RemoteExceptions.
However, I think that declaring a method as throwing Exception is inadvisable. I was thinking of re-throwing the RemoteException as a DataException so that it won't get confused with standard Java exceptions.

What do people think of this? One problem is that the stack trace is replaced, but the RMI exception could be chained if necessary
[ February 11, 2002: Message edited by: Steven Sloggett ]

Steven
Murali Nanchala
Ranch Hand

Joined: Mar 14, 2001
Posts: 74
Originally posted by Steven Sloggett:
Hi,
I agree that only the RMI interface/classes should be concerned with RemoteExceptions.
However, I think that declaring a method as throwing Exception is inadvisable. I was thinking of re-throwing the RemoteException as a DataException so that it won't get confused with standard Java exceptions.

What do people think of this? One problem is that the stack trace is replaced, but the RMI exception could be chained if necessary
[ February 11, 2002: Message edited by: Steven Sloggett ]

You are absolutely right Steven. Re-throwing (as you call) of an Exception is the best way in such an event. But before you 'replace' the stack trace, use it!!


"It is almost always answered in the API" - Murali Nanchala
.sanjeev.
Greenhorn

Joined: Jan 30, 2002
Posts: 6
hi!
i didn't get the need of DataClient to extends DataStore.
will it work as factory?
regards,
sanjeev.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Question...