为子系统中一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得子系统更加容易使用,组合基础类,提供复杂功能,外部不需要知道内部具体逻辑,只了解外观即可。
各子系统:
@Log4j2
class SubSystemOne {
public void methodOne() {
log.info("子系统方法1");
}
}
@Log4j2
class SubSystemTwo {
public void methodTwo() {
log.info("子系统方法2");
}
}
@Log4j2
class SubSystemThree {
public void methodThree() {
log.info("子系统方法1");
}
}
@Log4j2
class SubSystemFour {
public void methodFour() {
log.info("子系统方法4");
}
}
外观类:
@Log4j2
public class FacadePattern {
private SubSystemOne one;
private SubSystemTwo two;
private SubSystemThree three;
private SubSystemFour four;
public FacadePattern() {
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void methodA(){
log.info("功能组合A");
one.methodOne();
two.methodTwo();
four.methodFour();
}
public void methodB(){
log.info("功能组合B");
one.methodOne();
two.methodTwo();
three.methodThree();
}
}
客户端调用:
FacadePattern facade = new FacadePattern();
facade.methodA();
facade.methodB();
结果:
17:00:14.326 [main] INFO com.learn.putc.designpatterns.structure.facade.FacadePattern - 功能组合A
17:00:14.331 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemOne - 子系统方法1
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemTwo - 子系统方法2
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemFour - 子系统方法4
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.FacadePattern - 功能组合B
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemOne - 子系统方法1
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemTwo - 子系统方法2
17:00:14.332 [main] INFO com.learn.putc.designpatterns.structure.facade.SubSystemThree - 子系统方法1