This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes All of URLy Bird 1.1.3  how to use the same GUI and Data in alone or netword client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "All of URLy Bird 1.1.3  how to use the same GUI and Data in alone or netword client" Watch "All of URLy Bird 1.1.3  how to use the same GUI and Data in alone or netword client" New topic
Author

All of URLy Bird 1.1.3 how to use the same GUI and Data in alone or netword client

liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi:George,Andrew and Philippe:
1.For network client:I create Factory and FactoryImpl to create remote objects.RemoteServices and its implementation class RemoteServicesImpl contains book and search method.The RemoteServicesImpl contains Data instance.FactoryImpl contains getConnection() to return the RemoteServicesImpl to network client.each network client contains a new RemoteServicesImpl instance,each RemoteServicesImpl contains a new Data instance.
2.For alone client:I create Services interface and its implementation class
ServicesImpl that contains book and search.The ServicesImpl contains Data instance.the alone client invokes directly the ServicesImpl's method.
3.I already create the class correctly and test work well.For testing,i create aloneClient and networkClient for local and remote test.They work correctly.
My question is:Whether i can create Clienta and Clientn class for accessing database?
Whether i must be create only one client for alone or network depending the
command line parameter ?if yes,how can i implements it?
thanks.
[ May 13, 2004: Message edited by: liqun chang ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Liqun,

Sorry, I missed this earlier.

I would recommend having only one client for both stand alone and networked mode.

To do this, you just need an interface on the client side which defines the methods the client may call on the database whether it is local or remote. You could consider having a factory to create either local or remote connections.

Then the main body of your client gui is just calling methods on the interface - it is unaware of whether the client is directly connected or connected via a network.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Hi,


Then the main body of your client gui is just calling methods on the interface - it is unaware
of whether the client is directly connected or connected via a network.


An (interesting?) point is whether or not the client should be able to write code which invokes
the DBMain methods (read, update, delete, find, create) without having to catch a
RemoteException.

So, I have three different connection modes.

1. Exclusive local mode, wherein the client does not need to catch a RemoteException.

DBMainAdditions dbMainAdditions = (DBMainAdditions) StaticClass.getLocalConnection();
[where DBMainAdditions extends Sun's supplied DBMain interface]

And, once the connection is obtained, the client could, for theoretical purposes, and
assuming that only Sun's required 5 methods are called, cast to DBMain:

DBMain dbMain = (DBMain) dbMainAdditions;
String[] fields = dbMain.read(10);

I don't consider this exclusive local mode to be the best coding choice for the client,
for if the client does not catch and handle RemoteException's, then in the future if
the client decides to write code to communicate to a remote database, the code
has to be changed to insert the catch clause for a RemoteException.

Nevertheless, it satisfies a requirement for my project that the connection to the
database be an instance of both Data and implement DBMain; so, I can insert
the following which are true:
JUnit assertTrue(dbMainAdditions instanceof Data);
Junit assertTrue(dbMainAdditions instanceof DBMain);

If I have a local connection as described in the next section, and this local connection
reference type is IndependentDataInterface, I can make the same assertions which
are true:
IndependentDataInterface data = StaticClass.getLocalConnection();
JUnit assertTrue(data instanceof Data);
Junit assertTrue(data instanceof DBMain);

2. And I have an IndependentDataInterface wherein the client always has to catch a
RemoteException even if the connection is local and the RemoteException can never
technically be thrown:

2a. Local mode.
IndependentDataInterface data = StaticClass.getLocalConnection();

2b. Remote mode.
IndependentDataInterface data = StaticClass.getRemoteConnection();

My actual client application which has the GUI (graphical user interface), uses the
IndependentDataInterface variable data. Once the GUI client application has data
defined, the code works identically whether data references a local or a remote
connection (and all the code, of course, catches and handles RemoteException,
even though this exception can't possibly be thrown if data happens to be a local
connection during one particular run of the GUI client).

Thanks,
Javini Javono
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Javini,

I prefer to have the lower level classes catch the RemoteException, so that the main client code remains oblivious to the connection method.

In my case, IndependentDataInterface declares that it throws exceptions I create (such as ConnectivityException) which wraps the RemoteException.

That way if the connectivity method is later changed (so we use Sockets or JMS or ...) we do not have to rewrite any of the client classes to catch the new methods.

Regards, Andrew
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi:Andrew and Javini.thanks for responding.
Andrew: according to your suggestion.I will do as below.

1.I will create Services interface(contains book and search)and throws IOException and RecordNotFoundException,its implementation class ServicesImpl has Data instance.These two classes(Services and ServicesImpl)
are used for alone client.

I will create RemoteServices interface implements Services and Remote,this is the good trick.RemoteServices has two methods in it(book and search).So do we can be simple,because the method that throws IOException can also thrwos RemoteExceptions.RemoteServices's implementation class is RemoteServicesImpl that do specified work.These two classes(RemoteServices and RemoteServicesImpl) are used for network client.

Because my design is:for network,each client has a remote object(RemoteServicesImpl instance) and each remote object has a Data instance.
so i require a Factory interface(has getConnection() that return RemoteServicesImpl object).the Factory interface implements Remote and
its implementation class is FactoryImpl.So do we have only one remote object
registry to rmiregistry.

2.From previous mention.I will create local(for client either alone client or network client) ServicesFactory and create method getServices(String location).but so do,it is a little not seem to the full factory pattern.




3.So my question is,whehter previous thinking is right? If have any questions please you tell me? thanks.
[ May 18, 2004: Message edited by: liqun chang ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Liqun,

It looks like you have the basic concept working in a similar way to how I would do it.

I would tend to have one Factory that creates all the connections:



For the main part this is just personal preferences though.

Do you understand why I used .equals() instead of ==?

Regards, Andrew
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi:Andrew let my try to answer your question above.
The equals() method is used to see whether value is equality.and the "=="is to see whether the reference point to same object.Am i right?

Andrew: the instruction say:


Non-Networked Mode
The program must be able to work in a non-networked mode. In this mode, the database and GUI must run in the same VM and must perform no networking, must not use loopback networking, and must not involve the serialization of any objects when communicating between the GUI and database elements.
The operating mode is selected using the single command line argument that is permitted. Architecturally, this mode must use the database and GUI from the networked form, but must not use the network server code at all.


as my mention previous,my Factory interface implements Remote interface,
and my FactoryImpl class implements Factory and extends UnicastRemoteObject
so do i can reduce the remote object that registrys rmiregistry.

1.My question is whehter i can use the FactoryImpl(remote object)for creating local business object(ServicesImpl) and remote business object (RemoteServicesImpl)and return them to client?I don't know whether you understand what i say?
Because Factory implements Remote and its implementation class FactoryImpl
is remote object,so i am a little confusion whether it can used for local client?
Whether Factory and FactoryImpl is network server code?

please you help me.
[ May 19, 2004: Message edited by: liqun chang ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Liqun,

You have given the definitions of what the equals() method and the == operator. But more to the point - do you know why I used equals() in the code snippet, rather than the == you used?

I am not completely sure what you are suggesting with your factory methods. You may be safe, but I am not 100% sure about it.

If the remainder of your client code does not have networked code in it, and you do not require the network to be running to get to the server, you may be OK.

Regards, Andrew
liqun chang
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi:Andrew

as you say:

I am not completely sure what you are suggesting with your factory methods. You may be safe, but I am not 100% sure about it.

If the remainder of your client code does not have networked code in it, and you do not require the network to be running to get to the server, you may be OK.



you do not answer me really.because you use two "may be".then i am more
wildered.

I design the Factory and its implementation class FactoryImpl either for network or for local.
When i start the server,the FactoryImpl is the remote object that registry to rmiregistry.i can return Services either for remote or for local.

When i do not start the server,FactoryImpl is the local object that can return local Services.

Whether you suggest me to create Factory only in client side that return either remote Services or local Services?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: All of URLy Bird 1.1.3 how to use the same GUI and Data in alone or netword client
 
Similar Threads
Design questions
nx:All of URLy Bird1.1.3 about record lock
NX: stand-alone mode using RMI
Providing multiple Data instances on RMI
B&S Stub errors running in network client mode