美文网首页
中介(联合国)模式

中介(联合国)模式

作者: ttyttytty | 来源:发表于2022-02-21 19:30 被阅读0次

中介(联合国)模式

  • 有好多同级别的工作类,如何统筹他们的调用or通信,降低类之间的耦合。网状交互->星状交互

  • view的事件机制:计算机有多个按钮,会有一个统一管理的类,将控件的结果处理后展示在展示框上。

  • 与门面Facade模式的区别:中介与各工作类之间时双向通信的,Facade只是单向调用工作类整合后对外提供接口。

    public abstract class Handler {
        protected Mediator mediator;
    
        public Handler(Mediator mediator) {
            this.mediator = mediator;
        }
    
        public abstract void handle(String duty);
    }
    
    public abstract class Mediator {
        public abstract void handleDuty(String duty,Handler handler) ;
    }
    
    public class Mediator1 extends Mediator {
        private Handler1 handler1;
        private Handler2 handler2;
    
        public Mediator1() {
        }
    
        public Mediator1(Handler1 handler1, Handler2 handler2) {
            this.handler1 = handler1;
            this.handler2 = handler2;
        }
    
        public Handler1 getHandler1() {
            return handler1;
        }
    
        public void setHandler1(Handler1 handler1) {
            this.handler1 = handler1;
        }
    
        public Handler2 getHandler2() {
            return handler2;
        }
    
        public void setHandler2(Handler2 handler2) {
            this.handler2 = handler2;
        }
    
    public class Handler1 extends Handler {
        public Handler1(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void handle(String duty) {
            mediator.handleDuty(duty,this);
        }
    
        public void actuallyGet(String duty) {
            System.out.println("Handler1 actually get " + duty);
        }
    }
    public class Handler2 extends Handler {
        public Handler2(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void handle(String duty) {
            mediator.handleDuty(duty, this);
        }
    
        public void actuallyGet(String duty) {
            System.out.println("Handler2 handle get " + duty);
        }
    }
    
    
  • public abstract class Handler {
        protected Mediator mediator;
    
        public Handler(Mediator mediator) {
            this.mediator = mediator;
        }
    
        public abstract void handle(String duty);
    }
    
public abstract class Mediator {
    public abstract void handleDuty(String duty,Handler handler) ;
}
public class Mediator1 extends Mediator {
    private Handler1 handler1;
    private Handler2 handler2;

    public Mediator1() {
    }

    public Mediator1(Handler1 handler1, Handler2 handler2) {
        this.handler1 = handler1;
        this.handler2 = handler2;
    }

    public Handler1 getHandler1() {
        return handler1;
    }

    public void setHandler1(Handler1 handler1) {
        this.handler1 = handler1;
    }

    public Handler2 getHandler2() {
        return handler2;
    }

    public void setHandler2(Handler2 handler2) {
        this.handler2 = handler2;
    }
public class Handler1 extends Handler {
    public Handler1(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void handle(String duty) {
        mediator.handleDuty(duty,this);
    }

    public void actuallyGet(String duty) {
        System.out.println("Handler1 actually get " + duty);
    }
}
public class Handler2 extends Handler {
    public Handler2(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void handle(String duty) {
        mediator.handleDuty(duty, this);
    }

    public void actuallyGet(String duty) {
        System.out.println("Handler2 handle get " + duty);
    }
}

相关文章

网友评论

      本文标题:中介(联合国)模式

      本文链接:https://www.haomeiwen.com/subject/tsbilrtx.html