zoukankan      html  css  js  c++  java
  • 设计模式 适配器模式

    考虑下面的情景

    一个系统有很多模块组成,系统提供的安全的校验,每个模块也有自己的安全校验。普通开发人员只关注本模块的安全校验,但是系统的安全校验又 由于模块的,这种情景就很适合适配器模式。

    对于系统安全校验,开发人员提供如下的接口及实现

    package 适配器;
    
    //系统模块安全监察
    public interface IsystemSecurity {
    
        public boolean check();
    }
    package 适配器;
    
    public class SystemSecurity implements IsystemSecurity {
        public boolean check() {
            return true;
        }
    }

    模块安全校验提供如下的接口

    package 适配器;
    
    public interface ImoduleSecurity {
        public boolean check();
    }

    这个接口如何实现呢

    下面的是使用适配器模式的实现

    package 适配器;
    
    public class ModuleSecurity implements ImoduleSecurity {
        private IsystemSecurity systeSecurity;
    
        public ModuleSecurity(IsystemSecurity systemSecurity) {
            this.systeSecurity = systemSecurity;
        }
    
        public boolean check() {
            if (!systeSecurity.check())
                return false;
    
            return true;
        }
    }

    六.使用场景及使用感受
    希望复用一些现存的类,但是接口又与复用环境要求不一致。
    其实适配器模式有点无奈之举,在前期设计的时候,我们就不应该考虑适配器模式,而应该考虑通过重构统一接口。
    七.适配器模式与装饰者模式
    它们都可以用来包装对象,本质区别在于
    适配器模式:将一个接口转换成另外一个接口。
    装饰者模式:不改变接口,只加入职责。  

  • 相关阅读:
    test
    设置section的距离
    UISearchBar的应用
    Centos6.6 系统优化
    Linux系统之间文件传输 scp 命令
    MySQL 数据库安装
    AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件
    Ubuntu 16.04 Chrome浏览器安装flash player插件
    Centos6.6 yum源更新
    qume-kvm 命令管理
  • 原文地址:https://www.cnblogs.com/ilahsa/p/2886772.html
Copyright © 2011-2022 走看看