本文共 4617 字,大约阅读时间需要 15 分钟。
《大话设计模式》阅读笔记和总结。原书是C#编写的,本人用Java实现了一遍,包括每种设计模式的UML图实现和示例代码实现。
目录: Github地址:
定义:中介者模式(Mediator):用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
UML图:
代码实现:
发起人
class Originator{ // 需要保存的属性,可能有多个 private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } // 创建备忘录,将当前需要保存到 信息导入并实例化一个Memento对象 public Memento CreateMemento(){ return new Memento(state); } // 恢复备忘录,将Memento导入并将相关数据恢复 public void setMemento(Memento memento){ state = memento.getState(); } // 显示数据 public void show(){ System.out.println("State="+state); }}
备忘录类
class Memento { private String state; public Memento(String state){ this.state = state; } public String getState() { return state; }}
管理者类
class Caretaker{ public Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }
客户端代码
public class MementoDesign { public static void main(String[] args) { Originator o = new Originator(); // Origiinator初始状态,为“ON” o.setState("ON"); o.show(); // 保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节 Caretaker c = new Caretaker(); c.setMemento(o.CreateMemento()); // Origiinator改变了状态属性为“OFF” o.setState("OFF"); o.show(); // 恢复原初始状态 o.setMemento(c.getMemento()); o.show(); }}
运行结果
State=ONState=OFFState=ON
例子:美国和伊拉克冲突不断,要想解决这个问题需要通过联合国中的联合国安理会处理国与国之间的事情。用程序模拟联合国安理会上美国和伊拉克辩论的情况。
UML图:
代码实现:
游戏角色类
public class GameRole { private int vitality;//生命力 private int attack;//攻击力 private int defense;// 防御力 public int getVitality() { return vitality; } public void setVitality(int vitality) { this.vitality = vitality; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getDefense() { return defense; } public void setDefense(int defense) { this.defense = defense; } /** * 状态显示 */ public void setDisplay(){ System.out.println("角色当前状态:"); System.out.println("体力:"+ vitality); System.out.println("攻击力:"+ attack); System.out.println("防御力:"+ defense); } /** * 获得初始状态 */ public void GetInitState(){ vitality = 100; attack = 100; defense = 100; } /** * 战斗,与大boss决战,损耗 */ public void fight(){ vitality = 0; attack = 0; defense = 0; } // 保存角色状态,将三个值通过实例化:角色状态存储箱返回 public RoleStateMemento saveState(){ return new RoleStateMemento(vitality, attack, defense); } public void recoveryState(RoleStateMemento memento){ vitality = memento.getVitality(); attack = memento.getAttack(); defense = memento.getDefense(); }}
角色状态存储箱类
public class RoleStateMemento { private int vitality;//生命力 private int attack;//攻击力 private int defense;// 防御力 // 将生命力、攻击力、防御力存入状态存储箱对象中 public RoleStateMemento(int vitality, int attack, int defense) { this.vitality = vitality; this.attack = attack; this.defense = defense; } public int getVitality() { return vitality; } public void setVitality(int vitality) { this.vitality = vitality; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getDefense() { return defense; } public void setDefense(int defense) { this.defense = defense; } }
角色状态管理者
public class RoleStateCaretaker { private RoleStateMemento memento; public RoleStateMemento getMemento() { return memento; } public void setMemento(RoleStateMemento memento) { this.memento = memento; }}
客户端代码
public class Main { public static void main(String[] args) { // 游戏角色初始状态,三项指标都是100 GameRole role = new GameRole(); role.GetInitState(); role.setDisplay(); // 保存进度 RoleStateCaretaker stateAdmin = new RoleStateCaretaker(); stateAdmin.setMemento(role.saveState()); // 大战boss的时候损失严重 role.fight(); role.setDisplay(); //恢复之前的状态 role.recoveryState(stateAdmin.getMemento()); role.setDisplay(); }}
运行效果
角色当前状态:体力:100攻击力:100防御力:100角色当前状态:体力:0攻击力:0防御力:0角色当前状态:体力:100攻击力:100防御力:100
转载地址:http://atkol.baihongyu.com/