外观模式类似配置文件,将所有相关联的类解耦,通过一个外观类封装在一起。同时达到一个面向对象的作用。
烧包小程序员虽然对各个品牌的车子如数家珍,但是对于车子的原理、内部构造其实并不清楚。外观模式就是通过一个大罩子,将相互关联的组建封装在一起,完成行为。
class Engining{
public void startup(){
System.out.println("引擎启动!");
}
public void shutdown(){
System.out.println("引擎关闭!");
}
}
class Computer{
public void startup(){
System.out.println("行车电脑启动!");
}
public void shutdown(){
System.out.println("行车电脑关闭!");
}
}
class Car{
private Engining engining;
private Computer computer;
Car(Engining engining,Computer computer){
this.engining = engining;
this.computer = computer;
}
public void startup(){
this.computer.startup();
this.engining.startup();
System.out.println("车子发动起来啦!");
}
public void shutdown(){
this.computer.shutdown();
this.engining.shutdown();
System.out.println("车子熄火啦!");
}
}
//主函数
public class FacadePattern {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car = new Car(new Engining(),new Computer());
car.startup();
car.shutdown();
}
}