Facade mode (Facade), also called "Process mode: Facade mode provides a consistent interface for a set of interfaces in a subsystem , this mode defines a high-level interface, which makes this subsystem easier to use.
The appearance mode is used to shield the internal subsystem by defining a consistent interface details, so that the calling end only needs to make calls to this interface without having to care about the internal details of this subsystem.
Class diagram analysis:
Facade: Provides a unified calling interface for the calling end, what does the appearance class know? The subsystem is responsible for processing the request, thereby proxying the caller's request to the appropriate subsystem object.
Client: The caller of the appearance interface.
SubSystem collection: refers to the module or subsystem, which handles the tasks assigned by the Facade object. He is the actual provider of the function.
Background introduction:
Set up a home theater: DVD player, projector, automatic screen, surround sound, popcorn machine, required to complete the use of home theater The function is controlled by directly using the remote control (coordinating the switches of each device). The process is:
<pre class="brush:java;">public class DVDPlayer {
// 使用单例模式
private static final DVDPlayer instance = new DVDPlayer();
private DVDPlayer() {}
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("dvd 打开了...");
}
public void off() {
System.out.println("dvd 关闭了...");
}
public void play() {
System.out.println("dvd 播放中...");
}
public void pause() {
System.out.println("dvd 暂停了...");
}
}</pre><pre class="brush:java;">public class Popcorn {
private static final Popcorn instance = new Popcorn();
private Popcorn(){}
public static Popcorn getInstance() {
return instance;
}
public void on() {
System.out.println("爆米花机打开了...");
}
public void off() {
System.out.println("爆米花机关闭了...");
}
public void pop() {
System.out.println("爆米花做好了...");
}
}</pre><pre class="brush:java;">public class Projector {
private static final Projector instance = new Projector();
private Projector(){}
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("投影仪打开了...");
}
public void off() {
System.out.println("投影仪关闭了...");
}
public void focus() {
System.out.println("投影仪聚焦中...");
}
}</pre><pre class="brush:java;">public class Screen {
private static final Screen instance = new Screen();
private Screen(){}
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("屏幕上升...");
}
public void down() {
System.out.println("屏幕下降...");
}
}</pre><pre class="brush:java;">public class Stereo {
private static final Stereo instance = new Stereo();
private Stereo(){}
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("立体音响打开...");
}
public void off() {
System.out.println("立体音响关闭...");
}
public void up() {
System.out.println("立体音响音量+...");
}
public void down() {
System.out.println("立体音响音量-...");
}
}</pre><pre class="brush:java;">public class TheaterLight {
private static final TheaterLight instance = new TheaterLight();
private TheaterLight(){}
public static TheaterLight getInstance() {
return instance;
}
public void on() {
System.out.println("灯光打开...");
}
public void off() {
System.out.println("灯光关闭...");
}
public void dim() {
System.out.println("灯光亮度调暗...");
}
public void bright() {
System.out.println("灯光亮度调亮...");
}
}</pre>
<pre class="brush:java;">public class HomeTheaterFacade {
private DVDPlayer dvdPlayer;
private Popcorn popcorn;
private Projector projector;
private Stereo stereo;
private Screen screen;
private TheaterLight theaterLight;
public HomeTheaterFacade() {
this.dvdPlayer = DVDPlayer.getInstance();
this.popcorn = Popcorn.getInstance();
this.projector = Projector.getInstance();
this.stereo = Stereo.getInstance();
this.screen = Screen.getInstance();
this.theaterLight = TheaterLight.getInstance();
}
/**
* 准备开始
*/
public void ready() {
popcorn.on();
popcorn.pop();
screen.down();
projector.on();
projector.focus();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
/**
* 播放
*/
public void play() {
dvdPlayer.play();
}
/**
* 暂停
*/
public void pause() {
dvdPlayer.pause();
}
/**
* 结束
*/
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}</pre>
<pre class="brush:java;">public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
System.out.println("----------准备开始-----------");
homeTheaterFacade.ready();
System.out.println("----------开始播放-----------");
homeTheaterFacade.play();
System.out.println("----------播放暂停-----------");
homeTheaterFacade.pause();
System.out.println("----------播放结束-----------");
homeTheaterFacade.end();
}
}</pre>
Notes and details of appearance mode
, so the appearance mode reduces the complexity of the client's use of the subsystem.
.
The above is the detailed content of Java Design Patterns: Appearance Pattern Example Analysis. For more information, please follow other related articles on the PHP Chinese website!