Simple query I have to use 2 different class with the same instance name. for example: DataServer d; Data d; i want use: if(local) { d=new Data("db.db"); } else { d=new dataServer(); } how do i do this could some one explain how do i do this: cheers
Narayan Veeramani
Greenhorn
Joined: Jun 06, 2001
Posts: 25
posted
0
You could have an interface implemented by both the classes DataServer and Data and declare variable d as a reference to that interface.
ram fbn
Greenhorn
Joined: Jun 15, 2001
Posts: 4
posted
0
Thanks but still its not clear ?
public class DataclientImpl implements DataServer { DataServer d; //Data d; i need to too..
connect(String mode) { if(local) { d=new Data(); }else { d = (DataServer)Naming.lookup(url); } } public lock(index) { d.lock(index); } } here i need both DataServer and data instance for Local And Remote mode. so how do u suggest to do cheers.
Matt Cannata
Greenhorn
Joined: Mar 23, 2001
Posts: 23
posted
0
Here is what I believe Narayan was implying. I used it in my project and it works. public interfact Database public class DataServer implements Database public class Data implements Database Now your code looks like: public class DataclientImpl implements DataServer { Database d; connect(String mode) { if(local) { d=new Data(); }else { d = (DataServer)Naming.lookup(url); } } public lock(index) { d.lock(index); } } Hope this helps, Matt
Rick Fortier
Ranch Hand
Joined: Jun 04, 2001
Posts: 147
posted
0
I know this is picky put if everyone used the CODE blocks around there code sections it would make it easier to read.
[This message has been edited by Rick Fortier (edited June 15, 2001).]
baiju
Greenhorn
Joined: Jun 13, 2001
Posts: 17
posted
0
yeah I do understand this but i just dont understand why u say data class extend database,i dont want to use it.In my case but Dont know what ram needs.. my data class why should ram implement the database in dataserver. i think he need to use one class to do the local and remote. but i too dont understand completely.. baiju
baiju
Greenhorn
Joined: Jun 13, 2001
Posts: 17
posted
0
still i did not understand.. public class Data implements Database why this... I think ram doesnt wants that
baiju
Matt Cannata
Greenhorn
Joined: Mar 23, 2001
Posts: 23
posted
0
The most common solution to the local/remote problem is to look up the DataServer in remote mode and just use an instance of the Data class in local mode. As part of this solution, the Database interface serves as an adapter, where the client can make calls against the interface, without worrying whether it is local or remote. Without it the code would something look like: (in the DataClient) private Data d1 localClient; private DataServer d2 remoteClient; public void lock(int recordNum) { if(localMode) { d1.lock(recordNum); } else { d2.lock(recordNum); } } I think you would agree that this is something you would want to avoid.
ram fbn
Greenhorn
Joined: Jun 15, 2001
Posts: 4
posted
0
yes i did understand and let me try this. thanks for ur suggestion. i think i could use factory. how could i call this as adapter.. ram
shailesh sonavadekar
Ranch Hand
Joined: Oct 12, 2000
Posts: 1874
posted
0
Ram fbn , Welcome to javaranch. PROPER NAMES ARE NOW REQUIRED Please look carefully at official naming policy at javaranch & reregister yourself with proper first & last name. Please adhere to official naming policy & help maintain the decorum of the forum. Waiting for your posts with proper first & last name. Once you have reregister , please let us know about that & then your previous account will be disabled. Regards.