简介:用一个中介对象封装一系列对象的交互,使各对象间不用显式引用
目的:
总结:统一管理服务之间的关系, 将服务之间的交互进行封装, 降低服务间的耦合
组成:中介者,服务
1,服务
package com.design.m.agency; /** * 会员抽象类 */ public abstract class Member { /** * 防御 */ public void defense(){ System.out.println(getMemberName() +" - 发起防御"); } /** * 攻击 */ public void attack(Member member){ System.out.println(getMemberName() +" - 攻击 - 用户:" + member.getMemberName()); } public abstract String getMemberName(); }
package com.design.m.agency; /** * 用户-A */ public class WorkerAMember extends Member { @Override public String getMemberName() { return "用户-A"; } }
package com.design.m.agency; /** * 用户-B */ public class WorkerBMember extends Member { @Override public String getMemberName() { return "用户-B"; } }
package com.design.m.agency; /** * 用户-C */ public class WorkerCMember extends Member { @Override public String getMemberName() { return "用户-C"; } }
package com.design.m.agency; /** * 用户-D */ public class WorkerDMember extends Member { @Override public String getMemberName() { return "用户-D"; } }
package com.design.m.agency; /** * 用户-E */ public class WorkerEMember extends Member { @Override public String getMemberName() { return "用户-E"; } }
2,中介者接口
package com.design.m.agency; /** * 中介者接口 */ public interface AgencyService { /** * 添加会员 * @param member */ void addMember(Member member); /** * 删除会员 * @param member */ void removeMember(Member member); /** * a进行防御 * @param a * @param b */ void agencyDefense(Member a); /** * A攻击B * @param a * @param b */ void agencyAttack(Member a, Member b); }
3,中介者实现
package com.design.m.agency; import java.util.ArrayList; import java.util.List; /** * 中介模式的实现 */ public class AgencyServiceImpl implements AgencyService { /** * 会员列表 */ private List<Member> memberList; public AgencyServiceImpl() { super(); memberList = new ArrayList<>(); } @Override public void addMember(Member member) { memberList.add(member); } @Override public void agencyDefense(Member a) { //如果是会员,则其他会员会一起帮助其防御 if(memberList.contains(a)){ for(Member member : memberList){ member.defense(); } }else{ a.defense(); } } @Override public void agencyAttack(Member a, Member b) { if(memberList.contains(a) && memberList.contains(b)){ //A和B都是会员则取消攻击 System.out.println(a.getMemberName() +" 和 "+ b.getMemberName() +" 都是会员, 不允许互相攻击 ! "); }else if(memberList.contains(a) && !memberList.contains(b)){ //A是会员B不是会员则其他会员帮助攻击 System.out.println(a.getMemberName() +"是会员, "+ b.getMemberName() +"不是会员, 其他会员将帮助一起攻击 !"); for(Member member : memberList){ member.attack(b); } }else if(!memberList.contains(a) && memberList.contains(b)){ //A不是会员B是会员则其他会员帮助防御 System.out.println(a.getMemberName() +" 不是会员, "+ b.getMemberName() +" 是会员, 其他会员帮助其一起防御 !"); a.attack(b); for(Member member : memberList){ member.defense(); } }else{ //A和B都不是会员,不参与 System.out.println(a.getMemberName() +" 不是会员, " + b.getMemberName() +" 也不是会员, 其他会员不参与 !"); a.attack(b); } } @Override public void removeMember(Member member) { memberList.remove(member); } }
4,Main
package com.design.m.agency; /** * 13-中介者模式 * * 概括:统一管理服务之间的关系, 将服务之间的交互进行封装, 降低服务间的耦合 * */ public class MainTest { public static void main(String[] args) { /** * 中介者模式 */ Member workA = new WorkerAMember(); Member workB = new WorkerBMember(); Member workC = new WorkerCMember(); Member workD = new WorkerDMember(); Member workE = new WorkerEMember(); AgencyService agency = new AgencyServiceImpl(); agency.addMember(workA); agency.addMember(workB); agency.addMember(workC); agency.addMember(workD); //A和B都是会员 agency.agencyAttack(workA, workB); //A是会员B不是会员 agency.agencyAttack(workA, workE); //A不是会员B是会员 agency.agencyAttack(workE, workA); //A和B都不是会员 agency.removeMember(workD); agency.agencyAttack(workE, workD); } }
5,Result
用户-A 和 用户-B 都是会员, 不允许互相攻击 !
用户-A是会员, 用户-E不是会员, 其他会员将帮助一起攻击 ! 用户-A - 攻击 - 用户:用户-E 用户-B - 攻击 - 用户:用户-E 用户-C - 攻击 - 用户:用户-E 用户-D - 攻击 - 用户:用户-E
用户-E 不是会员, 用户-A 是会员, 其他会员帮助其一起防御 ! 用户-E - 攻击 - 用户:用户-A 用户-A - 发起防御 用户-B - 发起防御 用户-C - 发起防御 用户-D - 发起防御
用户-E 不是会员, 用户-D 也不是会员, 其他会员不参与 ! 用户-E - 攻击 - 用户:用户-D