Design pattern (8) - Appearance Model

Appearance (Facade) model provides a unified interface, the interface used to access a group of subsystems, the appearance of a high-level interface that allows the subsystem easier to use. Appearance model is easy to understand, so that it allows us to avoid the tight coupling between the client and subsystems. Here it is given to FIG class

The reason to change the appearance of the interface mode is designed to simplify the interface, the reason for that name, because of the complexity of it all one or several classes are hidden in the back, only to reveal a clean, bright look.

Described below with reference to an example. We simulated a home theater, containing a DVD player, projector, automatic screen, surround sound, and even a popcorn machine. We need to make each part of the system is written as a class. Here with a DVD player, for example, write a class

public class DvdPlayer {
	String description;
	int currentTrack;
	Amplifier amplifier;
	String movie;
	
	public DvdPlayer(String description, Amplifier amplifier) {
		this.description = description;
		this.amplifier = amplifier;
	}
 
	public void on() {
		System.out.println(description + " on");
	}
 
	public void off() {
		System.out.println(description + " off");
	}

        public void eject() {
		movie = null;
                System.out.println(description + " eject");
        }
 
	public void play(String movie) {
		this.movie = movie;
		currentTrack = 0;
		System.out.println(description + " playing \"" + movie + "\"");
	}

	public void play(int track) {
		if (movie == null) {
			System.out.println(description + " can't play track " + track + " no dvd inserted");
		} else {
			currentTrack = track;
			System.out.println(description + " playing track " + currentTrack + " of \"" + movie + "\"");
		}
	}

	public void stop() {
		currentTrack = 0;
		System.out.println(description + " stopped \"" + movie + "\"");
	}
 
	public void pause() {
		System.out.println(description + " paused \"" + movie + "\"");
	}

	public void setTwoChannelAudio() {
		System.out.println(description + " set two channel audio");
	}
 
	public void setSurroundAudio() {
		System.out.println(description + " set surround audio");
	}
 
	public String toString() {
		return description;
	}
}

We need to open one by one these components before you begin watching a movie, these tasks will be written to call a lot of classes and methods. After watching the film, you have to put everything off and put it all in reverse if the action once again, very complex on writing code. For this purpose we will use the facade pattern, provide a more reasonable appearance of the class interface by implementing, we can be a complex subsystem easier to use.

public class HomeTheaterFacade {
	Amplifier amp;
	Tuner tuner;
	DvdPlayer dvd;
	CdPlayer cd;
	Projector projector;
	TheaterLights lights;
	Screen screen;
	PopcornPopper popper;
 
	public HomeTheaterFacade(Amplifier amp, 
				 Tuner tuner, 
				 DvdPlayer dvd, 
				 CdPlayer cd, 
				 Projector projector, 
				 Screen 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;
	}
 
	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);
	}
 
 
	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();
	}

	public void listenToCd(String cdTitle) {
		System.out.println("Get ready for an audiopile experence...");
		lights.on();
		amp.on();
		amp.setVolume(5);
		amp.setCd(cd);
		amp.setStereoSound();
		cd.on();
		cd.play(cdTitle);
	}

	public void endCd() {
		System.out.println("Shutting down CD...");
		amp.off();
		amp.setCd(cd);
		cd.eject();
		cd.off();
	}

	public void listenToRadio(double frequency) {
		System.out.println("Tuning in the airwaves...");
		tuner.on();
		tuner.setFrequency(frequency);
		amp.on();
		amp.setVolume(5);
		amp.setTuner(tuner);
	}

	public void endRadio() {
		System.out.println("Shutting down the tuner...");
		tuner.off();
		amp.off();
	}
}

As a result, when we watch movies to write code in the test program, it will be very easy:

public class HomeTheaterTestDrive {
	public static void main(String[] args) {
		Amplifier amp = new Amplifier("Top-O-Line Amplifier");
		Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);
		DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp);
		CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);
		Projector projector = new Projector("Top-O-Line Projector", dvd);
		TheaterLights lights = new TheaterLights("Theater Ceiling Lights");
		Screen screen = new Screen("Theater Screen");
		PopcornPopper popper = new PopcornPopper("Popcorn Popper");
 
		HomeTheaterFacade homeTheater = 
				new HomeTheaterFacade(amp, tuner, dvd, cd, 
						projector, screen, lights, popper);
 
		homeTheater.watchMovie("Raiders of the Lost Ark");//简化的接口
		homeTheater.endMovie();
	}
}

Design Principles

Facade pattern reflects yet another design principle - a minimum knowledge of principles: only your close friends and talk. This principle tells us to reduce the interaction between objects, leaving only a few "close friends", that is in the design, do not let too much class coupled together.

This principle provides us with a number of principles: respect of any object in the object's method, we should only call methods belong to the following ranges:

  • The object itself
  • Object as a parameter passed in the method
  • This method any object created or instantiated
  • Any component of this object

It is worth mentioning that this principle while reducing the dependence between objects, reducing the maintenance cost of the software; but this principle will lead to more use of "packaging" class is created, and to deal with other components of communication this may lead to increased complexity and development time and reduce run-time performance. During development, we need to make a choice based on the actual situation, in the example in this article, it is clear that the use of the convenience of a minimum knowledge of the principles of software maintenance far outweigh the disadvantages.

Published 295 original articles · won praise 37 · views 30000 +

Guess you like

Origin blog.csdn.net/tianshan2010/article/details/104714003