Facade

Facade Pattern:为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用。他是为子系统中的一组接口所提供的一个一致的界面

Tv.java

 

package com.flyingh.bean;

public class Tv {
	public void turnOn() {
		System.out.println("turn on the TV!");
	}

	public void chooseChannel() {
		System.out.println("choose the TV channel!");
	}

	public void changeToVideo() {
		System.out.println("turn to video!");
	}

	public void setTvBrightness() {
		System.out.println("set the TV's brightness!");
	}

	public void setTvVolume() {
		System.out.println("set the TV's volume!");
	}

	public void turnOff() {
		System.out.println("turn off the TV!");
	}
}

 Dvd.java

 

package com.flyingh.bean;

public class Dvd {

	public void turnOn() {
		System.out.println("turn on the DVD!");
	}

	public void pop() {
		System.out.println("pop it to put disk in or get disk out!");
	}

	public void putDiskIn() {
		System.out.println("put the dvd disk in!");
	}

	public void push() {
		System.out
				.println("push it after putting the disk in or getting the disk out!");
	}

	public void setDvdVolume() {
		System.out.println("set the dvd volume!");
	}

	public void getDiskOut() {
		System.out.println("get the dvd disk out!");
	}

	public void turnOff() {
		System.out.println("turn off the DVD!");
	}

}

 Radio.java

 

package com.flyingh.bean;

public class Radio {
	public void turnOn() {
		System.out.println("turn on the radio!");
	}

	public void chooseChannel() {
		System.out.println("choose the radio channel,turn to am or fm!");
	}

	public void rotate() {
		System.out.println("rotate the line!");
	}

	public void setVolume() {
		System.out.println("set the radio volume!");
	}

	public void turnOff() {
		System.out.println("turn off the radio!");
	}
}

 Facade.java

 

package com.flyingh.facade;

import com.flyingh.bean.Dvd;
import com.flyingh.bean.Radio;
import com.flyingh.bean.Tv;

public class Facade {
	private Tv tv;
	private Dvd dvd;
	private Radio radio;

	public Facade(Tv tv, Dvd dvd, Radio radio) {
		super();
		this.tv = tv;
		this.dvd = dvd;
		this.radio = radio;
	}

	public void endWatchDvdAndTv() {
		closeDvd();
		closeTv();
	}

	public void watchTv() {
		tv.turnOn();
		tv.chooseChannel();
		tv.setTvVolume();
		tv.setTvBrightness();
		System.out.println("start to watch TV!");
	}

	public void closeTv() {
		tv.turnOff();
	}

	public void watchDvd() {
		tv.turnOn();
		tv.changeToVideo();
		dvd.turnOn();
		dvd.pop();
		dvd.putDiskIn();
		dvd.push();
		dvd.setDvdVolume();
		tv.setTvVolume();
		tv.setTvBrightness();
		System.out.println("start to watch DVD!");
	}

	public void closeDvd() {
		dvd.pop();
		dvd.getDiskOut();
		dvd.push();
		dvd.turnOff();
	}

	public void listenRadio() {
		radio.turnOn();
		radio.chooseChannel();
		radio.rotate();
		radio.setVolume();
		System.out.println("start to listen to radio!");
	}

	public void closeRadio() {
		radio.turnOff();
	}

}

 Client.java

 

package com.flyingh.client;

import com.flyingh.bean.Dvd;
import com.flyingh.bean.Radio;
import com.flyingh.bean.Tv;
import com.flyingh.facade.Facade;

public class Client {
	public static void main(String[] args) {
		Tv tv = new Tv();
		Dvd dvd = new Dvd();
		Radio radio = new Radio();
		Facade facade = new Facade(tv, dvd, radio);
		facade.watchTv();
		System.out.println("***************");
		facade.closeTv();
		System.out.println("***************");
		facade.watchDvd();
		System.out.println("***************");
		facade.closeDvd();
		System.out.println("***************");
		facade.listenRadio();
		System.out.println("***************");
		facade.closeRadio();
		System.out.println("***************");
		facade.watchDvd();
		System.out.println("***************");
		facade.endWatchDvdAndTv();
	}
}

 程序运行结果如下:

 

turn on the TV!
choose the TV channel!
set the TV's volume!
set the TV's brightness!
start to watch TV!
***************
turn off the TV!
***************
turn on the TV!
turn to video!
turn on the DVD!
pop it to put disk in or get disk out!
put the dvd disk in!
push it after putting the disk in or getting the disk out!
set the dvd volume!
set the TV's volume!
set the TV's brightness!
start to watch DVD!
***************
pop it to put disk in or get disk out!
get the dvd disk out!
push it after putting the disk in or getting the disk out!
turn off the DVD!
***************
turn on the radio!
choose the radio channel,turn to am or fm!
rotate the line!
set the radio volume!
start to listen to radio!
***************
turn off the radio!
***************
turn on the TV!
turn to video!
turn on the DVD!
pop it to put disk in or get disk out!
put the dvd disk in!
push it after putting the disk in or getting the disk out!
set the dvd volume!
set the TV's volume!
set the TV's brightness!
start to watch DVD!
***************
pop it to put disk in or get disk out!
get the dvd disk out!
push it after putting the disk in or getting the disk out!
turn off the DVD!
turn off the TV!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值