参考:https://blog.csdn.net/weixin_48052161/article/details/118108908
门面模式就是相当于一键开启;
如上图所示:电脑组合了上面的cpu、disk、memory;
代码演示:
cpu:
package com.example.dtest.design23.facade;
public class CPU {
public void startup(){
System.out.println("cpu startup!");
}
public void shutdown(){
System.out.println("cpu shutdown!");
}
}
memory:
package com.example.dtest.design23.facade;
public class Memory {
public void startup(){
System.out.println("memory startup!");
}
public void shutdown(){
System.out.println("memory shutdown!");
}
}
disk:
package com.example.dtest.design23.facade;
public class Disk {
public void startup(){
System.out.println("disk startup!");
}
public void shutdown(){
System.out.println("disk shutdown!");
}
}
computer:
package com.example.dtest.design23.facade;
public class Computer {
private CPU cpu;
private Memory memory;
private Disk disk;
public Computer(){
cpu = new CPU();
memory = new Memory();
disk = new Disk();
}
public void startup(){
System.out.println("start the computer!");
cpu.startup();;
memory.startup();
disk.startup();
System.out.println("start computer finished!");
}
public void shutdown(){
System.out.println("begin to close the computer!");
cpu.shutdown();
memory.shutdown();
disk.shutdown();
System.out.println("computer closed!");
}
}
测试:
package com.example.dtest.design23.facade;
public class FacadeTest {
public static void main(String[] args) {
Computer computer = new Computer();
computer.startup();
computer.shutdown();
}
}
门面模式就是集成业务的模式,相对来所,耦合性高!