Book a Demo

Author Topic: Object modelling question  (Read 3401 times)

DaveWoodward

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Object modelling question
« on: December 19, 2002, 03:45:31 am »
Hi folks,

I am new the arena of object modelling, and am finding EA very useful is tracking what I am doing.

I am looking for some help with a modelling query I have. I am sure its a pretty simple question, but the answer will help in pointing me in the right direction at the start of my modelling task. I'd appreciate any pointers you can offer.

I want to display a window, differing in a number of respects depending on the authority of the user who has logged in. E.g. admin functions are only available to administrators etc.

I could do something like

newperson := user.create(userid);
If newperson.type = 'administrator' then
 begin
    DispWindow.button1.Visible := True;
    DispWindow.Button2.Visible := True;
    DispWindow.Caption := 'Admin logged in';
    etc
  end
else if newperson.type = 'normal' then
 begin  
    DispWindow.button1.Visible := False;
    DispWindow.Button2.Visible := False;
    DispWindow.Caption := 'User logged in';
    etc
 end;


but what I'd really like to do would be something like:

newperson := Administrator.Create;

newperson.DisplayWindow;

Where the DisplayWindow method of the Administrator class contained:

 begin
    DispWindow.button1.Visible := True;
    DispWindow.Button2.Visible := True;
    DispWindow.Caption := 'Admin logged in';
    etc
  end;

as before.

As I say I'm new to this, but the second approach looks more OO like to me, and would result in me having a person class with Administrator, NormalUser etc classes descending from it.

Does this make sense?   ???

Thanks
« Last Edit: December 19, 2002, 03:47:42 am by DaveWoodward »

Wai Chung

  • Guest
Re: Object modelling question
« Reply #1 on: December 24, 2002, 05:39:40 pm »
Hello Dave,

Your problem statement seems to be a classic situation where an interface will help bring out the power of polymorphism. You are on the right track with the second approach.

What I would do is to have an interface (say User) that specifies what behaviours a user can have, and then have two classes (Administrator and NormalUser) implement the interface with the desired behaviours.  

I would then declare a variable of type User to hold the reference to either the Administrator or NormalUser instance. The dynamic binding at run time should ensure that the appropriate object's operation is invoked.

The power of using interface is that it separates design from implementation. In future other classes can replace Administrator and NormalUser without changing exising code as long as the replacement classes implement the User interface, and these classes can implement the User interface in any way they like.

You might also want to consider using abstract class as the base class.

Hope this makes sense to you.

Wai