zoukankan      html  css  js  c++  java
  • 设计模式9---外观模式

    外观模式其实是平时一直在使用的,只是很少有人意思到而已。

    举例:作为投资者,你有很多选择,可以是股票,房产,黄金,股票也有很多,具体选哪个,抛哪个等等问题。

    其实作为普通投资者,根本没有必要的知识和心里素质。

    而这时候,你只要选择一个基金,由基金经理代替你来管理这些投资组合。

    而这就是一种设计模式:

    client端需要某些功能,这些功能在featureA和featureB中实现,而这时候,有facade类来帮助

    Client来操作,也就是说,client端根本不需要知道featureA和featureB的存在。

    下面以一个常见的操作硬件的engine来做demo。

    class VideoPlayer {
        
        public void init()
        {
            System.out.println("VideoPlayer prepare init done");
        }
        
        public void openVideo()
        {
            System.out.println("VideoPlayer open");
        }
        
        public void closedVideo()
        {
            System.out.println("VideoPlayer clsoed");
        }
    }
    private class Flashlight {
            
            boolean bStatus = false;
            
            public void init()
            {
                bStatus = false;
                System.out.println("flashlight prepare init done");
            }
            
            public boolean openflashlight()
            {
                if(!bStatus)
                {
                    System.out.println("flashlight open");
                    bStatus = true;
                }
                return true;
            }
            
            public void closeflashlight()
            {
                if(bStatus)
                {
                    System.out.println("flashlight close");
                    bStatus = false;
                }
            }
        }

    以上就是操作flashlight以及videoplay的操作,很简单,实际情况可能是,多个设备在调用这些设备,

    而且这些操作也许需要异步来完成。

    public class Engine {
        
        private Flashlight _mFlashlight = null;
        private VideoPlayer _mVideoPlayer = null;
        
        public Engine()
        {
            _mFlashlight = new Flashlight();
            _mFlashlight.init();
            
            _mVideoPlayer = new VideoPlayer();
            _mVideoPlayer.init();
        }
        
        public void openFlashlight()
        {
            _mFlashlight.openflashlight();
        }
        
        public void closeFlashlight()
        {
            _mFlashlight.closeflashlight();
        }
        
        public void openVideo()
        {
            _mVideoPlayer.openVideo();
        }
        
        public void closeVideo()
        {
            _mVideoPlayer.closedVideo();
        }
        
        
        private class Flashlight {
            
            boolean bStatus = false;
            
            public void init()
            {
                bStatus = false;
                System.out.println("flashlight prepare init done");
            }
            
            public boolean openflashlight()
            {
                if(!bStatus)
                {
                    System.out.println("flashlight open");
                    bStatus = true;
                }
                return true;
            }
            
            public void closeflashlight()
            {
                if(bStatus)
                {
                    System.out.println("flashlight close");
                    bStatus = false;
                }
            }
        }
    }

    这些我们可以交给Engine来管理,实际应用中,应该确保engine是调用Flashlight 和videoplayer的唯一入口。

    这样engine类才能做到统一管理。而Flashlight 和videoplayer对于UI层应该是透明的。

    package com.jayfulmath.designpattern.facade;
    
    import com.jayfulmath.designpattern.main.BasicExample;
    
    /* 外观模式经常用到,比如我们经常会把对硬件的操作(camera,flashlight)等等
     * 的操作都会封装在engine类里面,已确保UI层,只需要和engine类通信,而不需要知道
     * 具体硬件的操作。
     * 
     * */
    public class FacadeMain extends BasicExample {
    
        @Override
        public void startDemo() {
            // TODO Auto-generated method stub
            Engine mEngine = new Engine();
            mEngine.openFlashlight();
            mEngine.closeFlashlight();
            mEngine.openVideo();
            mEngine.closeVideo();
        }
    
    }

    对UI层来说,我只需要通过engine来帮我开关硬件设备,而无需关心细节。

    也就是对于普通投资者来说,我只需关心基金的盈利情况,而无需关心具体投资组合!

    经典的三层构架模式,就是外观模式的一种体现,当然三层模式耦合性还是太高,需要通过接口以及其他模式做进一步的分解。

  • 相关阅读:
    Proteus仿真与实际的差别
    用 Proteus学习51单片机之1602液晶
    用Proteus学习51单片机之中断
    用Proteus学习51单片机之键盘
    用Proteus学习51单片机之数码管
    PHPnow 安装服务 [ Apache_pn ] 失败的解决方法 for[windows7/vista]
    chrome 不能打印背景图的解决方法
    JS 无块级作用域
    IE6下zindex犯癫不起作用bug的初步研究
    ie6下的js调试工具companion.js
  • 原文地址:https://www.cnblogs.com/deman/p/4134630.html
Copyright © 2011-2022 走看看