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

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,适配器模式使原本由于接口不兼容而不能一起工作的那些类可以一起工作。

    系统的数据和行为都正确,单接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况.

    何时使用适配器模式:使用一个已经存在的类,但如果它的接口,也就是它的方法和你的要求不相同时,就应该考虑用适配器模式

    #需要适配的类
    class Adaptee
    {

    public function SpecificRequest(){

    echo '特殊请求';
    }


    }
    class Target
    {

    public function Request(){

    echo '普通请求';

    }

    }

    class Adapter extends Target
    {
    private $adaptee;

    public function __construct()
    {
    $this->adaptee = new Adaptee();
    }

    public function Request()
    {

    $this->adaptee->SpecificRequest();

    }

    }

    $adapter = new Adapter();

    $adapter->Request();




  • 相关阅读:
    JSP文件上传下载组件(2)
    JSP文件上传下载组件(1)
    FetchProfile类的作用
    html(二) -- 文本标签和实体字符
    html (一)-- 概述
    单元测试框架--junit
    内省和BeanUtils
    工厂模式
    反射
    观察者设计模式
  • 原文地址:https://www.cnblogs.com/paulversion/p/8435626.html
Copyright © 2011-2022 走看看