"Head First Design Patterns" study notes - skin mode

Skin mode, changing the interface, designed to simplify the interface, the complexity of all of one or several classes are hidden behind, only to reveal a clean, bright look.

Case

We watch movies in their own home theater, according to the most troublesome way, then we need to follow these steps:

  1. Open popcorn machine
  2. Start popcorn
  3. Dim the lights
  4. Down screen
  5. Turn on the projector
  6. The input to the projector is switched DVD
  7. The projector is provided wide mode
  8. Open amplifier
  9. The attack put the input is set to DVD
  10. The amplifier is set to surround sound
  11. The amplifier volume to the
  12. Open DVD Player
  13. Start playing a DVD

These steps are done, we started watching a movie, after watching the film, we want everything shut down, can not put these steps reverse operation again, so it is convenient mode processing in conjunction with the appearance of more.

We look at the specific code.

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, TheaterLights lights, Screen screen, PopcornPopper popper) {
        this.amp = amp;
        this.tuner = tuner;
        this.dvd = dvd;
        this.cd = cd;
        this.projector = projector;
        this.lights = lights;
        this.screen = screen;
        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();
        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();
    }
}

This is our home theater appearance code. It contains many subsystem components packaged in combination in this class. We should now integrate these components into a unified interface, write two methods, one watchMovie (String movie) a endMovie (), a movie needs to be done is to look at the integration of operations, it is the end of a movie player integration need.

Now we look at the component code is very simple.

public class Amplifier {
    public void on() {
        System.out.println("Amplifier on");
    }

    public void setDvd(DvdPlayer dvd) {
        System.out.println("Set dvd");
    }

    public void setSurroundSound() {
        System.out.println("Surround sound");
    }

    public void off() {
        System.out.println("Amplifier off");
    }
}

public class CdPlayer {}

public class DvdPlayer {
    public void on() {
        System.out.println("Dvd on");
    }

    public void play(String movie) {
        System.out.println("Play " + movie);
    }

    public void stop() {
        System.out.println("Dvd stop");
    }

    public void eject() {
        System.out.println("Dvd eject");
    }

    public void off() {
        System.out.println("Dvd off");
    }
}

public class PopcornPopper {
    public void on() {
        System.out.println("Popcorn popper on");
    }

    public void pop() {
        System.out.println("Making popcorn");
    }

    public void off() {
        System.out.println("Popcorn popper off");
    }
}

public class Projector {
    public void on() {
        System.out.println("Projector on");
    }

    public void wideScreenMode() {
        System.out.println("Wide screen mode");
    }

    public void off() {
        System.out.println("Projector off");
    }
}

public class Screen {
    public void down() {
        System.out.println("Screen down");
    }

    public void up() {
        System.out.println("Screen up");
    }
}

public class TheaterLights {
    public void dim(int i) {
        System.out.println("Light dim " + i);
    }

    public void on() {
        System.out.println("Light on");
    }
}

public class Tuner {}

Is a collection of the entire system needs to sub-assemblies, they provide some way for the home theater classes to use. When we use, first create an instance of these components, and then declare a constructor home theater class instance, then we only need to call two methods can be declared just watch movies and stop the movie.

public class HomeTheaterTestDrive {
    public static void main(String[] args) {
        Amplifier amplifier = new Amplifier();
        Tuner tuner = new Tuner();
        DvdPlayer dvdPlayer = new DvdPlayer();
        CdPlayer cdPlayer = new CdPlayer();
        Projector projector = new Projector();
        TheaterLights theaterLights = new TheaterLights();
        Screen screen = new Screen();
        PopcornPopper popcornPopper = new PopcornPopper();

        HomeTheaterFacade homeTheater = new HomeTheaterFacade(amplifier, tuner, dvdPlayer, cdPlayer, projector, theaterLights, screen, popcornPopper);

        homeTheater.watchMovie("电影");
        System.out.println();
        homeTheater.endMovie();
    }
}

Test class, we declare above a lot of components, as we mentioned above, we used the last time, just call the two methods, then all our work is done, when we'll see the movie, only need to put in to see the movie, when you finish, only need to perform endMovie () method.

Appearance pattern definition

Facade pattern provides a unified interface, the interface used to access a group subsystem. The appearance of a high-level interface that allows the subsystem easier to use.

This is readily appreciated, is to provide a simple interface that makes the subsystem easier to use, is similar to a remote control, when we press a remote control switch, a series of subsystem operation, easy to operate our client, the client simply press this button, it's just a simple analogy.

Also involves a new design principle: "minimum knowledge" principle, only your close friends and talk.

When you are designing a system, whether it is any object, you should pay attention to it and what kind of interaction, and pay attention to it and how the interaction of these classes. Constantly remind us, do not let too much like the design of the system are coupled together to avoid indeed affect the whole body, to partially modify the system, it will affect other parts. Reduce maintenance costs of the system.

So we in the method of this object, just call the method within the following ranges:

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

Here, the aspect mode is over, it is very simple.

Published 26 original articles · won praise 2 · Views 2325

Guess you like

Origin blog.csdn.net/qq_42909545/article/details/104975581