中介者
中介者模式:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。中介者模式又称为调停者模式,它是一种对象行为型模式。
//创建门派抽象类
package com.md.tertium;
//门派抽象类
public abstract class School {
protected IPatternAlliance patternAlliance;
public School(IPatternAlliance patternAlliance) {
super();
this.patternAlliance = patternAlliance;
}
//防御
public void defense() {
System.out.println( getName() + "防御" );
}
//攻击
public void attack(School school) {
System.out.println( getName() + "攻击" + school.getName() );
}
//有了中介者,将由中介者处理
public void attackByPatternAlliance(School school) {
patternAlliance.resolveAttack( this, school );
}
//门派攻击
public void defenseByPatternAlliance() {
patternAlliance.attack( this );
}
public abstract String getName();
}
package com.md.tertium;
//中介者
public class MediatorSchool extends School {
public MediatorSchool(IPatternAlliance patternAlliance) {
super( patternAlliance );
}
@Override
public String getName() {
return "中介者神阁";
}
}
package com.md.tertium;
public class CommandSchool extends School {
public CommandSchool(IPatternAlliance patternAlliance) {
super( patternAlliance );
}
@Override
public String getName() {
return "命令门";
}
}
package com.md.tertium;
public class SingletonSchool extends School {
public SingletonSchool(IPatternAlliance patternAlliance) {
super( patternAlliance );
}
@Override
public String getName() {
return "单例宗";
}
}
package com.md.tertium;
public class StrategySchool extends School {
public StrategySchool(IPatternAlliance patternAlliance) {
super( patternAlliance );
}
@Override
public String getName() {
return "策略";
}
}
package com.md.tertium;
import java.util.List;
//模式联盟 中介者接口
public interface IPatternAlliance {
//提供一个加入联盟的接口
public abstract void add(School school);
//防御联盟
public abstract void resolveAttack(School activeSide, School passiveSide);
//攻击联盟
public abstract void attack(School passiveSide);
}
package com.md.tertium;
import java.util.LinkedList;
import java.util.List;
//联盟模式的具体实现
public class PatternAlliance implements IPatternAlliance {
private List <School> schoolList = new LinkedList <School>();
@Override
public void add(School school) {
//加入联盟
schoolList.add( school );
}
@Override
public void resolveAttack(School activeSide, School passiveSide) {
if (schoolList.contains( passiveSide )) {
System.out.println( "被攻方" + passiveSide.getName() + "已加入联盟,联盟将齐力防御!" );
for (School school : schoolList) {
school.defense();
}
} else {
System.out.println( "被攻方" + passiveSide.getName() + "未加入联盟,联盟不给予防御帮助!" );
passiveSide.defense();
}
}
@Override
public void attack(School passiveSide) {
if (schoolList.contains( passiveSide ) && schoolList.contains( passiveSide )) {
System.out.println( "主攻方" + passiveSide.getName() + "以及被攻方" + passiveSide.getName() + "都已加入联盟,不允许内讧!" );
} else if (schoolList.contains( passiveSide ) && !schoolList.contains( passiveSide )) {
System.out.println( "主攻方" + passiveSide.getName() + "已加入联盟,被攻方" + passiveSide.getName() + "不在联盟之中,将集体攻打该门派!" );
for (School school : schoolList) {
school.attack( passiveSide );
}
} else {
System.out.println( "主攻方" + passiveSide.getName() + "未加入联盟,联盟无权干预此事!" );
passiveSide.attack( passiveSide );
}
}
}
package com.md.tertium;
public class Main {
public static void main(String[] args) {
// School singletonSchool = new SingletonSchool();
// School commandSchool = new CommandSchool();
// School mediatorSchool = new MediatorSchool();
//
// singletonSchool.attack( mediatorSchool );
// commandSchool.attack( mediatorSchool );
//
// mediatorSchool.defense();
IPatternAlliance patternAlliance = new PatternAlliance();
School singletonSchool = new SingletonSchool( patternAlliance );
School commandSchool = new CommandSchool( patternAlliance );
School mediatorSchool = new MediatorSchool( patternAlliance );
School strategySchool = new StrategySchool( patternAlliance );
//策略宫没有被联盟收留
patternAlliance.add( mediatorSchool );
patternAlliance.add( commandSchool );
patternAlliance.add( singletonSchool );
singletonSchool.attackByPatternAlliance( mediatorSchool );
commandSchool.attackByPatternAlliance( mediatorSchool );
System.out.println( "------------------------------------------------------" );
mediatorSchool.attackByPatternAlliance( strategySchool );
System.out.println( "------------------------------------------------------" );
strategySchool.attackByPatternAlliance( mediatorSchool );
mediatorSchool.defenseByPatternAlliance();
System.out.println( "------------------------------------------------------" );
}
}
《中介者模式》针对的问题是,解决一系列对象之间复杂的耦合关系,这一系列对象往往是“多对多”的耦合关系,《中介者模式》采用一个中介者对象将这一系列对象集中管理,而各个对象也将自己与其它对象的交互行为委托给中介者处理,从而减少这一系列对象之间的耦合。
它的优点倒是非常明显,清除了一系列对象之间复杂的耦合关系,并且中介者可以控制这一系列对象的行为,统一管理。
《中介者模式》的缺点是,由于中介者负责着一系列对象的交互与控制,所以中介者的类会非常复杂,而且一旦中介者类无法正常工作,那么所有将行为委托给中介者的类都将会出现问题,所以在使用的时候还是要特别小心。