スパイの観点から設計モデルの仲介者モデルを見る
8651 ワード
転載して声明を下さい:http://www.cnblogs.com/courtier/p/4292201.html仲介者パターン紹介: 複数のクラス間の通信方式は1つのオブジェクトにカプセル化され,このオブジェクトから複数のクラスにのみ転送される.
コードの観点から理解(スパイとスパイの間では状況報知局(MEDIATOR)でしか情報交換できない) スパイA: スパイB: 情報局: 運転結果:
(ソースコード:https://github.com/aliencool/Design-Pattrn/tree/master/mediator)
終了:
仲介者モードはGOFクラシックモードの一つであり、主に反射のINVOKEに用いられる.
package mediator;
//
public abstract class ColleaugeAgent {
// , , , , , ,
private MediatorIn mediatorIn;
public ColleaugeAgent(MediatorIn mediatorIn)
{
this.mediatorIn = mediatorIn;
}
protected MediatorIn getAgentOfLocation()
{
return mediatorIn;
}
}
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: , , ");
}
}
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();
}
}
}
(ソースコード:https://github.com/aliencool/Design-Pattrn/tree/master/mediator)
終了:
仲介者モードはGOFクラシックモードの一つであり、主に反射のINVOKEに用いられる.