Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

same instance name of different class ?

 
ram fbn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still i did not understand..
public class Data implements Database
why this...
I think ram doesnt wants that

baiju
 
Matt Cannata
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Your Friendly Bartender
Shailesh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic