主要组件
- 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象
- 具体构件(Concrete Component)角色:实现抽象构件,通过装饰角色为其添加一些职责。
- 抽象装饰(Decorator)角色:实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
- 具体装饰(ConcreteDecorator)角色:继承抽象装饰,实现抽象装饰的相关方法,并给具体构件对象添加附加的责任
结构示意图
简要概括
抽象装饰要实现抽象构件类、包含抽象构件的引用A,则后续的具体装饰也是抽象构件的实现类,所以具体装饰可以通过抽象构件类的引用A来声明使用,自由互相嵌套包裹
另外:
应用场景
- 1.切换功能
通过基础组件的构造函数来设置不同的装饰器类,以此来切换不同的装饰器方法,实现功能的切换.
如样例代码1,在具体装扮的基础上,切换不同的装扮 - 2.叠加功能
因为所有装饰器都继承于基础组件,则所有装饰器都是基础组件的子类,可以不断将前一个装饰器添加到该装饰器之中,如此,可以在执行本装饰器的功能之前,先执行前面装饰器的功能,实现功能上的叠加。
如样例代码2,在具体装扮的基础上,一层层往上叠加装扮,即
室内服装=冬天基础装扮+室内装扮
室外服装=冬天基础装扮+室内装扮+室外装扮
样例代码1 切换功能
/**
* @program: DesignPatterns
* @description:
* @author: TATE.LU
* @create: 2020-12-07 16:11
**/
public class Decorator {
public static void main(String []args){
Clothes clothes=new ConcreteClothes();
//这是具体的一种装扮,实现了Clothes接口规范,可以想怎么穿,就怎么穿
System.out.println("\n某种具体装扮----------");
clothes.putOn();
//这是冬天的穿衣打扮父类,实现了Clothes接口规范,定义冬天的衣服最基本要穿什么
WinterClothes winterClothes=new WinterClothes(clothes);
System.out.println("\n\n切换成冬天基础装扮----------");
WinterBaseClothes winterBaseClothes=new WinterBaseClothes(winterClothes);
winterBaseClothes.putOn();
System.out.println("\n\n切换成室内装扮-----------");
IndoorWinterClothes indoorWinterClothes=new IndoorWinterClothes(winterClothes);
indoorWinterClothes.putOn();
System.out.println("\n\n切换成出门装扮!---------");
OutdoorWinterClothes outdoorWinterClothes=new OutdoorWinterClothes(winterClothes);
outdoorWinterClothes.putOn();
}
}
interface Clothes{
public void putOn();
}
class ConcreteClothes implements Clothes{
public ConcreteClothes(){
// System.out.println("具体的装扮");
}
public void putOn() {
System.out.print("某种具体装扮");
}
}
class WinterClothes implements Clothes{
private Clothes clothes;
public WinterClothes(Clothes clothes){
this.clothes=clothes;
}
public void putOn() {
if(this.clothes!=null){
this.clothes.putOn();
}
}
}
class WinterBaseClothes extends WinterClothes{
public WinterBaseClothes(Clothes clothes){
super(clothes);
}
public void putOn(){
super.putOn();
baseDecorate();
}
// 基本装扮
public void baseDecorate(){
System.out.print("+ 保暖衣,保暖裤,鞋子");
}
}
class IndoorWinterClothes extends WinterClothes{
public IndoorWinterClothes(Clothes clothes){
super(clothes);
}
public void putOn(){
super.putOn();
indoorDecorate();
}
// 室内装扮
public void indoorDecorate(){
System.out.print("+ 长袖,长裤 ");
}
}
class OutdoorWinterClothes extends WinterClothes{
public OutdoorWinterClothes(Clothes clothes){
super(clothes);
}
public void putOn() {
super.putOn();
outdoorDecorate();
}
public void outdoorDecorate(){
System.out.print("+ 羽绒服,围巾,帽子 ");
}
}
运行结果
样例代码2 叠加功能
/**
* @program: DesignPatterns
* @description:
* @author: TATE.LU
* @create: 2020-12-07 16:11
**/
public class Decorator {
public static void main(String []args){
Clothes clothes=new ConcreteClothes();
//这是具体的一种装扮,实现了Clothes接口规范,可以想怎么穿,就怎么穿
System.out.println("\n某种具体装扮----------");
clothes.putOn();
//这是冬天的穿衣打扮父类,实现了Clothes接口规范,定义冬天的衣服最基本要穿什么
WinterClothes winterClothes=new WinterClothes(clothes);
//加上冬天基础装扮
System.out.println("\n\n加上冬天基础装扮----------");
WinterBaseClothes winterBaseClothes=new WinterBaseClothes(winterClothes);
winterBaseClothes.putOn();
//加上室内装扮
System.out.println("\n\n加上室内装扮-----------");
IndoorWinterClothes indoorWinterClothes=new IndoorWinterClothes(winterBaseClothes);
indoorWinterClothes.putOn();
//加上室外装扮
System.out.println("\n\n加上室外装扮---------");
OutdoorWinterClothes outdoorWinterClothes=new OutdoorWinterClothes(indoorWinterClothes);
outdoorWinterClothes.putOn();
}
}
interface Clothes{
public void putOn();
}
class ConcreteClothes implements Clothes{
public ConcreteClothes(){
// System.out.println("具体的装扮");
}
public void putOn() {
System.out.print("某种具体装扮");
}
}
class WinterClothes implements Clothes{
private Clothes clothes;
public WinterClothes(Clothes clothes){
this.clothes=clothes;
}
public void putOn() {
if(this.clothes!=null){
this.clothes.putOn();
}
}
}
class WinterBaseClothes extends WinterClothes{
public WinterBaseClothes(Clothes clothes){
super(clothes);
}
public void putOn(){
super.putOn();
baseDecorate();
}
// 基本装扮
public void baseDecorate(){
System.out.print("+ 保暖衣,保暖裤,鞋子");
}
}
class IndoorWinterClothes extends WinterClothes{
public IndoorWinterClothes(Clothes clothes){
super(clothes);
}
public void putOn(){
super.putOn();
indoorDecorate();
}
// 室内装扮
public void indoorDecorate(){
System.out.print("+ 长袖,长裤 ");
}
}
class OutdoorWinterClothes extends WinterClothes{
public OutdoorWinterClothes(Clothes clothes){
super(clothes);
}
public void putOn() {
super.putOn();
outdoorDecorate();
}
public void outdoorDecorate(){
System.out.print("+ 羽绒服,围巾,帽子 ");
}
}
运行结果