Home > Java > javaTutorial > Java Design Patterns: Appearance Pattern Example Analysis

Java Design Patterns: Appearance Pattern Example Analysis

WBOY
Release: 2023-04-20 15:49:08
forward
1047 people have browsed it

Mode introduction

  • 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.

UML Class Diagram

Java Design Patterns: Appearance Pattern Example Analysis

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.

##Appearance mode Case:

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:

  • Open the popcorn machine

  • Put it down Screen

  • Turn on the projector

  • Turn on the stereo

  • Turn on the DVD and select dvd

  • Get the popcorn

  • Dim the lights

  • Play

  • After watching the movie, turn off various devices.

Java Design Patterns: Appearance Pattern Example Analysis

##The codes for DVD, Popcorn, Projector, Screen, Stereo, and TheaterLight are as follows:

<div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;dvd 打开了...&quot;); } public void off() { System.out.println(&quot;dvd 关闭了...&quot;); } public void play() { System.out.println(&quot;dvd 播放中...&quot;); } public void pause() { System.out.println(&quot;dvd 暂停了...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;爆米花机打开了...&quot;); } public void off() { System.out.println(&quot;爆米花机关闭了...&quot;); } public void pop() { System.out.println(&quot;爆米花做好了...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;投影仪打开了...&quot;); } public void off() { System.out.println(&quot;投影仪关闭了...&quot;); } public void focus() { System.out.println(&quot;投影仪聚焦中...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;屏幕上升...&quot;); } public void down() { System.out.println(&quot;屏幕下降...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;立体音响打开...&quot;); } public void off() { System.out.println(&quot;立体音响关闭...&quot;); } public void up() { System.out.println(&quot;立体音响音量+...&quot;); } public void down() { System.out.println(&quot;立体音响音量-...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><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(&quot;灯光打开...&quot;); } public void off() { System.out.println(&quot;灯光关闭...&quot;); } public void dim() { System.out.println(&quot;灯光亮度调暗...&quot;); } public void bright() { System.out.println(&quot;灯光亮度调亮...&quot;); } }</pre><div class="contentsignin">Copy after login</div></div>

HomeTheaterFacade(Coordinate the switches of each device)

<div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>

Client(Test)

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:java;">public class Client { public static void main(String[] args) { HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(); System.out.println(&quot;----------准备开始-----------&quot;); homeTheaterFacade.ready(); System.out.println(&quot;----------开始播放-----------&quot;); homeTheaterFacade.play(); System.out.println(&quot;----------播放暂停-----------&quot;); homeTheaterFacade.pause(); System.out.println(&quot;----------播放结束-----------&quot;); homeTheaterFacade.end(); } }</pre><div class="contentsignin">Copy after login</div></div>

Implementation results:

Java Design Patterns: Appearance Pattern Example AnalysisNotes and details of appearance mode

    Appearance mode
  • The subsystem is shielded from the outside The details

    , so the appearance mode reduces the complexity of the client's use of the subsystem.

  • The appearance mode has a coupling relationship between the client and the subsystem - decoupling, allowing the subsystem to The internal modules of the system are easier to maintain and expand
  • By rationally using the appearance mode, we can help us better
  • divide the levels of access

    .

  • When the system requires layered design, you can consider using the Facade mode.
  • When maintaining a large legacy system, the system may have become very difficult to maintain and expand. At this time, you can consider developing a Facade class for the new system to provide the functionality of the legacy system. A relatively clear and simple interface allows the new system to interact with the Facade class and improves reusability.
  • Do not use appearance mode too much or unreasonably. It is better to use appearance mode or call the module directly. The purpose is to make the system hierarchical and facilitate maintenance.

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template