Is there a design pattern that allows you to simulate different activities for different roles?

We have a requirement where depending on the role the user has, they get different options in the dropdown menu. (The selected option will control the actions in the recording)

This was not originally implemented by me, I hasten to add using if statements and store the allowed parameters in an array.

As predicted, the "rules" became more complex and new roles were added, so that it is now a wicked mess of spaghetti code that might require dummy variables to be added to keep track of what's going on.

My question is, is there a design pattern that can help solve this problem?

Example

The Approval role will only see Approve , Reject, and Submitted for Approval if you are reading the entry, 'City. If they own the entry, then they can only see Reject

The role of the CSA can see Review , Approve, or Correction Needed , if you read a post that was not rejected, if it was rejected then well, it gets tricky to explain succinctly.

We have 3 more roles ... :(

+3


source to share


1 answer


One possible solution could be the Decorator design pattern , hence it dynamically adds responsibilities to objects. Also provides a flexible alternative to subclassing for extending functionality. In your case, the implementation could be something like this:

//Component
public abstract class Option{   
    // defines the interface for objects that can have responsibilities added to them dynamically.
}

//ConcreteComponent 
public class ApproveOption extends Option{
    //defines an object to which additional responsibilities can be attached.
}

//ConcreteComponent 
public class RejectOption extends Option{
    //defines an object to which additional responsibilities can be attached.
}

public abstract class Decorator{   
    //maintains a reference to a Component object and defines an interface that conforms to 
    //Component interface.
}

public class OptionsDecorator extends Decorator{   
    //adds (Visible) responsibilities to the component depending from the Role.
}

      



Or through a state design pattern to allow an object to change its behavior when its internal state changes. It looks like the object will change its class. And become:

//Context
public class Role{  
    //defines the interface of interest to clients
    //maintains an instance of a ConcreteState subclass that defines the current state.
}

public abstract class State{
    //defines an interface for encapsulating the behavior associated with a particular state 
    //of the Context.
}

//Concrete State
public class ApproverState extends State{
    //each subclass implements a behavior associated with a state of Context
}

public class CSAState extends State{
    //each subclass implements a behavior associated with a state of Context
}

      

0


source







All Articles