java设计模式之装饰者模式

说到装饰者模式那么何为装饰者模式呢?
      the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.

翻译:装饰器模式是一种设计模式,它允许动态地将行为添加到单个对象,而不会影响来自同一类的其他对象的行为

通俗的讲就是在不改变原有代码的基础上添加新的功能

优点:扩展性好、改动方便、维护方便、有助于遵守单一职责原则

比如说一部手机,最早只能打电话发短信,突然随着时间的推移出现了三星手机、苹果手机、华为等众多手机品牌,开始附加新的功能:照相机功能、邮件功能、健康功能等

1、首先创建一个装饰者基类手机Mobile
 

package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is all mobile parent Object
 * @author: W.HL
 * @create: 2019-03-29 14:44
 **/

public abstract class Mobile
{
    /**
     * desc something
     */
    public abstract String getDesc();

}

2、创建具体手机品牌

  • 三星手机
package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is Samung mobile demo
 * @author: W.HL
 * @create: 2019-03-29 14:48
 **/

public class SamsungMobile extends Mobile
{
    /**
     * desc something
     */
    @Override
    public String getDesc()
    {
        return "This is Samsung S10 mobile";
    }
}
  • 苹果手机
package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is apple mobile demo
 * @author: W.HL
 * @create: 2019-03-29 14:50
 **/

public class AppleMobile extends Mobile
{

    /**
     * desc something
     */
    @Override
    public String getDesc()
    {
        return "This is Iphone XR mobile";
    }
}

3、创建在原有手机中添加功能装饰者基类

package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is all mobile features parent object
 * @author: W.HL
 * @create: 2019-03-29 14:51
 **/

public abstract class FeatureService extends Mobile
{
    /**
     * desc something
     */
    @Override
    public abstract String getDesc();
}

4、创建具体功能类

  • 照相机功能
package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is camera feature in mobile
 * @author: W.HL
 * @create: 2019-03-29 14:58
 **/

public class CameraFeature extends FeatureService
{
    private Mobile mobile;

    public CameraFeature(Mobile mobile)
    {
        this.mobile = mobile;
    }
    /**
     * desc something
     */
    @Override
    public String getDesc()
    {
        return mobile.getDesc()+"\n新增相机功能";
    }
}
  • 邮件功能
package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is camera feature in mobile
 * @author: W.HL
 * @create: 2019-03-29 14:58
 **/

public class MailFeature extends FeatureService
{
    private Mobile mobile;

    public MailFeature(Mobile mobile)
    {
        this.mobile = mobile;
    }
    /**
     * desc something
     */
    @Override
    public String getDesc()
    {
        return mobile.getDesc()+"\n新增邮件功能";
    }
}
  • 健康功能
package com.weather.spring.cloud.initializrstart.design.mode.decorate;

/**
 * @program: msa-weather-master
 * @description: This is camera feature in mobile
 * @author: W.HL
 * @create: 2019-03-29 14:58
 **/

public class HealthFeature extends FeatureService
{
    private Mobile mobile;

    public HealthFeature(Mobile mobile)
    {
        this.mobile = mobile;
    }
    /**
     * desc something
     */
    @Override
    public String getDesc()
    {
        return mobile.getDesc()+"\n新增健康功能";
    }
}

5、编写测试类进行测试

package com.weather.spring.cloud.initializrstart.design.mode.decorate;

import org.junit.Test;


public class DecorateTest
{
    @Test
    public void decorateTest(){
        Mobile samsung = new SamsungMobile();

        Mobile iphone = new AppleMobile();

        /*增加相机功能*/
        FeatureService featureService =  new CameraFeature(samsung);

        featureService = new MailFeature(featureService);
        System.out.println(featureService.getDesc());
        System.out.println("******************************************");

        featureService = new MailFeature(iphone);
        featureService =  new CameraFeature(featureService);
        featureService =  new HealthFeature(featureService);
        System.out.println(featureService.getDesc());

    };
}

5、预猜想结果

  • 三星手机有相机功能、发邮件功能
  • 苹果手机有发邮件功能、相机功能、健康功能

6、验证结果

java源码的I/O流就是典型的装饰者模式,缺点容易产生众多的装饰者类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值