Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would you design this?

Status
Not open for further replies.

theweirdo

Programmer
Aug 7, 2002
17
CA
Hello there. I'm trying to design a system in Java that recognizes users (more than one kind). I'm not sure how to approach this in an Object Oriented way.

There are two kinds of users: regular and admin. The regular ones don't do much except send messages to the system and sometimes receive files or messages from the system. The administrators do regular administrator tasks. Regular users' accounts expire after a certain time (for example, in 24 hours). Admins never expire.

So I'm wondering, should I create an Interface class and then have User and Admin classes implement the Interface class?

Is it even a good idea to have each user as a separate object? I was thinking of creating one object per user, and when the time expires, the object is destroyed. And if that IS a good idea, I'm not sure I know how to dynamically create objects like that. To clarify, let's say I have an event method onFoo() and within that method, I want to create a new regularUser object. I can't just write regularUser fooUser = new regularUser() can I? Since there will be more than one regular user object... I'm very confused now.

Thanks in advance for any advice.
 
Maybe you should think of the abstraction UserAccount too.
Can system users have multiple accounts on different systems?

So you have classes RegularUserAccount and AdminstratorUserAccount inheriting from UserAccount.
UserAccount class can define authentication features like login, password.

A user is logged in a system with one of the system's account. But can multiple users be logged in the same system? Maybe yes, for example like in Windows XP, but there is only one active user at a time.
So you could have classes System and SystemUser(don't need to make sub-classes, his rights are defined by his account) :
Code:
System
   SystemUser log_user (UserAccount account)
      require
          all_user_accounts.has (account)
      ensure
          Result != Void
          active_user () == Result

   SystemUser active_user ()

   List[RegularUserAccount] regular_user_accounts ()

   List[AdmistratorUserAccount] administrator_user_accounts ()

   List[UserAccount] all_user_accounts ()

end

You can also apply the Observer pattern :
- System can be a subject : it notifies the regular active user when its account expired (it can notify for operations such as: file transfer, messages to users, etc.)
- User can be a System observer : it is warned when its account has expired, or file sending notification, etc.





--
Globos
 
Thanks for the quick response, Globos.

Actually, the system can have more than one user logged in at the same time. But I don't know how many, I guess it could be unlimited. So should the user object be created for the duration of its existence? Or should it only be created when the user logs on? And since I have more than one active user at a time, should I make a Vector of objects??

Thanks.
 
At first glance, User objects don't need to be persistent(though it depends on your needs). But UserAccount objects are persistents. A user object is created when someone wants to log in the system, and may be deleted when he logs off. That sounds correct and logical. Again it depends on your need. By the way, what kind of system is it? Is it a chat system, or a micro operating system like? Or something else?
Concerning you last question, you can use a Vector object to hold all the active users. You should use the most general(abstract) collection class, to be not tightly coupled with a particular kind of collection. So for example for the interface of the System class use the Collection class. Internally, for implementation purpose, you can use any effective class you want(Vector, List, etc.), in pseudo-java code:
Code:
class System
   public void make () is -- the creator(constructor)
     do
        _active_users = new List ();
        -- ...
     end
 
   public Collection active_users () is
     do
        Result = _active_users;
     end
   
   private Collection _active_users;

   -- etc.

end  

--later, typical client code do :
System system = ...

Iterator iactive_user = system.active_users ().iterator ();
while (iactive_user.hasNext ())
{
  User user = (User) iactive_user.next ();
  -- ...
}

Again, btw, do active and logged in users are the same?

--
Globos
 
Hi Globos,

To answer your question, this system is for IRC. Basically the system will be seen as a user of IRC and will sit in a channel. When 'real' users join the channel, the system will look at the user's IP and determine what type of user he is, then the system will give the user the appropriate authentication. Administrators will have to log on, but regular users don't. So I guess the event of a user joining or leaving the channel will be when the system creates a user object. However, if that is the case, I'm not sure how to deal with the expiration (since the user object is created/destroyed one or more times before the expirey time). Maybe the Vector idea is better.

Now that I've thought more about it, I'm not sure how I should store the user's information (such as IP, password, expirey time, etc).

I'm not sure I understand your last question... but logged in users is a subset of the active users because an active user (who hasn't expired yet) may not be online at the moment.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top