设计模式之装饰者模式

125 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  • 个人简介:微信公众号关注:SteveCode。为您分享更多的知识学术。生于忧患死于安乐
  • 专注Java技术干货分享,Java基础技术、数据结构、相关工具、Spring全家桶、intellij idea......

装饰者设计模式

需求

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

代码结构

在这里插入图片描述

详细代码 展示

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    AmericanoCoffee
 * Date:    2022-03-13 15:08
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:美式咖啡
 *
 * @author Songxianyang
 * @date 2022-03-13 15:08
 */
public class AmericanoCoffee extends Coffee{
    public AmericanoCoffee() {
        setName("美式咖啡");
        setPrice(2.0f);
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    ChinaCoffee
 * Date:    2022-03-13 15:12
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:中国咖啡
 *
 * @author Songxianyang
 * @date 2022-03-13 15:12
 */
public class ChinaCoffee extends Coffee{
    public ChinaCoffee() {
        setName("中国咖啡");
        setPrice(2.0f);
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    Coffee
 * Date:    2022-03-13 15:07
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-03-13 15:07
 */
public class Coffee extends Drink {
    
    @Override
    float count() {
        return getPrice();
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    DecoratorSeasone
 * Date:    2022-03-13 15:14
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述: 装饰者 调料
 *
 * @author Songxianyang
 * @date 2022-03-13 15:14
 */
public class DecoratorSeason extends Drink{
    private Drink drink;
    
    public DecoratorSeason(Drink drink) {
        this.drink = drink;
    }
    
    @Override
    float count() {
        // getPrice 当前调料的钱   drink.count() 上一次计算的钱
        return getPrice()+drink.count();
    }
    
    
    @Override
    public void setName(String name) {
        super.setName(name+"&&"+drink.getName());
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    Drink
 * Date:    2022-03-13 15:01
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述: 饮料类 抽象起来
 *
 * @author Songxianyang
 * @date 2022-03-13 15:01
 */

public abstract class Drink {
    private String name;
    
    private float price;
    
    /**
     * 计算价格
     * @return
     */
    abstract float count();
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public float getPrice() {
        return price;
    }
    
    public void setPrice(float price) {
        this.price = price;
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    Milk
 * Date:    2022-03-13 15:21
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:调料 牛奶
 *
 * @author Songxianyang
 * @date 2022-03-13 15:21
 */
public class Milk extends DecoratorSeason{
    public Milk(Drink drink) {
        super(drink);
        setName("调料 牛奶");
        setPrice(1.0f);
    }
    
    
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator.master
 * Type:    SoyBean
 * Date:    2022-03-13 15:23
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:豆浆
 *
 * @author Songxianyang
 * @date 2022-03-13 15:23
 */
public class SoyBean extends DecoratorSeason{
    public SoyBean(Drink drink) {
        super(drink);
        setName("豆浆");
        setPrice(2.0f);
    }
}

/*******************************************************************************
 * Package: com.example.demo.design.decorator
 * Type:    Test
 * Date:    2022-02-20 13:36
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.design.decorator.master;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-02-20 13:36
 */
public class Test {
    public static void main(String[] args) {
        //普通一杯 中国咖啡
        Drink order = new ChinaCoffee();
        System.out.println(order.getPrice());
        // 中国咖啡+牛奶
        order= new Milk(order);
        System.out.println("咖啡总价"+ order.count());
        System.out.println(order.getName());
        // 中国咖啡+豆浆
        order=new SoyBean(order);
    
        System.out.println("咖啡总价"+ order.count());
        System.out.println(order.getName());
        // 中国咖啡+豆浆+豆浆
        order=new SoyBean(order);
    
        System.out.println("咖啡总价"+ order.count());
        System.out.println(order.getName());
    
    }
}

测试截图

在这里插入图片描述 注意的点: 在装饰器类:DecoratorSeason 中两个的方法 。有说明

装饰器 挺绕的 面试经常问

IO 分析

在这里插入图片描述 在这里插入图片描述

/*******************************************************************************
 * Package: com.example.demo.jdk
 * Type:    Io
 * Date:    2022-03-15 22:25
 *
 * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.example.demo.jdk;

import java.io.DataInputStream;
import java.io.FileInputStream;

/**
 * 功能描述:
 *
 * @author Songxianyang
 * @date 2022-03-15 22:25
 */
public class Io {
    public static void main(String[] args) throws Exception {
        /**
         * FilterInputStream   装饰器
         *   protected volatile InputStream in; 饮料
         *DataInputStream 咖啡
         */
        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(""));
        System.out.println(dataInputStream.read());
        dataInputStream.close();
    }
}