DaFou
asked on
How to code an Object model
Ola,
I am trying to create an object model for a chatserver. The chatserver is the root class. any class inside the root class should not be instantiable or accessible outside of a chatserver object ( an instance of the chatserver class ).
There are 2 collections ( objects that act like a collection ) inside the ChatServer root object, Channels ( ChannelsCollection class ) and Clients ( ClientsCollection class ).
The Channels collection contains Channel objects ( instances of the Channel class ).
The Channel class should only be accessible from within the Channels colletion.
The Clients collection contains Client objects ( instances of the Client class ).
The Client class should only be accessible from within the Clients colletion.
A Channel object has a collection Clients ( same ClientsCollection class as used in the ChatServer object )
Here is a diagram:
ChatServer
| |
| Channels
| |
| Channel
| |
Clients
|
Client
Can someone show me how thats done? ( Only Chatserver, Channels and Channel class is enough )
I am trying to create an object model for a chatserver. The chatserver is the root class. any class inside the root class should not be instantiable or accessible outside of a chatserver object ( an instance of the chatserver class ).
There are 2 collections ( objects that act like a collection ) inside the ChatServer root object, Channels ( ChannelsCollection class ) and Clients ( ClientsCollection class ).
The Channels collection contains Channel objects ( instances of the Channel class ).
The Channel class should only be accessible from within the Channels colletion.
The Clients collection contains Client objects ( instances of the Client class ).
The Client class should only be accessible from within the Clients colletion.
A Channel object has a collection Clients ( same ClientsCollection class as used in the ChatServer object )
Here is a diagram:
ChatServer
| |
| Channels
| |
| Channel
| |
Clients
|
Client
Can someone show me how thats done? ( Only Chatserver, Channels and Channel class is enough )
offcourse u will also add method in Channels to retrieve channels, u can also replace HashTable with something like LinkedHashMap
this is a sample for JTDS, u need nothing special in Eclipse, just put in ur project library path the jTDS jar:
http://www.onjava.com/pub/a/onjava/2004/06/16/dbcancel.html?page=last&x-order=date
http://www.onjava.com/pub/a/onjava/2004/06/16/dbcancel.html?page=last&x-order=date
sorry !! 2 times in the same day the last comment is not for this thread
ASKER
also i would like indexers to work so that this would work
ChatServer lChatServer = new ChatServer();
lChatServer.Channels[ 0 ] // returns a channel object
and
lChatServer.Channels.Add( "ChannelName" ) // adds a channel object
ChatServer lChatServer = new ChatServer();
lChatServer.Channels[ 0 ] // returns a channel object
and
lChatServer.Channels.Add( "ChannelName" ) // adds a channel object
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am missing though how the Channels class is not instantiable from outside the ChatServer coz Channel class is public.
ok, in class Channels the add method will look like this
public void addChannel (String channelName)
{
Channel channel = new Channel(channelName, ..., ...);
channels.put(channelName, channel);
// or u decide what will be the key in ur LinkedHashMap
}
or can be:
public void addChannel (Channel channel)
{
channels.put(channel.getNa me(), channel);
}
public void addChannel (String channelName)
{
Channel channel = new Channel(channelName, ..., ...);
channels.put(channelName, channel);
// or u decide what will be the key in ur LinkedHashMap
}
or can be:
public void addChannel (Channel channel)
{
channels.put(channel.getNa
}
{
private Channels channels = new Channels()
public void addChannel(Channel channel)
{
// will call Channels method addChannel()
}
public Vector getChannels()
{
}
}
class Channels
{
private Hashtable channels = new Hashtable();
public void addChannel (Channel channel)
{
}
}
do u need more clarification?