[Design mode] study Note 10: appearance mode (facade)

Source: Internet
Author: User

From http://blog.csdn.net/shuangde800

The last time we learned how the adapter mode converts an interface of a class to another interface that meets customer expectations. To achieve this in Java, an incompatible interface object must be encapsulated to become a compatible object.

What I want to learn this time isAppearance mode.

The appearance mode is to hide all the complex things of one or more classes behind the scenes, only revealing a clean and beautiful appearance.


Approaching appearance Mode

Suppose you want to design a home theater in Java, including a DVD player, projector, automatic screen, surround sound, and even a popcorn machine. You have designed some classes:



To watch a movie, take the following steps:

1. Open the popcorn machine

2. Start popcorn

3. Dim the light

4. Drop the screen

..... (Omitted below)

Call these classes:

popper.on();popper.pop();lights.dim(10);screen.down();projector.on();projector.wideScreenMode();amp.on();amp.setDvd(dvd);amp.setSurroundSound();amp.setVolume(5);dvd.on();dvd.play(movie);


It is very troublesome to do this every time. If you want to close it after reading it, it will be very troublesome.


With the appearance mode, you can implement an appearance class that provides more reasonable interfaces to make it easy to integrate complex subsystems.

Public class hometheaterfacade {amplifier amp; tuner; dvdplayer DVD; cdplayer CD; projector; theaterlights lights; screen; popcornpopper Popper; // constructor, upload necessary equipment to public hometheaterfacade (amplifier amp, tuner, dvdplayer DVD, cdplayer CD, projector, screen, theaterlights lights, popcornpopper Popper) {This. amp = amp; this. tuner = tuner; this. DVD = DVD; this. cd = Cd; this. projector = projector; this. screen = screen; this. lights = lights; this. popper = Popper;} // encapsulate a series of actions to be prepared for a movie in this method 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. widescreenmode (); amp. on (); amp. setdvd (DVD); amp. setsurroundsound (); amp. setvolume (5); DVD. on (); DVD. play (movie);} // encapsulate a series of actions of the home theater system into this method public void endmovie () {system. out. println ("shutting movie theater down... "); popper. off (); lights. on (); screen. up (); projector. off (); amp. off (); DVD. stop (); DVD. eject (); DVD. off ();}}

In this way, to watch a movie, you only need to call the watchmovie method of this class! Is it several hundred times easier?



Define appearance Mode

The appearance mode provides a unified interface to access a group of interfaces in the subsystem. The appearance mode defines a high-level interface to make it easier for subsystems to use.


The appearance mode is quite straightforward and easy to understand. This is not the same as other models. But this does not reduce it. For example, the appearance mode allows us to avoid tight coupling between customers and subsystems.

The appearance not only simplifies the interface, but also connects the customer from the sub-system of the component

The appearance and adapter can wrap many classes, but the appearance is intended to simplify the interface, and the adapter's intention is to convert the interface into different interfaces.


Appearance mode class diagram:

Design Principle: "Minimum knowledge" Principle

Minimum knowledge principle: only talk to your close friend

This means that when designing a system, whether it is any object, you should pay attention to what classes it interacts with and how it interacts with these classes.


This principle requires that we do not allow too many classes to be coupled together, so that modifying part of the system may affect other parts. If many classes are mutually dependent, the system will become a fragile system, which requires a lot of maintenance costs and will be too complicated to be easily understood by others.

Principles of this Principle:

For any object, in this object method, we should only call methods that fall within the following scope:

1. the object itself

2. Objects passed as method parameters

3. Any object created or instantiated by this method

4. Any component of the object


The first three tell us that if an object calls the returned results of other methods, do not call the method of this object!

For example, the following method is not recommended:

public float getTemp() {    return station.getThermometre.getTemperature();}

Instead, implement a gettemperature () in the station, and then call it directly from the station:

public float getTemp() {    return station.getTemperature();}

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: info-contact@alibabacloud.com 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.