• JAVA设计模式学习笔记-外观模式


    一个家庭影院项目

    组建一个家庭影院:
    DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机

    传统的控制接口设计

    直接用遥控器:统筹各设备开关
    开爆米花机
    放下屏幕
    开投影仪
    开音响
    开DVD,选dvd
    去拿爆米花
    调暗灯光
    播放
    观影结束后,关闭各种设备

    外观模式的原理和设计

    外观模式:提供一个统一的接口,来访问子系统中一群功能相关接口
    外观模式定义了一个高层接口,让子系统更容易使用

    一个设备

    package com.java.test.facademode.hometheater;
    
    public class DVDPlayer {
    
    
    
    	private 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 void play() {
    		System.out.println("DVDPlayer is playing");
    	}
    	
    	public void pause() {
    		System.out.println("DVDPlayer  pause");
    	}
    	
    	public void setdvd() {
    		System.out.println("DVDPlayer  is setting dvd");
    	}
    }
    

    另一种设备

    package com.java.test.facademode.hometheater;
    
    public class Popcorn {
    
    
    	private 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 void pop() {
    		System.out.println("Popcorn is popping");
    	}
    	
    	
    	
    }
    

    集中控制

    package com.java.test.facademode.hometheater;
    
    public class HomeTheaterFacade {
    	private TheaterLights mTheaterLights;
    	private Popcorn mPopcorn;
    	private Stereo mStereo;
    	private Projector mProjector;
    	private Screen mScreen;
    	private DVDPlayer mDVDPlayer;
    
    	public HomeTheaterFacade() {
    		mTheaterLights = TheaterLights.getInstance();
    		mPopcorn = Popcorn.getInstance();
    		mStereo = Stereo.getInstance();
    		mProjector = Projector.getInstance();
    		mScreen = Screen.getInstance();
    		mDVDPlayer = DVDPlayer.getInstance();
    	}
    
    	public void ready() {
    		mPopcorn.on();
    		mPopcorn.pop();
    		mScreen.down();
    		mProjector.on();
    		mStereo.on();
    		mDVDPlayer.on();
    		mDVDPlayer.setdvd();
    		mTheaterLights.dim(10);
    	}
    
    	public void end() {
    		mPopcorn.off();
    		mTheaterLights.bright();
    		mScreen.up();
    		mProjector.off();
    		mStereo.off();
    		
    		mDVDPlayer.setdvd();
    		mDVDPlayer.off();
    		
    	}
    
    	public void play() {
    		mDVDPlayer.play();
    	}
    
    	public void pause() {
    		mDVDPlayer.pause();
    	}
    }
    

    测试类

    package com.java.test.facademode;
    
    import com.java.test.facademode.hometheater.HomeTheaterFacade;
    
    
    
    public class MainTest {
    	public static void main(String[] args) {
    		HomeTheaterFacade mHomeTheaterFacade=new HomeTheaterFacade();
    		
    		mHomeTheaterFacade.ready();
    		mHomeTheaterFacade.play();
    	}
    }
    

    最少知识原则—最少知识原则的意义
    最少知识原则:尽量减少对象之间的交互,只留几个“密友”
    项目设计中就是不要让太多的类耦合在一起

    如何遵循最少知识原则
    对象的方法调用范围:
    该对象本身
    作为参数传进来的对象
    此方法创建和实例化的对象
    对象的组件

  • 相关阅读:
    解读MSP430F169的头文件们
    解决编辑安装falkon中碰到缺少ecm问题
    vim基础普及
    本科2020专业目录
    cp 文件复制命令
    mkdir rmdir
    whereis ,which 文件查找
    gitlab软件的安装与使用
    如何节省数据库的成本
    innodb_undo_tablespaces参数引发的启动报错
  • 原文地址:https://www.cnblogs.com/kristse/p/facade.html
Copyright © 2020-2023  润新知