二十三种设计模式之外观模式

xiaoxiao2021-02-28  77

外观模式:提供一个统一的接口,来访问子系统中一个群功能相关的接口

外观模式定义了一个高层接口,让子系统更容易使用 实现对外功能的解耦

外观模式与命令模式的各自侧重点

外观模式:系统对外接口简化

命令模式:把命令封装成对象 实现命令调用的解耦

应用场景:家庭影院

public class DVDPlayer { public static DVDPlayer instance = null; private DVDPlayer(){} public static DVDPlayer getInstance(){ if(instance == null){ instance = new DVDPlayer(); } return instance; } public void on(){ System.out.println("DVDPlayer On"); } public void off(){ System.out.println("DVDPlayer off"); } } public class Popcorn { public static Popcorn instance = null; private Popcorn(){} public static Popcorn getInstance(){ if(instance == null){ instance = new Popcorn(); } return instance; } public void on(){ System.out.println("Popcorn On"); } public void off(){ System.out.println("Popcorn off"); } } public class Projector { public static Projector instance = null; private Projector(){} public static Projector getInstance(){ if(instance == null){ instance = new Projector(); } return instance; } public void on(){ System.out.println("Projector On"); } public void off(){ System.out.println("Projector off"); } } public class Screen { public static Screen instance = null; private Screen(){} public static Screen getInstance(){ if(instance == null){ instance = new Screen(); } return instance; } public void on(){ System.out.println("Screen On"); } public void off(){ System.out.println("Screen off"); } } public class Stereo { public static Stereo instance = null; private Stereo(){} public static Stereo getInstance(){ if(instance == null){ instance = new Stereo(); } return instance; } public void on(){ System.out.println("Stereo On"); } public void off(){ System.out.println("Stereo off"); } } public class ThreaterLights { public static ThreaterLights instance = null; private ThreaterLights(){} public static ThreaterLights getInstance(){ if(instance == null){ instance = new ThreaterLights(); } return instance; } public void on(){ System.out.println("ThreaterLights On"); } public void off(){ System.out.println("ThreaterLights Off"); } public void pop(){ System.out.println("ThreaterLights pop"); } } public class HomeThreaterFacade { private ThreaterLights mThreaterLights; private Popcorn mPopcorn; private Stereo mStereo; private Projector mProjector; private Screen mScreen; private DVDPlayer mDVDPlayer; public HomeThreaterFacade(){ mThreaterLights = ThreaterLights.getInstance(); mPopcorn = Popcorn.getInstance(); mStereo = Stereo.getInstance(); mProjector = Projector.getInstance(); mScreen = Screen.getInstance(); mDVDPlayer = DVDPlayer.getInstance(); } public void ready(){ mPopcorn.on(); mScreen.on(); mProjector.on(); mScreen.on(); mDVDPlayer.on(); } public void end(){ mPopcorn.off(); mScreen.off(); mProjector.off(); mScreen.off(); mDVDPlayer.off(); } } public class MainTest { public static void main(String[] args) { HomeThreaterFacade homeThreaterFacade= new HomeThreaterFacade(); homeThreaterFacade.ready(); homeThreaterFacade.end(); } }

转载请注明原文地址: https://www.6miu.com/read-42285.html

最新回复(0)