jeudi 19 juillet 2018

Restrictions in Mediator Pattern

There is a way to implement access control in 'Mediator' design pattern? [REF]

For example, I have 3 objects which communicate between them through a Mediator:

public interface Mediator {

    public void operationA();
    public void operationB();
    public void operationC();

}

public abstract class Colleague {

    protected Mediator mediator;

    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }

    public Mediator getMediator() {
        return mediator;
    }

}

public class ConcreteMediator implements Mediator() {

    private ObjectA objectA;
    private ObjectA objectB;
    private ObjectA objectC;

    public void operationA() {
        objectA.operationA();
    }

    public void operationB() {
        objectB.operationB();
    }

    public void operationC() {
        objectC.operationC();
    }
}

public class ObjectA extends Colleague {
    public operationA() {
        System.out.println("Operation A");
    }
}

public class ObjectB extends Colleague {
    public operationB() {
        System.out.println("Operation B");
    }
}

public class ObjectC extends Colleague {
    public operationC() {
        System.out.println("Operation C");
    }
}

And I want that operation A can only be executed by object C or object A. What is the best way to implement this restriction?

Aucun commentaire:

Enregistrer un commentaire