mediator - 对象行为型模式
1.意图
用一个中介对象封装一些列对象交互。中介使各对象不需要显示地相互引用,从而使
其耦合松散,而且可以独立的改变它们的交互。
2.参与者
Mediator - 中介者定义一个接口用于各同事对象通信
ConcreteMediator - 了解并维护它的各个同事
- 具体中介者通过协调各个同事对象实现协作行为
Colleague class - 每个同事类都知道它的中介者对象
- 每个同事对象在需要与其它同事通信的时候与它的中介者通信。
3.结构
4.代码
public interface Mediator {
void regiser(People p);
Message checkClothes();
Message buyClothes(Message message);
}
public class ClothesMediator implements Mediator {
Map<String, People> map = new HashMap<String, People>();
int idcard;
public void regiser(People p) {
String idcard = "Clothes" + (this.idcard++);
map.put(idcard, p);
}
@Override
public Message checkClothes() {
Map<String, Map<String, Integer>> messageMap = new HashMap<String, Map<String, Integer>>();
Iterator<String> it = this.map.keySet().iterator();
while (it.hasNext()) {
String idcard = it.next();
People p = map.get(idcard);
messageMap.put(idcard, p.getClothes());
}
Message m = new Message(messageMap);
return m;
}
@Override
public Message buyClothes(Message message) {
if (message == null) {
return null;
}
Map<String, Map<String, Integer>> ms = message.getMap();
Set<String> set = ms.keySet();
Map<String, Integer> getClothes = new HashMap<String, Integer>();
for(String idcard:set){
Map<String, Integer> mm = ms.get(idcard);
People p = this.map.get(idcard);
getClothes.putAll(p.sellClothes(mm));
}
Map<String, Map<String, Integer>> messageMap = new HashMap<String, Map<String, Integer>>();
messageMap.put("buy clothes success", getClothes);
return new Message(messageMap);
}
}
public class Message {
private Map<String, Map<String ,Integer>> map;
Message(Map<String, Map<String ,Integer>> map) {
this.map = map;
}
public Map<String, Map<String ,Integer>> getMap() {
return map;
}
public void setMap(Map<String, Map<String ,Integer>> map) {
this.map = map;
}
@Override
public String toString() {
return "Message [map=" + map + "]";
}
}
public class People {
private Map<String, Integer> clothes;
private Map<String, Integer> huse;
public Map<String, Integer> getClothes() {
return clothes;
}
public void buyClothes(Map<String, Integer> clothes) {
if (this.clothes == null) {
this.clothes = new HashMap<String, Integer>();
}
this.clothes.putAll(clothes);
}
public Map<String, Integer> sellClothes(Map<String, Integer> clothes) {
Iterator<String> it = clothes.keySet().iterator();
while (it.hasNext()) {
String clothesName = it.next();
Integer num = clothes.get(clothesName);
Integer pNum = this.clothes.get(clothesName);
if (num != null) {
if (pNum < num) {
throw new RuntimeException("该用户衣服数量不够");
}
this.clothes.put(clothesName, pNum - num);
}
}
return clothes;
}
}
public class Client {
public static void main(String[] args) {
Mediator clothesMediator = new ClothesMediator();
People sellClothesA = new People();
Map<String, Integer> clothes = new HashMap<String, Integer>();
clothes.put("红衣", 2);
clothes.put("蓝衣", 3);
sellClothesA.buyClothes(clothes);
People sellClothesB = new People();
Map<String, Integer> clothes2 = new HashMap<String, Integer>();
clothes2.put("红衣", 4);
clothes2.put("蓝衣", 2);
sellClothesB.buyClothes(clothes2);
People sellClothesC = new People();
Map<String, Integer> clothes3 = new HashMap<String, Integer>();
clothes.put("红衣", 1);
clothes.put("蓝衣", 3);
sellClothesC.buyClothes(clothes);
clothesMediator.regiser(sellClothesA);
clothesMediator.regiser(sellClothesB);
clothesMediator.regiser(sellClothesC);
People p1 = new People();
People p2 = new People();
People p3 = new People();
/**
* p1购买红衣服两件
*/
Message checkMessage = clothesMediator.checkClothes();
System.out.println(checkMessage);
Iterator<String> it = checkMessage.getMap().keySet().iterator();
String idcard = "";
int num = 2;
String name = "红衣";
Map buyColthes = new HashMap<String, Integer>();
buyColthes.put("红衣", 2);
while (it.hasNext()) {
String card = it.next();
Map<String, Integer> cardClothes = checkMessage.getMap().get(card);
Integer sl = cardClothes.get(name);
if (sl != null && sl.intValue() > num) {
idcard = card;
break;
}
}
Map<String, Map<String, Integer>> messageMap = new HashMap<String, Map<String, Integer>>();
if (!"".equals(idcard)) {
messageMap.put(idcard, buyColthes);
}
Message buymessage = new Message(messageMap);
System.out.println(buymessage);
Message sellMesage = clothesMediator.buyClothes(buymessage);
System.out.println(sellMesage);
p1.buyClothes(sellMesage.getMap().get("buy clothes success"));
System.out.println(p1.getClothes());
}
}