简体   繁体   English

调解员模式的限制

[英]Restrictions in Mediator Pattern

There is a way to implement access control in 'Mediator' design pattern? 有没有一种方法可以在“ Mediator”设计模式中实现访问控制? [REF] [REF]

For example, I have 3 objects which communicate between them through a Mediator: 例如,我有3个通过中介器在它们之间进行通信的对象:

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? 我希望操作A只能由对象C或对象A执行。实现此限制的最佳方法是什么?

Split the Mediator interface into separate interfaces, grouping operations. 将Mediator接口拆分为单独的接口,进行分组操作。

If you need fine grained control of this, interface inheritance is ok. 如果您需要对此进行细粒度的控制,则可以继承接口。

ConcreteMediator can implement multiple interfaces. ConcreteMediator可以实现多个接口。 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. 不要对同事使用继承,也不要将ConcreteMediator传递给其构造函数作为将中介程序接口拆分为接口的一种引用类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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