说到装饰者模式那么何为装饰者模式呢?
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流就是典型的装饰者模式,缺点容易产生众多的装饰者类