美文网首页
设计模式(二结构型模式)

设计模式(二结构型模式)

作者: X1_blog | 来源:发表于2020-04-30 17:14 被阅读0次

适配器模式

目的

增加兼容性, 调和两个接口; 属于接口再封装

类型

类适配器 / 对象适配器

优点

  1. 接口变动时, 改适配器就可以继续工作, 灵活性增强
  2. 不影响源类本身的工作
  3. 适配器模式下实现了php多继承

使用场景

  1. 稳定接口需要修改功能时, 为了避免违背开闭原则, 应当使用适配器模式进行改动; 只要确保适配器测试正常就不用担心影响源接口功能

DEMO

/*类适配器模式*/
interface Target{
    function method_1();
}


class Adaptee 
{
    function method_2()
    {
        echo "method_2";
    }
}
 

class Adapter extends Adaptee implements Target
{
    function method_1()
    {
        echo "method_1";
    }
}


class client
{
    
    static function test()
    {
        $adapter = new Adapter();
        $adapter -> method_1();
        $adapter -> method_2();
    }
}

client::test();
/*对象适配器*/
interface Target{
    function method_1();
}

class Adaptee 
{
    function method_2()
    {
        echo "method_2";
    }
}
 
class Adapter implements Target
{
    private $adaptee ;
    public function __construct(Adaptee $adaptee)
    {
        $this->adaptee = $adaptee;
    }
    function method_1()
    {
        echo "method_1";
    }

    function method_2()
    {
        $this->adaptee -> method_2();
    }
}


class client
{
    static function test()
    {
        $adaptee = new Adaptee();
        $adapter = new Adapter($adaptee);
        $adapter -> method_1();
        $adapter -> method_2();
    }
}

client::test();

装饰器模式

组成

装饰模式包含如下角色:

  • Component: 抽象构件
  • ConcreteComponent: 具体构件
  • Decorator: 抽象装饰类
  • ConcreteDecorator: 具体装饰类

目的

创建装饰类对被装饰类的属性/方法进行运算/调整, 且不影响被装饰类的结构(属性/方法)

实现方法

将被装饰类的实例作为装饰类的参数传入, 在装饰类上添加新的方法

/*装饰器模式*/
class Component
{
    public $userList = ["mary","tom"];
    function show()
    {
        print_r($this->userList);
    }
}


class Decorator 
{
    private $component ;
    function __construct(Component $component){
        $this->component = $component ;
    }
    function uppercase()
    {
        foreach ($this->component->userList as $key => $value) {
            $this->component->userList[$key] = strtoupper($value);
        }
    }
}

$component = new Component();
$decorator = new Decorator($component);
$decorator-> uppercase();
$component-> show();

代理模式

定义

创建代理类, 拥有被代理类(真实类)的部分/全部同名方法, 客户端通过调用代理类使用真实类的方法

使用场景

  1. 隐藏真实类
  2. 创建多个代理类对不同调用者进行权限管理
  3. 在代理类中实现功能的简单扩展(不提倡)

DEMO

/*代理模式*/
interface shopping{
    function buy();
}
/*代理类*/
class proxyShopping implements shopping
{
    private $realShopping ;
    function buy()
    {
        $this->say();
        if(!$this->realShopping)$this->realShopping = new realShopping();
        $this->realShopping->buy();
    }
    function say(){
        echo "proxy start\n";
    }
}

/*真实类*/
class realShopping implements shopping
{
    
    function buy()
    {
        echo "buy something...";
    }
}

class client
{
    /*客户端只知道代理类存在, 从而保护了真实类*/
    static function main()
    {
        $shopping = new proxyShopping();
        $shopping->buy();
    }
}
client::main();

相关文章

  • 第1章 设计模式概述

    一、设计模式的概念 二、设计模式的历史 三、设计模式的要素 四、设计模式的分类 ■ 创建型设计模式 ■ 结构型设计...

  • 23种设计模式总结一

    23 种经典设计模式共分为 3 种类型,分别是创建型、结构型和行为型。 一、创建型设计模式 创建型设计模式包括:单...

  • 设计模式简单总结(待完善)

    设计模式简单总结 设计模式可以分为:创建型,结构型,行为型三种模式。 1 创建型模式 1.1 单例模式 用来指定某...

  • android常用设计模式

    26种设计模式 创建型设计模式[5] 单例模式,工厂模式,抽象工厂模式,建造模式,原型模式,[简单工厂模式] 结构...

  • 23种设计模式总结二

    23 种经典设计模式共分为 3 种类型,分别是创建型、结构型和行为型。 结构型设计模式 结构型模式就是一些类或对象...

  • 设计模式的分类

    一、创建型设计模式 指导我们解决创建对象的方面的需求和问题。 二、结构型设计模式 指导我们设计程序结构,也就是如何...

  • Nodejs实现23种设计模式-1.简单工厂模式

    Nodejs实现24种设计模式--简单工厂模式 导语:24种设计模式,分为三大类,创建型、结构型和行为型。这些模式...

  • 项目开发-------iOS设计模式

    iOS的设计模式大体可以分为以下几种设计模式 1.创建型:单例设计模式、抽象工厂设计模式 2.结构型:MVC 模式...

  • 状态模式,命令模式和策略模式的区别

    设计模式中有三个模式State, Command, Strategy,这三种设计模式都是行为型设计模式,在结构上又...

  • 设计模式归纳

    一、设计模式的分类 23 种经典的设计模式分为三类:创建型、结构型、行为型。 1.创建型设计模式主要解决“对象的创...

网友评论

      本文标题:设计模式(二结构型模式)

      本文链接:https://www.haomeiwen.com/subject/tnttghtx.html