head first---------facade design pattern
head first----------外观模式或者门面模式
外观模式又名门面模式:提供了一个统一的接口,用来访问子系统中的一群接口。外观模式定义了一个高层接口,从而让子系统更容易使用。
package com.clark.facade.pattern;
import org.omg.CORBA.TCKind;
/**
* 功放、扩音器类
* @author Administrator
*
*/
public class Amplifier {
public void on(){
System.out.println("打开功放....");
}
public void off(){
System.out.println("关闭功放....");
}
//设置是DVD还是VCD播放模式
public void setPlayerModel(TvPlayerModel tvPlayerModel){
tvPlayerModel.setModel();
}
public void setSurroundSound(TvPlayerModel tvPlayerModel){
tvPlayerModel.setVoiceType();
}
//音量大小
public void setVoiceLevel(int voice){
System.out.println("音量大小为:"+voice);
}
}
import org.omg.CORBA.TCKind;
/**
* 功放、扩音器类
* @author Administrator
*
*/
public class Amplifier {
public void on(){
System.out.println("打开功放....");
}
public void off(){
System.out.println("关闭功放....");
}
//设置是DVD还是VCD播放模式
public void setPlayerModel(TvPlayerModel tvPlayerModel){
tvPlayerModel.setModel();
}
public void setSurroundSound(TvPlayerModel tvPlayerModel){
tvPlayerModel.setVoiceType();
}
//音量大小
public void setVoiceLevel(int voice){
System.out.println("音量大小为:"+voice);
}
}
package com.clark.facade.pattern;
/**
* DVD 播放
* @author Administrator
*
*/
public class DvdPlayerModel implements TvPlayerModel{
@Override
public void setModel() {
System.out.println("播放模式是DVD....");
}
public void on(){
System.out.println("打开Dvd player...");
}
public void off(){
System.out.println("关闭dvd player....");
}
public void play(String movie){
System.out.println("dvd 播放...."+movie);
}
public void setVoiceType(){
System.out.println("声音为立体声....");
}
public void setTwoChannelAudio(){
System.out.println("设置两个频道自动播放....");
}
public void pause(){
System.out.println("暂停....");
}
public void stop(){
System.out.println("停止播放....");
}
}
/**
* DVD 播放
* @author Administrator
*
*/
public class DvdPlayerModel implements TvPlayerModel{
@Override
public void setModel() {
System.out.println("播放模式是DVD....");
}
public void on(){
System.out.println("打开Dvd player...");
}
public void off(){
System.out.println("关闭dvd player....");
}
public void play(String movie){
System.out.println("dvd 播放...."+movie);
}
public void setVoiceType(){
System.out.println("声音为立体声....");
}
public void setTwoChannelAudio(){
System.out.println("设置两个频道自动播放....");
}
public void pause(){
System.out.println("暂停....");
}
public void stop(){
System.out.println("停止播放....");
}
}
package com.clark.facade.pattern;
/**
* 组装家庭影院的类,包括watchMovie(String type)
* endMovie()方法
* @author Administrator
*
*/
public class HomeTheaterFacade {
public Amplifier amplifier;
public Tuner tuner;
public TvPlayerModel tvModel;
public Projector projector;
public TheaterLight lights;
public Screen screen;
public PopCornPopper popper;
public HomeTheaterFacade(Amplifier amplifier, Tuner tuner,
TvPlayerModel tvModel, Projector projector, TheaterLight lights,
Screen screen, PopCornPopper popper) {
this.amplifier = amplifier;
this.tuner = tuner;
this.tvModel = tvModel;
this.projector = projector;
this.lights = lights;
this.screen = screen;
this.popper = popper;
}
//encapsulation watch movie
public void watchMovie(String movie){
System.out.println("Get ready to watch a movie......");
popper.on();
popper.pop();
lights.dim(10);
screen.down();
projector.on();
projector.setWidenScreenModel();
amplifier.on();
amplifier.setPlayerModel(tvModel);
amplifier.setSurroundSound(tvModel);
amplifier.setVoiceLevel(5);
tvModel.on();
tvModel.play(movie);
}
//close movie
public void endMovie(){
System.out.println("Shutting movie theater down.....");
popper.off();
lights.off();
screen.up();
projector.off();
amplifier.off();
tvModel.stop();
tvModel.off();
}
}
/**
* 组装家庭影院的类,包括watchMovie(String type)
* endMovie()方法
* @author Administrator
*
*/
public class HomeTheaterFacade {
public Amplifier amplifier;
public Tuner tuner;
public TvPlayerModel tvModel;
public Projector projector;
public TheaterLight lights;
public Screen screen;
public PopCornPopper popper;
public HomeTheaterFacade(Amplifier amplifier, Tuner tuner,
TvPlayerModel tvModel, Projector projector, TheaterLight lights,
Screen screen, PopCornPopper popper) {
this.amplifier = amplifier;
this.tuner = tuner;
this.tvModel = tvModel;
this.projector = projector;
this.lights = lights;
this.screen = screen;
this.popper = popper;
}
//encapsulation watch movie
public void watchMovie(String movie){
System.out.println("Get ready to watch a movie......");
popper.on();
popper.pop();
lights.dim(10);
screen.down();
projector.on();
projector.setWidenScreenModel();
amplifier.on();
amplifier.setPlayerModel(tvModel);
amplifier.setSurroundSound(tvModel);
amplifier.setVoiceLevel(5);
tvModel.on();
tvModel.play(movie);
}
//close movie
public void endMovie(){
System.out.println("Shutting movie theater down.....");
popper.off();
lights.off();
screen.up();
projector.off();
amplifier.off();
tvModel.stop();
tvModel.off();
}
}
package com.clark.facade.pattern;
/**
* 观赏电影
* @author Administrator
*
*/
public class HomeTheaterTest {
public static void main(String[] args) {
//实例化组件
Amplifier amplifier=new Amplifier();
Tuner tuner=new Tuner();
TvPlayerModel tvModel=new DvdPlayerModel();
Projector projector=new Projector();
Screen screen=new Screen();
TheaterLight lights=new TheaterLight();
PopCornPopper popCornPopper=new PopCornPopper();
HomeTheaterFacade homeTheater=new HomeTheaterFacade(amplifier, tuner, tvModel, projector, lights, screen, popCornPopper);
homeTheater.watchMovie("火线三兄弟...........");
homeTheater.endMovie();
}
}
/**
* 观赏电影
* @author Administrator
*
*/
public class HomeTheaterTest {
public static void main(String[] args) {
//实例化组件
Amplifier amplifier=new Amplifier();
Tuner tuner=new Tuner();
TvPlayerModel tvModel=new DvdPlayerModel();
Projector projector=new Projector();
Screen screen=new Screen();
TheaterLight lights=new TheaterLight();
PopCornPopper popCornPopper=new PopCornPopper();
HomeTheaterFacade homeTheater=new HomeTheaterFacade(amplifier, tuner, tvModel, projector, lights, screen, popCornPopper);
homeTheater.watchMovie("火线三兄弟...........");
homeTheater.endMovie();
}
}
package com.clark.facade.pattern;
/**
* 爆米花类
* @author Administrator
*
*/
public class PopCornPopper {
public void on(){
System.out.println("打开爆米花机......");
}
public void off(){
System.out.println("关闭爆米花机......");
}
public void pop(){
System.out.println("开始爆米花机......");
}
}
/**
* 爆米花类
* @author Administrator
*
*/
public class PopCornPopper {
public void on(){
System.out.println("打开爆米花机......");
}
public void off(){
System.out.println("关闭爆米花机......");
}
public void pop(){
System.out.println("开始爆米花机......");
}
}
package com.clark.facade.pattern;
/**
* 投影仪
* @author Administrator
*
*/
public class Projector {
//set player on model
public void setWidenScreenModel(){
System.out.println("projecor model is widen....");
}
public void on(){
System.out.println("打开投影仪....");
}
public void off(){
System.out.println("关闭投影仪...");
}
//设置投影仪模式
public void setTvModel(TvPlayerModel tvModel){
tvModel.setModel();
}
}
/**
* 投影仪
* @author Administrator
*
*/
public class Projector {
//set player on model
public void setWidenScreenModel(){
System.out.println("projecor model is widen....");
}
public void on(){
System.out.println("打开投影仪....");
}
public void off(){
System.out.println("关闭投影仪...");
}
//设置投影仪模式
public void setTvModel(TvPlayerModel tvModel){
tvModel.setModel();
}
}
package com.clark.facade.pattern;
/**
* 屏幕类
* @author Administrator
*
*/
public class Screen {
public void down(){
System.out.println("把屏幕放下...");
}
public void up(){
System.out.println("把屏幕拉上...");
}
}
/**
* 屏幕类
* @author Administrator
*
*/
public class Screen {
public void down(){
System.out.println("把屏幕放下...");
}
public void up(){
System.out.println("把屏幕拉上...");
}
}
package com.clark.facade.pattern;
/**
* 电影院的类
* @author Administrator
*
*/
public class TheaterLight {
public void on(){
System.out.println("打开电影院....");
}
public void off(){
System.out.println("关闭电影院....");
}
public void dim(int volumn){
System.out.println("灯光亮度是原来的"+volumn+"%");
}
}
/**
* 电影院的类
* @author Administrator
*
*/
public class TheaterLight {
public void on(){
System.out.println("打开电影院....");
}
public void off(){
System.out.println("关闭电影院....");
}
public void dim(int volumn){
System.out.println("灯光亮度是原来的"+volumn+"%");
}
}
package com.clark.facade.pattern;
/**
* 收音机类
* @author Administrator
*
*/
public class Tuner {
public void on(){
System.out.println("打开tuner...");
}
public void off(){
System.out.println("关闭tuner...");
}
public void setAm(){
System.out.println("跳到 am 频道...");
}
public void setFm(){
System.out.println("跳到 fm 频道....");
}
}
/**
* 收音机类
* @author Administrator
*
*/
public class Tuner {
public void on(){
System.out.println("打开tuner...");
}
public void off(){
System.out.println("关闭tuner...");
}
public void setAm(){
System.out.println("跳到 am 频道...");
}
public void setFm(){
System.out.println("跳到 fm 频道....");
}
}
package com.clark.facade.pattern;
/**
* 播放模式,DVD、VCD
* @author Administrator
*
*/
public interface TvPlayerModel {
public void setModel();
public void setVoiceType();//声道种类
public void on();
public void off();
public void play(String movie);
public void setTwoChannelAudio();
public void pause();
public void stop();
}
/**
* 播放模式,DVD、VCD
* @author Administrator
*
*/
public interface TvPlayerModel {
public void setModel();
public void setVoiceType();//声道种类
public void on();
public void off();
public void play(String movie);
public void setTwoChannelAudio();
public void pause();
public void stop();
}
package com.clark.facade.pattern;
/**
* VCD 播放
* @author Administrator
*
*/
public class VcdPlayer implements TvPlayerModel{
@Override
public void setModel() {
System.out.println("播放模式是VCD....");
}
public void on(){
System.out.println("打开vcd player...");
}
public void off(){
System.out.println("关闭vcd player....");
}
public void play(String movie){
System.out.println("vcd 播放...."+movie);
}
public void setVoiceType(){
System.out.println("声音为立体声....");
}
public void setTwoChannelAudio(){
System.out.println("设置两个频道自动播放....");
}
public void pause(){
System.out.println("暂停....");
}
public void stop(){
System.out.println("停止播放....");
}
}
/**
* VCD 播放
* @author Administrator
*
*/
public class VcdPlayer implements TvPlayerModel{
@Override
public void setModel() {
System.out.println("播放模式是VCD....");
}
public void on(){
System.out.println("打开vcd player...");
}
public void off(){
System.out.println("关闭vcd player....");
}
public void play(String movie){
System.out.println("vcd 播放...."+movie);
}
public void setVoiceType(){
System.out.println("声音为立体声....");
}
public void setTwoChannelAudio(){
System.out.println("设置两个频道自动播放....");
}
public void pause(){
System.out.println("暂停....");
}
public void stop(){
System.out.println("停止播放....");
}
}
//=================Test result ======================
Get ready to watch a movie......
打开爆米花机......
开始爆米花机......
灯光亮度是原来的10%
把屏幕放下...
打开投影仪....
projecor model is widen....
打开功放....
播放模式是DVD....
声音为立体声....
音量大小为:5
打开Dvd player...
dvd 播放....火线三兄弟...........
Shutting movie theater down.....
关闭爆米花机......
关闭电影院....
把屏幕拉上...
关闭投影仪...
关闭功放....
停止播放....
关闭dvd player....
打开爆米花机......
开始爆米花机......
灯光亮度是原来的10%
把屏幕放下...
打开投影仪....
projecor model is widen....
打开功放....
播放模式是DVD....
声音为立体声....
音量大小为:5
打开Dvd player...
dvd 播放....火线三兄弟...........
Shutting movie theater down.....
关闭爆米花机......
关闭电影院....
把屏幕拉上...
关闭投影仪...
关闭功放....
停止播放....
关闭dvd player....