Design Pattern (ix): Appearance mode

Source: Internet
Author: User

I. Overview

  The appearance mode provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.

Second, solve the problem

In the previous lecture, we learned the adapter pattern, which is used to transform an interface, and the appearance pattern can be understood as a transformation of a group of interfaces, the customer just call an interface, without invoking multiple interfaces can achieve the purpose. Think of real-life examples, when we install software on a PC, we often have a default installation or a one-click installation option (eliminating the need to choose the installation directory, installed components, etc.), as well as the mobile phone restart function (gatekeeper and start together as an operation). In short, the appearance of the model is to solve the difficulties caused by multiple complex interfaces, to simplify the role of user operations.

Third, structure class diagram

Iv. Role of Members

  Client: The subsystem can be used directly by invoking the façade interface.

  appearance (facade): encapsulates one or more interfaces of a subsystem and provides a unified interface for client invocation.

V. Examples of applications

In recent years has been more popular smart home, in my opinion is a lot of home appliances connected to the net, we can operate these appliances on the mobile phone, has brought us a lot of convenience. Let's assume a scenario where we go home at night, we need to turn on the lights, turn on the air conditioning, close the windows, pull the curtains, put the music, and complete these five actions before we can comfortably lie on the couch and relieve the toil of the day. If you want to do such a daily operation is too troublesome, now our smart home came, he gave us a mobile app, as long as the near home when pressing a button, a variety of appliances will automatically work, create a comfortable home environment waiting for us back. Let's see how we do it.

The first step, create a variety of electrical appliances

Package facade.pattern;//lamp public class Light {public void on () {System.out.println ("Electric light turned On");} public void Off () {System.out.println ("light Off");}}


Package facade.pattern;//Air Conditioning public class Aircondition {public void on () {System.out.println ("air conditioning turned on");} public void Off () {System.out.println ("air conditioner Off");}}


Package facade.pattern;//Windows public class Windows {public void on () {System.out.println ("windows open");} public void Off () {System.out.println ("window closed");}}


Package facade.pattern;//curtains public class Curtain {public void on () {System.out.println ("curtains Open");} public void Off () {System.out.println ("curtain Closed");}}


Package Facade.pattern;//mp3 player public class Mp3player {public void on () {System.out.println ("MP3 player opened"); public void Off () {System.out.println ("MP3 player Off");}}

Step two, create a mobile app

Package facade.pattern;//Mobile Apppublic class App {private light light;private aircondition aircondition;private Window Window;private Curtain curtain;private Mp3player mp3;//instantiated when initializing the appliance public App (light, aircondition aircondition, Window window,curtain Curtain, Mp3player mp3) {super (); this.light = Light;this.aircondition = Aircondition;this.window = Window;this.curtain = Curtain;this.mp3 = MP3;} Set Home mode, open the required appliance public void AtHome () {Light.on (); Aircondition.on (); (); (); Mp3.on ();}}

Step three, test your phone app

Package Facade.pattern;public class Apptest {public static void main (string[] args) {Light light = new Light (); Aircondition aircondition = new Aircondition (); window window = new window (); Curtain curtain = new Curtain (); Mp3player mp3 = new Mp3player (); App = new app (Light,aircondition,window,curtain,mp3);//The above code customers can not know SYSTEM.OUT.PRINTLN ("---Set up a smart home with your phone, set to Home mode--- ");//Customer One-click Operation Set Home Mode App.athome ();}}

Operation Result:


Vi. Advantages and Disadvantages

 1. Advantages

(1), the appearance of not only simplifies the interface, but also the customer from the formation of the subsystem decoupling. customers only need to focus on the interfaces provided by the façade, without needing to care about the classes of the subsystems that the skins encapsulate.

(2), providing a simplified interface, while still exposing the integrity of the system, for the needs of people to use.

Vii. use of the scene

1. Use appearance when you need to simplify and unify a large interface or simplify a group of complex interfaces.

2. We want customers to decouple from a complex subsystem.

Viii. design principles that can be used in the appearance mode--least knowledge principle

Minimum knowledge principle definition: talk to your closest friend only.

  The principle of least knowledge design is that in the case of any object, within the method of that object, we should only invoke methods that fall into the following scope:

  1. The object itself

Package Facade.pattern;public class Lighttest {public void on () {System.out.println ("Light is turned on");} public void Off () {System.out.println ("light Off");} public void Test () {//Other methods in this class are called in the method on ();}}

  2. The object passed in as a parameter of the method (example of the mobile app for the appearance mode)

Mobile Apppublic class App {        //out-of-mode public void Goout, aircondition aircondition, Window window,curtain curta In, Mp3player mp3) { (); (); Window.on (); Curtain.on (); ();}}

  3. Any object created or instantiated by this method

Mobile Apppublic class App {private light light;private aircondition aircondition;private Window window;private curtain Curt    Ain;private mp3player mp3; Public        App () {        }public mp3player getMp3 () {return mp3;} public void SetMp3 (Mp3player mp3) {This.mp3 = MP3;} public void StartMp3 () {Mp3.on ();}}

Not recommended for use as follows

public void Startmp3player () {mp3player mp3 = new App (). GETMP3 (); Mp3.on ();}

should read

public void Startmp3player () {new App (). STARTMP3 ();}

4. Any component of the object

Mobile Apppublic class App {private light light;private aircondition aircondition;private Window window;private curtain Curt    Ain;private mp3player mp3; Public        App () {        }        //In this method, you can use any method of component mp3 public void StartMp3 () {Mp3.on ();}}

Design Pattern (ix): Appearance mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.