观察者模式:牵一发而动全身。
文章目录
- 观察者模式:牵一发而动全身。
- 前言
- 一、观察者的作用
- 二、如何实现观察者
- 总结
前言
观察者模式一般使用场景是: 多个对象之间存在联系,当其中一个改变的时候,其他的所有关系对象都要做出相应改变;
一、观察者的作用
1 可以快速通知其他对象做出改变
2 减少了对象之间的耦合性,对象之间不直接通信,通过观察者来驱动相应改变
二、如何实现观察者
例如现在有三个人,买了若干东西,在超市中结账;已经结账完毕;此时通知某一个商品降价了,要求重新计算每个人购买的商品总价;
既: 某个商品价格的改动,涉及到了所有购买这个商品的人的总价计算;
人 买了若干个苹果,橘子
@Data
@AllArgsConstructor
@NoArgsConstructor
public class People{
private List<Apple> appleList;
private List<Orange> orangeList;
public Integer getMemony() {
return Optional.ofNullable(appleList).orElse(new ArrayList<>()).stream().mapToInt(Apple::getPrice).sum()
+ Optional.ofNullable(orangeList).orElse(new ArrayList<>()).stream().mapToInt(Orange::getPrice).sum();
}
public void setOrangePrice(Integer price) {
Optional.ofNullable(orangeList).orElse(new ArrayList<>())
.stream().forEach(orange -> orange.setPrice(orange.getPrice() + price));
}
}
苹果/ 橘子
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Orange {
private String name;
private Integer price;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Apple {
private String name;
private Integer price;
}
观察者
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Guanchazhe {
private List<People> peopleList;
public void setOrangePrice(Integer price) {
for (People people : peopleList) {
people.setOrangePrice(price);
}
}
}
开始测试
public class Test {
public static void main(String[] args) {
People people = getPeople();
People people1 = getPeople1();
People people2 = getPeople2();
System.out.println(people + "==========" + people.getMemony());
System.out.println(people1 + "===========" + people1.getMemony());
System.out.println(people2 + "===========" + people2.getMemony());
// 橘子打折,价格降低5元;
// 所有人应该付款的价格都需要重新计算
List<People> peopleList = CollectionUtil.newArrayList();
peopleList.add(people1);
peopleList.add(people2);
peopleList.add(people);
Guanchazhe guanchazhe = new Guanchazhe();
guanchazhe.setPeopleList(peopleList);
guanchazhe.setOrangePrice(-5);
System.out.println(people + "==========" + people.getMemony());
System.out.println(people1 + "===========" + people1.getMemony());
System.out.println(people2 + "===========" + people2.getMemony());
}
private static People getPeople() {
People people = new People();
List<Apple> appleList = CollectionUtil.newArrayList();
appleList.add(new Apple("大苹果", 12));
appleList.add(new Apple("小苹果", 11));
people.setAppleList(appleList);
List<Orange> orangeList = CollectionUtil.newArrayList();
orangeList.add(new Orange("大橘子", 15));
orangeList.add(new Orange("小橘子", 13));
people.setOrangeList(orangeList);
return people;
}
private static People getPeople2() {
People people = new People();
List<Orange> orangeList = CollectionUtil.newArrayList();
orangeList.add(new Orange("大橘子", 15));
people.setOrangeList(orangeList);
return people;
}
private static People getPeople1() {
People people = new People();
List<Apple> appleList = CollectionUtil.newArrayList();
appleList.add(new Apple("大苹果", 12));
appleList.add(new Apple("小苹果", 11));
people.setAppleList(appleList);
return people;
}
}
1 可以看到,降价之前每个人的总费用已经计算完毕
2 降价之后,通过观察者的 setOrangePrice 降价方法,通知了所有人,重新计算各自价格
3 最终每个人的总价重新计算,得到了降价之后的总价
总结
观察者,相当于简化了多个有关系的对象之间通信(一对多为主),其实写起来很简单,大家看起来也很简单,但这也称之为一种设计模式; 感觉就是: 可能我们平时稍微优化的代码,可能就是已经使用了一种设计模式,只是我们自己不知道而已…