1、中介者模式
package com.yqj.pattern.mediator;
import java.util.HashMap;
import java.util.Map;
abstract class Mediator {
public abstract void register(String colleagueName, Colleague colleague);
public abstract void getMessage(int stateChange, String colleagueName);
}
class ConcreteMediator extends Mediator {
private Map<String, Colleague> colleagueMap;
private Map<String, String> internalMap;
public ConcreteMediator() {
colleagueMap = new HashMap<>();
internalMap = new HashMap<>();
}
@Override
public void register(String colleagueName, Colleague colleague) {
colleagueMap.put(colleagueName, colleague);
if (colleague instanceof Alarm) {
internalMap.put("alarm", colleagueName);
} else if (colleague instanceof TV) {
internalMap.put("TV", colleagueName);
} else if (colleague instanceof Curtains) {
internalMap.put("curtains", colleagueName);
} else if (colleague instanceof CoffeeMachine) {
internalMap.put("coffeeMachine", colleagueName);
}
}
@Override
public void getMessage(int stateChange, String colleagueName) {
if (colleagueMap.get(colleagueName) instanceof Alarm) {
if (stateChange == 0) {
((CoffeeMachine) (colleagueMap.get(internalMap.get("coffeeMachine")))).startCoffee();
((TV) (colleagueMap.get(internalMap.get("TV")))).startTv();
} else if (stateChange == 1) {
((TV) (colleagueMap.get(internalMap.get("TV")))).stopTv();
}
} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
if (stateChange == 0) {
((Curtains) (colleagueMap.get(internalMap.get("curtains")))).upCurtains();
}
} else if (colleagueMap.get(colleagueName) instanceof TV) {
} else if (colleagueMap.get(colleagueName) instanceof Curtains) {
}
}
}
abstract class Colleague {
private String name;
private Mediator mediator;
public Colleague(String name, Mediator mediator) {
this.name = name;
this.mediator = mediator;
}
public abstract void sendMessage(int stateChange);
public Mediator getMediator() {
return mediator;
}
public String getName() {
return name;
}
}
class Alarm extends Colleague {
public Alarm(String name, Mediator mediator) {
super(name, mediator);
mediator.register(name, this);
}
public void sendAlarm(int stateChange) {
sendMessage(stateChange);
}
@Override
public void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange, this.getName());
}
}
class TV extends Colleague {
public TV(String name, Mediator mediator) {
super(name, mediator);
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange, this.getName());
}
public void startTv() {
System.out.println("打开电视机");
}
public void stopTv() {
System.out.println("关闭电视机");
}
}
class Curtains extends Colleague {
public Curtains(String name, Mediator mediator) {
super(name, mediator);
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange, this.getName());
}
public void upCurtains() {
System.out.println("打开窗帘");
}
}
class CoffeeMachine extends Colleague {
public CoffeeMachine(String name, Mediator mediator) {
super(name, mediator);
mediator.register(name, this);
}
@Override
public void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange, this.getName());
}
public void startCoffee() {
System.out.println("制作咖啡");
}
public void finishCoffee() {
System.out.println("制作完成咖啡");
sendMessage(0);
}
}
public class Client {
public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
Alarm alarm = new Alarm("alarm", mediator);
CoffeeMachine coffeeMachine = new CoffeeMachine("coffeeMachine", mediator);
Curtains curtains = new Curtains("curtains", mediator);
TV tv = new TV("TV", mediator);
alarm.sendAlarm(0);
System.out.println("====");
alarm.sendAlarm(1);
System.out.println("====");
coffeeMachine.finishCoffee();
}
}
2、备忘录模式
package com.yqj.pattern.memento;
import java.util.ArrayList;
import java.util.List;
class GameRole{
private int vit;
private int def;
public GameRole(int vit, int def) {
this.vit = vit;
this.def = def;
}
public Memento createMemento(){
return new Memento(vit,def);
}
public void recoverMenmento(Memento memento){
vit = memento.getVit();
def = memento.getDef();
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
@Override
public String toString() {
return "GameRole{" +
"vit=" + vit +
", def=" + def +
'}';
}
}
class Memento{
private int vit;
private int def;
public Memento(int vit, int def) {
this.vit = vit;
this.def = def;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
class Caretaker{
private List<Memento> mementos = new ArrayList<>();
public void add(Memento memento){
mementos.add(memento);
}
public Memento get(int index){
return mementos.get(index);
}
}
public class Client {
public static void main(String[] args) {
GameRole gameRole = new GameRole(100,100);
Caretaker caretaker = new Caretaker();
caretaker.add(gameRole.createMemento());
gameRole.setVit(70);
gameRole.setDef(80);
caretaker.add(gameRole.createMemento());
gameRole.setVit(60);
gameRole.setDef(70);
caretaker.add(gameRole.createMemento());
System.out.println(gameRole);
gameRole.recoverMenmento(caretaker.get(1));
System.out.println(gameRole);
gameRole.recoverMenmento(caretaker.get(0));
System.out.println(gameRole);
}
}