设计模式-外观模式

步骤1

//空调
public class AirConditioner {
    public void on() {
        System.out.println("打开空调");
    }

    public void off() {
        System.out.println("关闭空调");
    }
}
//电脑
public class Computer {
    public void on() {
        System.out.println("打开电脑");
    }

    public void off() {
        System.out.println("关闭电脑");
    }
}

步骤2

public class RemoteControl {
    private AirConditioner conditioner;
    private Computer computer;
    public RemoteControl() {
        super();
        this.conditioner = new AirConditioner();
        this.computer = new Computer();
    }
    
    public void onAll() {
        conditioner.on();
        computer.on();
    }
    
    public void offAll() {
        conditioner.off();
        computer.off();
    }
}

步骤3

RemoteControl control = new RemoteControl();
control.onAll();
System.out.println();
control.offAll();

输出

打开空调
打开电脑

关闭空调
关闭电脑

你可能感兴趣的:(设计模式-外观模式)