• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[URLyBIRD] guicontroller problem with interfaces

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
     
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ?
     
    Bartender
    Posts: 3648
    16
    Android Mac OS X Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Jerry Goldbaum
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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:
     
    Ranch Hand
    Posts: 169
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 169
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi;

    sure it will return cast problem.

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

    regards.
     
    Jerry Goldbaum
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 169
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 169
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, I need to reconsider my design - I will be back in a few days
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic