【设计模式】通过在苹果官网购买iPhone配件了解装饰器模式

开发 前端
装饰器主要是通过组合的模式,对类内容进行扩展,而不是通过继承的方式,特别是在Java只能单继承的情况,行之有效。

背景

一个iPhone,可以套上保护壳(ConcDecorator A),也可以再套上无线充(ConcDecorator B),得到最后的效果(execute)。

苹果无需发布新的型号(继承),我们简单地通过装饰(组合)就可以得到想要的最终产品。

模式定义

Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.

为动态保持相同接口的对象附加额外的职责。装饰器为扩展功能提供了一种灵活的选择,而不是子类化。

可以看到,是十分符合我们的需求的,每一个装饰品如保护壳、无线充,就是一个个可以嵌套的装饰器。

https://refactoringguru.cn/design-patterns/decorator

模式实现

1.定义iPhone接口

主要定义了iPhone模板的说明接口方法getDesc,后续以这个方法为核心,展开嵌套。

package com.example.designpattern.decorator;

/**
 * 手机
 *
 * @author hongcunlin
 */
public interface Phone {
    /**
     * 介绍
     *
     * @return 介绍内容
     */
    String getDesc();
}

2.实现iPhone接口

简单试下getDesc的方法,目前是一个裸机的iPhone。

package com.example.designpattern.decorator.impl;

import com.example.designpattern.decorator.Phone;

/**
 * 苹果手机iPhone 14 Pro Max
 *
 * @author hongcunlin
 */
public class Iphone implements Phone {
    @Override
    public String getDesc() {
        return "iPhone";
    }
}

3.定义iPhone装饰器抽象类

装饰器的超类,里边存放iPhone属性,围绕它进行嵌套。

package com.example.designpattern.decorator.impl.decorator;

import com.example.designpattern.decorator.Phone;

/**
 * 手机装饰器
 *
 * @author hongcunlin
 */
public abstract class IphoneDecorator implements Phone {
    /**
     * 手机(抽象类存在的意义,否则就使用接口了)
     */
    protected Phone phone;

    /**
     * 构造方法
     *
     * @param phone 手机
     */
    IphoneDecorator(Phone phone) {
        this.phone = phone;
    }
}

4.1.实现iPhone装饰器之保护壳

嵌套一

package com.example.designpattern.decorator.impl.decorator;

import com.example.designpattern.decorator.Phone;

/**
 * iPhone保护壳
 *
 * @author hongcunlin
 */
public class IphoneCaseDecorator extends IphoneDecorator {
    /**
     * 构造方法
     *
     * @param phone 手机
     */
    public IphoneCaseDecorator(Phone phone) {
        super(phone);
    }

    @Override
    public String getDesc() {
        return "带保护壳的" + phone.getDesc();
    }
}

4.2.实现iPhone装饰器之无线充

嵌套二

package com.example.designpattern.decorator.impl.decorator;

import com.example.designpattern.decorator.Phone;

/**
 * iPhone无线充
 *
 * @author hongcunlin
 */
public class IphoneChargerDecorator extends IphoneDecorator {
    /**
     * 构造方法
     *
     * @param phone 手机
     */
    public IphoneChargerDecorator(Phone phone) {
        super(phone);
    }

    @Override
    public String getDesc() {
        return "带无线充的" + phone.getDesc();
    }
}

5.测试

这里我们先初始化一个iPhone,接着带上保护壳看看效果,最后再带上无线充看看效果。

package com.example.designpattern.decorator;

import com.example.designpattern.decorator.impl.Iphone;
import com.example.designpattern.decorator.impl.decorator.IphoneCaseDecorator;
import com.example.designpattern.decorator.impl.decorator.IphoneChargerDecorator;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class DesignPatternTest {
    @Test
    public void test() {
        // 常规iPhone
        Phone iphone = new Iphone();
        System.out.println(iphone.getDesc());

        // 带保护壳的iPhone
        iphone = new IphoneCaseDecorator(iphone);
        System.out.println(iphone.getDesc());

        // 带无线充的带保护壳的iPhone
        iphone = new IphoneChargerDecorator(iphone);
        System.out.println(iphone.getDesc());
    }
}

可以看到,随着装饰器的层层嵌套,iPhone的模样越来越复杂,是符合我们的预期的。

最后

装饰器主要是通过组合的模式,对类内容进行扩展,而不是通过继承的方式,特别是在Java只能单继承的情况,行之有效。

后面有空接着闲聊23种设计模式中的其他种。

责任编辑:武晓燕 来源: 今日头条
相关推荐

2023-09-04 13:14:00

装饰器设计模式

2023-12-13 13:28:16

装饰器模式Python设计模式

2022-01-19 08:21:12

设计装饰器模式

2023-10-07 00:17:06

AirDrop中介者模式

2024-02-23 12:11:53

装饰器模式对象

2020-12-01 07:16:05

重学设计模式

2023-10-07 00:14:53

2022-03-25 11:01:28

Golang装饰模式Go 语言

2023-09-26 00:27:07

设计模式链接

2020-08-28 10:11:33

苹果配件手机

2024-04-10 12:27:43

Python设计模式开发

2011-06-02 18:02:50

iPhone MVC

2023-05-15 08:51:46

解释器模式定义

2022-09-14 08:16:48

装饰器模式对象

2021-06-03 09:18:25

装饰器模式包装

2020-11-09 08:20:33

解释器模式

2023-01-09 08:43:53

Go设计模式

2009-11-18 13:20:07

PHP设计模式

2012-04-24 23:40:17

苹果

2011-12-30 21:28:24

iPad
点赞
收藏

51CTO技术栈公众号