Appearance mode is a common software design mode in software engineering. It provides a unified high-level interface for a group of interfaces in the subsystem. Subsystems are easier to use.
It is a State-mode UML diagram.
Structure
Facade
This appearance class provides a common external interface for packages 1, 2, and 3 in the subsystem.
Clients
A customer object reads and writes data resources of each interface in the subsystem through an external interface.
Packages
Internal libraries that customers can read through the appearance interface.
Let's take a look at the next small example to see the story of a DVD:
First, you must have a TV:
Public class TV {public void turnon () {system. out. println ("turn on TV");} public void turnoff () {system. out. println ("turn off TV ");}}
DVD:
Public class DVD {public void turnon () {system. out. println ("Open DVD");} public void putdvd (moviedisc movie) {system. out. println ("put the disc"); system. out. println ("start playing now:" + movie. getmoviename ();} public void takeout (moviedisc movie) {system. out. println ("Stop playing:" + movie. getmoviename (); system. out. println ("retrieve disc");} public void turnoff () {system. out. println ("turn off the DVD ");}}
Video:
Public class moviedisc {private string moviename; Public moviedisc (string moviename) {This. moviename = moviename; system. out. println ("this movie name is:" + moviename);} Public String getmoviename () {return moviename ;}}
When we want to watch a movie:
// Turn on the TV, DVD, put the film into it, and start watching the TV. turnon (); DVD. turnon (); DVD. putdvd (movie); // after reading it, watch another DVD. takeout (movie); moviedisc movie2 = new moviedisc ("golden robbery"); DVD. putdvd (movie2); // watch it. Shut down and sleep the DVD. takeout (movie); DVD. turnoff (); TV. turnoff ();
The customer calls are very complicated, so they have to do everything on their own...
Use appearance mode:
public class HomeTheater {DVD dvd = null;TV tv =null;MovieDisc movie = null;public HomeTheater(DVD dvd,TV tv,MovieDisc movie){this.dvd = dvd;this.movie = movie;this.tv =tv;}public void watchMovie() {tv.turnOn();dvd.turnOn();dvd.putDVD(movie);}public void endMovie(){dvd.takeOut(movie);dvd.turnOff();tv.turnOff();}public void chageMovie(MovieDisc movie2){dvd.takeOut(movie);dvd.putDVD(movie2);this.movie = movie2;}}
When you want to watch a movie:
HomeTheater homeTheater = new HomeTheater(dvd, tv, movie);homeTheater.watchMovie();homeTheater.chageMovie(movie2);homeTheater.endMovie();
You only need to call a few simple interfaces.
Take a look at the complete test class:
Public class TETS {public static void main (string [] ARGs) {// generate a TV, DVD, or disc TV = new TV (); DVD/DVD = new DVD (); moviedisc movie = new moviedisc ("taojie"); // turn on the TV, DVD, put it into the film, and start watching the TV. turnon (); DVD. turnon (); DVD. putdvd (movie); // after reading it, watch another DVD. takeout (movie); moviedisc movie2 = new moviedisc ("golden robbery"); DVD. putdvd (movie2); // watch it. Shut down and sleep the DVD. takeout (movie); DVD. turnoff (); TV. turnoff (); system. out. println ("-------- use cinema -------"); hometheater = new hometheater (DVD, TV, movie); hometheater. watchmovie (); hometheater. chagemovie (movie2); hometheater. endmovie ();}}
Result:
This movie is named Tao Jie.
Turn on the TV
Open a DVD
Put the disc
Start playing now: taojie
Stop playing: taojie
Retrieve a disc
Turn off the DVD
Disable TV
Stop playing: taojie
Retrieve a disc
This movie is called the golden robbery
Put the disc
Now play: the golden robbery
-------- Use cinema -------
Turn on the TV
Open a DVD
Put the disc
Start playing now: taojie
Stop playing: taojie
Retrieve a disc
Put the disc
Now play: the golden robbery
Stop playing: Golden robbery
Retrieve a disc
Turn off the DVD
Disable TV
The client can only call a few lines of code to implement the function, which is not so cumbersome.
Note the differences with other design patterns
Proxy Mode
1. The original class and the proxy class in proxy mode inherit the same parent class;
2. The original class object and the proxy class object interface have the same functions;
3 To hide the original class.
Adapter Mode
1. Only the adapter inherits the target interface;
2. Convert the original class interface to the interface required by the target code.
Appearance Mode
Is the integration and unified adaptation of multiple classes.
Good materials for learning the appearance mode:
Http://www.cnblogs.com/hegezhou_hot/archive/2010/12/06/1897398.html
Many of the above materials are collected from the Internet.