当前位置:首页 >探索 >【设计模式】通过在苹果官网购买iPhone配件了解装饰器模式 官网购买 背景一个iPhone

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

2024-06-29 09:21:45 [百科] 来源:避面尹邢网

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

作者:IT小村 开发 前端 装饰器主要是设计饰器通过组合的模式,对类内容进行扩展,模式模式而不是通过通过继承的方式,特别是苹果配件在Java只能单继承的情况,行之有效。官网购买

背景

一个iPhone,解装可以套上保护壳(ConcDecorator A),设计饰器也可以再套上无线充(ConcDecorator B),模式模式得到最后的通过效果(execute)。

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

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

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

模式定义

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

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

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

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

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;@SpringBootTestpublic 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种设计模式中的其他种。

责任编辑:武晓燕 来源: 今日头条 装饰器组合模式

(责任编辑:知识)

    推荐文章
    热点阅读