Nilesh
Nilesh

Reputation: 1222

Implementing different user types through classes and interfaces in Java

Problem
I'm fairly new to design patterns, and have been studying the book, Head First Design Patterns. I need to implement a system with 3 kinds of users: Member, Moderator, Admin. A Moderator can perform everything that a Member can, plus added stuff, an Admin can perform everything that a Moderator can, plus added stuff. I have made a basic sketch of how this can be implemented with interfaces and classes; being inexperienced, I need advice from the SO community about this design - whether it's too bloated or silly, or if it needs corrections. So please bear with me.

Possible solution
Here are the interfaces:

public interface AdminBehavior
{
    public addUser();
    public deleteUser();
}

public interface ModeratorBehavior
{
    public blockUser();
    public deletePost();
}

Behavior classes:

public class AdminBehaviors implements AdminBehavior
{
    public addUser()        {
        ...
    }
    public deleteUser()     {
        ...
    }
}

public class NoAdminBehaviors implements AdminBehavior
{
    public addUser()        {
        ...//cannot do
    }
    public deleteUser()     {
        ...//cannot do
    }
}

+ Same as above done for Moderators...classes ModeratorBehaviors and NoModeratorBehaviors

The actual User classes:

public class Member
{
    protected ModeratorBehavior moderatorBehavior;
    protected AdminBehavior adminBehavior;

    public Member()     {
        moderatorBehavior = new NoModeratorBehavior();
        adminBehavior = new NoAdminBehavior();
    }

    public login()      {
        ...
    }
    public logout()     {
        ...
    }
    public post()       {
        ...
    }
    public comment()        {
        ...
    }

    //Moderator priv'ed actions
    public blockUser()      {
        moderatorBehavior.blockUser();
    }
    public deletePost()     {
        moderatorBehavior.deletePost();
    }

    //Admin priv'ed actions
    public addUser()        {
        adminBehavior.addUser();
    }
    public deleteUser()     {
        adminBehavior.deleteUser();
    }
}

public class Moderator extends Member
{
    public Moderator()  {
        moderatorBehavior = new ModeratorBehavior();
        adminBehavior = new NoAdminBehavior();
    }
}

public class Admin extends Moderator ((or Member?))
{
    public Admin()  {
        moderatorBehavior = new ModeratorBehavior();
        adminBehavior = new AdminBehavior();
    }
}

Personally, I feel that this seems a bit over-done or confusing...better ways to do it?

Upvotes: 2

Views: 3358

Answers (4)

Eyal Golan
Eyal Golan

Reputation: 804

If a Moderator can do whatever a Member does + more, and Admin can do whatever Moderator does + more.

why not have an interface Member, Moderator extends it and Admin extends Moderator?

public interface Member {
  void foo();
}

public interface Moderator extends Member {
  void bar();
}

public interface Admin extends Moderator {
  void boo();
}

I am not sure behavior is the best approach to what you have described.

Upvotes: 1

Edd
Edd

Reputation: 8580

I quite like it... does seem bloated though.

I'd probably just use inheritance. Admin extends Moderator extends Member implements UserType.

  • UserType interface could define all your methods
  • Member could implement all methods but with the non-behaviour
  • Moderator could inherit from member and override the methods it requires behaviour for
  • Admin could inherit from Moderator and override the additional methods it requires behaviour for

    That would be simpler I think, but less clever

    Upvotes: 2

  • kosa
    kosa

    Reputation: 66657

    I think you can simplify this by having Member interface as super interface. Moderator interface extends Member interface and Admin extends Moderator interface. This way Admin get all privileges as Member and Moderator. Admin member implementation class will extend Admin interface which by default gets all operations from super interface. Same like, other implementation classes implementation corresponding interface. I don't know which pattern it is, but I feel looks clean.

    Upvotes: 0

    awiebe
    awiebe

    Reputation: 3846

    Well for starters an admin should definitely extend Moderator as an admin has all the moderators capabilities and then some. Unless the interface for moderators and admins is different in which case they should both extend or at least implement behaviours in a new item (Privileged User). Member should not contain prived methods, the should be moved to Priviledged user. In the view the normal users should not be capable of invoking any of the prived users commands, doing so would cause an error, because "Members" do not have those methods.

    Upvotes: 0

    Related Questions