java设计模式之外观模式

外观模式:外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

//子系统类(电灯类)
public class Light {
	private String position;
	public Light(String position){
		this.position=position;
	}
	public void on(){
		System.out.println(this.position+"灯打开");
	}
	public void off(){
		System.out.println(this.position+"灯关闭");
	}
}

//子系统类
public class Fan {
	
	public void on(){
		System.out.println("风扇打开");
	}
	public void off(){
		System.out.println("风扇关闭");
	}

}

//子系统类
public class AirConditioner {
	public void on(){
		System.out.println("空调打开");
	}
	public void off(){
		System.out.println("空调关闭");
	}
	
}

//子系统类
public class TV {
	public void on(){
		System.out.println("电视机打开");
	}
	public void off(){
		System.out.println("电视机关闭");
	}
}

//外观类(总开关类)
public class SwitchFacade {
	private Light light[]=new Light[4];
	private Fan fan;
	private AirConditioner ac;
	private TV tv;
	public SwitchFacade(){
		light[0]=new Light("左前");
		light[1]=new Light("左后");
		light[2]=new Light("右前");
		light[3]=new Light("右前");
		fan=new Fan();
		ac=new AirConditioner();
		tv=new TV();
	}
	public void on(){
		light[0].on();
		light[1].on();
		light[2].on();
		light[3].on();
		fan.on();
		ac.on();
		tv.on();
	}
	public void off(){
		light[0].off();
		light[1].off();
		light[2].off();
		light[3].off();
		fan.off();
		ac.off();
		tv.off();
	}

}

//测试类
public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwitchFacade sf=new SwitchFacade();
		sf.on();
		System.out.println("---------------------------");
		sf.off();

	}

}


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