スパイの観点から設計モデルの仲介者モデルを見る

8651 ワード

転載して声明を下さい:http://www.cnblogs.com/courtier/p/4292201.html
  • 仲介者パターン紹介:
  • 複数のクラス間の通信方式は1つのオブジェクトにカプセル化され,このオブジェクトから複数のクラスにのみ転送される.
           从特工的角度来看设计模式之中介者模式_第1张图片
  • コードの観点から理解(スパイとスパイの間では状況報知局(MEDIATOR)でしか情報交換できない)
  • package mediator;
    //   
    public abstract class ColleaugeAgent {
        //             ,     ,    ,  ,     ,  ,         
        private MediatorIn mediatorIn;
        
        public ColleaugeAgent(MediatorIn mediatorIn)
        {
            this.mediatorIn = mediatorIn;
        }
        
        protected MediatorIn getAgentOfLocation()
        {
            return mediatorIn;
        }
    }
  • スパイA:
  • package mediator;
    //  A
    public class ColleaugeAgentA extends ColleaugeAgent{
    
        public ColleaugeAgentA(MediatorIn mediatorIn) {
            super(mediatorIn);
            // TODO Auto-generated constructor stub
        }
        
        //       B
        public void notifyB(ColleaugeAgentB agentB)
        {
            //     ,  ,   
            getAgentOfLocation().notifyOtheragent(agentB);
        }
        
        public void getMsg()
        {
            System.out.println("A:        ,  ,          ");
        }
    
    }
  • スパイB:
  • package mediator;
    //  A
    public class ColleaugeAgentB extends ColleaugeAgent{
    
        public ColleaugeAgentB(MediatorIn mediatorIn) {
            super(mediatorIn);
            // TODO Auto-generated constructor stub
        }
        
        //       A
        public void notifyA(ColleaugeAgentA agentA)
        {
            //     ,  ,   
            getAgentOfLocation().notifyOtheragent(agentA);
        }
        
        public void getMsg()
        {
            System.out.println("B:        ,  ,          ");
        }
    }
  • 情報局:
  • package mediator;
    
    public interface MediatorIn {
        //      
        void notifyOtheragent(ColleaugeAgent colleaugeAgent);
    }
    package mediator;
    //           ,  ,     ?
    public class ConcreteMediator implements MediatorIn{
        private ColleaugeAgentA colleaugeAgentA;
        private ColleaugeAgentB colleaugeAgentB;
        
        
    
        public void setColleaugeAgentA(ColleaugeAgentA colleaugeAgentA) {
            this.colleaugeAgentA = colleaugeAgentA;
        }
    
    
    
        public void setColleaugeAgentB(ColleaugeAgentB colleaugeAgentB) {
            this.colleaugeAgentB = colleaugeAgentB;
        }
    
    
        //      
        @Override
        public void notifyOtheragent(ColleaugeAgent colleaugeAgent) {
            // TODO Auto-generated method stub
            if ( colleaugeAgent instanceof ColleaugeAgentA)
            {
                colleaugeAgentB.getMsg();
            }
            else
            {
                colleaugeAgentA.getMsg();
            }
        }
    
    }
  • 運転結果:
  • 从特工的角度来看设计模式之中介者模式_第2张图片
    (ソースコード:https://github.com/aliencool/Design-Pattrn/tree/master/mediator)

  • 終了:

  • 仲介者モードはGOFクラシックモードの一つであり、主に反射のINVOKEに用いられる.