门面(Facde)、调停器(Mediator)、装饰器(Decorator)
门面模式(Facde)
示意图
用户调用的模块之间的关系错综复杂,使用门面模式来使得调用者调用更加方便直接。
案例
-
SpringMVC
中的DispatcherServlet
-
把本来需要请求
多
个接口的业务改成只请求一
个接口的业务
调停器(Mediator)
示意图
模块之间错综复杂的调用关系,通过引入一个中间协调者来解耦模式之间的复杂关系。
案例
- 消息队列
装饰器模式(Decorator)
顾名思义就是给类加一些功能(装饰)
实例代码
package pers.eddievim.dp.mediator;
/**
* @author eddieVim
* @微信公众号 埃迪的Code日记 / PositiveEddie
* @blog https://blog.csdn.net/weixin_44129784
* @create 2020/8/26 3:37 下午
*/
public interface Girl {
public void sayHi();
}
class CommonGirl implements Girl {
String name;
double height;
double weight;
@Override
public void sayHi() {
System.out.println("hi");
}
@Override
public String toString() {
return "CommonGirl{" +
"name='" + name + '\'' +
", height=" + height +
", weight=" + weight +
'}';
}
}
class BeautifulGirl implements Girl {
String name;
double height;
double weight;
public BeautifulGirl(CommonGirl girl) {
// 增高5cm
girl.height += 5;
// 减重10斤
girl.weight -= 10;
// 变美
this.name = girl.name;
this.height = girl.height;
this.weight = girl.weight;
}
@Override
public String toString() {
return "BeautifulGirl{" +
"name='" + name + '\'' +
", height=" + height +
", weight=" + weight +
'}';
}
@Override
public void sayHi() {
System.out.println("hi");
}
}
class GirlFriend implements Girl {
Girl girl;
String boyFirendName = "Eddie";
public GirlFriend(Girl girl) {
this.girl = girl;
}
@Override
public String toString() {
return "GirlFriend{" +
"girl=" + girl +
", boyFirendName='" + boyFirendName + '\'' +
'}';
}
@Override
public void sayHi() {
girl.sayHi();
}
}
class Main {
public static void main(String[] args) {
// 一个普通的女孩
CommonGirl commonGirl = new CommonGirl();
commonGirl.height = 160;
commonGirl.weight = 105;
commonGirl.name = "Ruby";
commonGirl.sayHi();
System.out.println(commonGirl);
// 蜕变成漂亮的女孩
BeautifulGirl beautifulGirl = new BeautifulGirl(commonGirl);
beautifulGirl.sayHi();
System.out.println(beautifulGirl);
// 变成女朋友了
GirlFriend girlFriend = new GirlFriend(beautifulGirl);
girlFriend.sayHi();
System.out.println(girlFriend);
}
}