wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [URLyBIRD] guicontroller problem with interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[URLyBIRD] guicontroller problem with interfaces" Watch "[URLyBIRD] guicontroller problem with interfaces" New topic
Author

[URLyBIRD] guicontroller problem with interfaces

Jerry Goldbaum
Greenhorn

Joined: Oct 31, 2008
Posts: 11
Hi,

I am thinking about the solution for two days and I am desperate now Please HELP
In my solution, in some degree, I follow the book "SCJD Exam with J2SE 5".
I have a problem with proper Object Oriented design related to GuiController class.

My design as it is now:
LOCAL OBJECTS DESIGN:
  • Interface provided by SUN: DBClient
  • DBClient "must have according to instruction from SUN" implementation class: Data

  • RMI OBJECTS DESIGN
  • To support RMI I created new interface: DBR (contains all methods from DBClient with RemoteException declared)
  • Interface to be exported by RMI is: remoteDB (extends Remote, DBR)
  • Class implementing remoteDB: RemoteDBImpl (implements remoteDB) - this class nicely wraps all exceptions and rethrows RemoteException if neeeded.


  • The most simple solution for me would be to create separate controller for local mode and other for network mode, however it seems to be lame...
    How to overcome the issue with DBClient and remoteDB not being compatible....
    I tried to create next interface, extending both DBClient and remoteDB and then by some type casting obtain proper reference however it failed....

    My goal is to obtain a controller reference like in the example solution:


    best regards,
    christian combarel
    Ranch Hand

    Joined: Aug 04, 2007
    Posts: 47
    Hi,
    I'm not really sure to understand your problem : you say that DBClient and remoteDB are not compatible but remoteDB "IS A" DBR wich "IS A" DBClient ?
    Why do you have two interfaces : DBR and remoteDB, a unique interface like "remoteDB extends DBClient, Remote" shouldn't be sufficient ?


    -------------
    Chris
    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 1966
        
        7

    Hi Jerry is your problem relate to choosing the database? My approach is to use the factory method design pattern. Have an interface say DatabaseFactory then the localDB/remoteDB class implements the DatabaseFactory. Of course you need to think about what methods to put in eg connect().

    Therefore, it's impossible to have incompatibility issues. Depending on how your app is started, mine uses the mode flag. So in my case I would do something like:



    Also your Data class can be initialized/used inside the LocalDB/RemoteDB classes. You don't necessary need to implement the Sun's provided interfaces at the LocalDB/RemoteDB level, which is incorrect anyway.

    Hope this helps.


    K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Hi,

    Christian,
    My DBClient interface from SUN does not throw RemoteException needed for RMI implementation.
    That is why I had to create second interface, DBR, having the same list of methods with RemoteException declared.

    As you can see DBR does not extend DBClient.

    then I created new interface

    because I see it more transparent this way, so I can later implement one interface in implementation class for RMI:
    mohamed sulibi
    Ranch Hand

    Joined: Sep 04, 2005
    Posts: 169
    hi Jerry Goldbaum;

    i know that your problem is you must duplicate the gui controller one for local and one for remote because the two type of mode have different type of interface dealing with it, i propose two solution for your problem:
    1- two different controller one for local and one for remote and this controller will passed and used with the client.
    2- for local and remote you can create another interface that have the common methods and use this instance for one controller.

    best regards.
    Mohamed Sulibi
    scjp, scjd in progress .... ( upload date 1/4/2009 or 1/4/9002).
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Hi,

    I know about solution 1 but I do not like it.
    Solution 2 is what I was chasing for the last few days.
    However I do not understand how it should be done.
    If I create third interface, let it be called TheInterface, in my opinion it should extend both previous interfaces (DBClient and DBR)?

    But after I do something like this:

    it does not work, casting problems....



    I started to reread Interfaces and OOD chapters one more time.
    mohamed sulibi
    Ranch Hand

    Joined: Sep 04, 2005
    Posts: 169
    hi;

    sure it will return cast problem.

    make your class has a reference instead of is a reference .

    regards.
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Hi,

    I know the difference between "is" and "have" in OO principles however in this case i do not get it.
    Could you explain what exactly do you mean?
    mohamed sulibi
    Ranch Hand

    Joined: Sep 04, 2005
    Posts: 169
    hi;

    before you think for local or remote think in the clients of your interface from business wise, so you can crease a new interface contain needed methods after that you can create one class that implements this interface. as you discuss you can create TheInterface interface:



    then implemented class TheInterfaceImpl:



    there are a trick here in the signature of these methods if you make all the methods in this interface to throws IOException you can implements this interface to be used remotely.

    regards.
    Mohamed Sulibi
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Ok but that is exactly what I am doing already, or am I wrong somewhere?

    I have DBClient interface and it's Data class implementing this interface.
    I have DBR interface with RemoteException:


    and implementation going like this:

    But the issue is that remoteDB extends DBR and not DBClient so in my GuiController I am not able to obtain a proper reference based on run mode: network or local. Different interfaces are responsible for database access in different modes (because DBClient can not be exposed via RMI) ...
    christian combarel
    Ranch Hand

    Joined: Aug 04, 2007
    Posts: 47
    Jerry,
    Wouldn't it solve your problem if you write something like this :


    In the controller, you get a DBClient no matter it is a "remote" or a "local" one. Am I mistaken ?
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Hi Chris,



    But remoteDB is intended to be used by RMI so in this case it mean that every method should be declared in remoteDB with the same signature as in DBClient, I would only need to add RemoteException. As far as I know compiler does not allow to add Exceptions freely. You are not allowed to declare exceptions other than in DBClient or their subclasses. So in result I am not able to have RemoteException needed for RMI in this solution (?)
    mohamed sulibi
    Ranch Hand

    Joined: Sep 04, 2005
    Posts: 169
    hi;

    As far as I know compiler does not allow to add Exceptions freely. You are not allowed to declare exceptions other than in DBClient or their subclasses.


    So i think you need to adapt the adaptee DBClient interface in another interface but not to inherited it, so use the adapter design pattern .

    regards.
    Jerry Goldbaum
    Greenhorn

    Joined: Oct 31, 2008
    Posts: 11
    Ok, I need to reconsider my design - I will be back in a few days
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: [URLyBIRD] guicontroller problem with interfaces
     
    Similar Threads
    gui controller and remote interface
    B&S: How to add RemoteException capability to DB interface?
    Apress: RMI Factory Pattern and GuiController
    Remoting layer
    NX: URLy Bird 1.3.1 Connection Factory Design