Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of Java appearance pattern

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "sample Analysis of Java appearance pattern". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Define

The appearance pattern provides a consistent interface for multiple complex subsystems, so that the caller only invokes with this interface without having to worry about the internal details of the subsystem.

Case requirement

When watching a movie, you need to do a series of operations, such as opening the player, putting down the screen, turning on the projector, turning on the stereo, etc., how to manage this?

Solution: appearance pattern implementation

Define the player class

Public class Player {private static Player player = new Player (); private Player () {} public static Player getInstance () {return player;} public void on () {System.out.println ("Open player");} public void off () {System.out.println ("close player") } public void play () {System.out.println ("use the player to play a movie");} public void pause () {System.out.println ("pause the player");} public void select () {System.out.println ("choose your favorite movie");}}

Define the projector class

Public class Projector {private static Projector projector = new Projector (); private Projector () {} public static Projector getInstance () {return projector;} public void on () {System.out.println ("put down the screen and turn on the projector");} public void off () {System.out.println ("turn off the projector") } public void focus () {System.out.println ("adjust projector focal length");} public void zoom () {System.out.println ("projector zoom");}}

Define audio class

Public class Stereo {private static Stereo stereo = new Stereo (); private Stereo () {} public static Stereo getInstance () {return stereo;} public void on () {System.out.println ("turn on the stereo");} public void off () {System.out.println ("turn off the stereo");} public void setVolume () {System.out.println ("adjust the volume") }}

Define popcorn machine class

/ * * popcorn machine * @ author:liyajie * @ createTime:2022/3/2 14:24 * @ version:1.0 * / public class Popcorn {private static Popcorn popcorn = new Popcorn (); private Popcorn () {} public static Popcorn getInstance () {return popcorn;} public void on () {System.out.println ("turn on the popcorn machine") } public void off () {System.out.println ("turn off the popcorn machine");} public void pop () {System.out.println ("make popcorn");}}

Define the appearance class of home theater

Public class HomeTheaterFacade {/ / aggregate each subsystem private Player player; private Popcorn popcorn; private Projector projector; private Stereo stereo; public HomeTheaterFacade () {} / / full parameter constructor public HomeTheaterFacade (Player player, Popcorn popcorn,Projector projector,Stereo stereo) {this.player = player; this.popcorn = popcorn; this.projector = projector; this.stereo = stereo } / / preparation phase public void ready () {popcorn.on (); player.on (); projector.on (); stereo.on ();} / / play public void play () {player.select (); popcorn.pop (); projector.focus (); stereo.setVolume () } / / end public void end () {player.off (); popcorn.off (); projector.off (); stereo.off ();}}

Define test classes

Public class Test {public static void main (String [] args) {HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade (Player.getInstance (), Popcorn.getInstance (), Projector.getInstance (), Stereo.getInstance ()); homeTheaterFacade.ready (); homeTheaterFacade.play (); homeTheaterFacade.end ();}}

View test results

Analysis.

We use the ready,play,end method of the home theater appearance class HomeTheaterFacade to operate the opening and closing of the player and projector. For the home theater appearance class, he does not need to know how to turn the player and projector on and off. Decoupling is realized to some extent, and it is easy to maintain and expand.

Summary

Advantages:

1. The appearance pattern shields the implementation details of the subsystem (security).

two。 The appearance mode reduces the complexity of the interaction between the client and the subsystem, decouples the client and the subsystem, and the modules within the subsystem are easier to maintain and expand.

3. Through the reasonable use of appearance mode, it can help us to better divide the access level.

Working with scen

1. When building a hierarchical system, the use of appearance patterns can simplify dependencies between subsystems

two。 When there is a complex system with many subsystems and a large number of operations, appearance patterns can be used.

3. When there is a lot of coupling between the client and multiple subsystems, the appearance pattern can be introduced to separate them and improve the independence and portability of the subsystems.

This is the end of "sample Analysis of Java appearance pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report