简体   繁体   中英

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?

Split the Mediator interface into separate interfaces, grouping operations.

If you need fine grained control of this, interface inheritance is ok.

ConcreteMediator can implement multiple interfaces. Don't use inheritance for Colleagues and pass the ConcreteMediator into their constructors as a reference type of one the interfaces you split the mediator interface into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM