• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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 )
0
DaFou
Asked:
DaFou
  • 6
  • 2
1 Solution
 
petmagdyCommented:
class ChatServer
{
  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?
0
 
petmagdyCommented:
offcourse u will also add method in Channels to retrieve channels, u can also replace HashTable with something like LinkedHashMap
0
 
petmagdyCommented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petmagdyCommented:
sorry !! 2 times in the same day the last comment is not for this thread
0
 
DaFouAuthor Commented:
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
0
 
petmagdyCommented:
in this case u must use LinkedHashMap instead of HashTable

the Channels class will look like :

import java.util.*;

class Channels
{
 private LinkedHashMap channels = new LinkedHashMap();
 
 public void addChannel (Channel channel)
 {

 }

 public Channel getChannelAt(int index)
 {
    Collection values = channels.values();
     Object[] channels = values.toArray();
    return (Channel) channels[index];
 }
}

add to ChatServer the method:

public Channel getChannelAt(int index)
{
 return channels.getChannelAt( index);

}


0
 
DaFouAuthor Commented:
I am missing though how the Channels class is not instantiable from outside the ChatServer coz Channel class is public.
0
 
petmagdyCommented:
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.getName(), channel);
}

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now