JAVA Decorator装饰器模式

装饰器模式

装饰器模式可动态的将新的责任功能附加到原来类的对象上,其扩展性比继承更具有弹性。
在某些场景下,父类可能会派生出数量众多的子类,为防止“类爆炸”。可采用装饰器模式。

特点

  1. 装饰类采用抽象类,使其可后续派生出多种具体装饰类。
  2. 通过聚合,持有被装饰类的对象,即包装了被装饰类的对象。
  3. 装饰类通过继承被包装类的基类(或实现其接口),保证了其 行为方法与被包装类一致。

构建过程

  1. 从包装类ConcreteConponent中抽象出共同基类(抽象类Component)。
  2. 装饰器Decorator,继承抽象类Component,且内部持有一个Component类引用。
  3. 具体装饰类ConcreteDecorator从装饰器Decorator中派生,实现具体装饰方法。

使用:ConcreteConponent类实例 作为参数 传入 具体装饰类ConcreteDecorator中,既通过ConcreteDecorator获得扩展能力。
在这里插入图片描述

使用场景

  1. 重构数量众多的子类,解决“类爆炸”问题。
  2. 类扩展,

DEMO

一个简单的例子。通过DecoratedHuman装饰器装饰Human。
在这里插入图片描述

主方法:

//装饰器DEMO
//HUMAN:被包装类的基类
//BOY GIRL,被包装类
//DecoratedHuman:装饰器抽象类
//CoolBoy、BeatifulGirl:具体装饰类
public class Decorator_v1 {
    public static void main(String[] args) {
        Girl plainGile = new Girl("Lucy");
        BeatifulGirl beauty = new BeatifulGirl(plainGile);
        beauty.Cry();
        beauty.Smile();
        beauty.Speak();


        Boy plainboy = new Boy("Tom");
        CoolBoy coolBoy = new CoolBoy(plainboy);
        coolBoy.Cry();
        coolBoy.Smile();
        coolBoy.Speak();
    }
}

被包装类的基类Human类:

//被包装类的抽象
public abstract class Human {
    String name;

    void Smile()
    {}

    void Cry()
    {}

    void Speak()
    {}
}

被包装类,Gril、Boy:

//被包装类
public class Girl extends Human{

    public Girl(String name)
    {
        this.name = name;
    }

    @Override
    void Smile() {
        System.out.println("I am " + name + " ,I am smiling !");
    }

    @Override
    void Cry() {
        System.out.println("I am " + name + " ,I am crying !");
    }

    @Override
    void Speak() {
        System.out.println("I am " + name + " ,I am speaking !");
    }
}
//被包装类
public class Boy extends Human{

    public Boy(String name)
    {
        this.name = name;
    }

    @Override
    void Smile() {
        System.out.println("I am " + name + " ,I am smiling !");
    }

    @Override
    void Cry() {
        System.out.println("I am " + name + " ,I am crying !");
    }

    @Override
    void Speak() {
        System.out.println("I am " + name + " ,I am speaking !");
    }
}

装饰器类DecoratedHuman继承Human:

public class DecoratedHuman extends Human{

    protected Human human;

    public DecoratedHuman(Human human)
    {
        this.human = human;
    }

    @Override
    void Smile() {
        human.Smile();
    }

    @Override
    void Cry() {
        human.Cry();
    }

    @Override
    void Speak() {
        human.Speak();
    }
}

具体装饰器类BeatifulGirl、CoolBoy:

//具体装饰类
public class BeatifulGirl extends DecoratedHuman{

    public BeatifulGirl(Human human) {
        super(human);
    }

    //在原类基础上增加了System.out.println("I am a Beauty !");
    @Override
    void Smile() {
        System.out.println("I am a Beauty !");
        human.Smile();
    }

    @Override
    void Cry() {
        System.out.println("I am a Beauty !");
        human.Cry();
    }

    @Override
    void Speak() {
        System.out.println("I am a Beauty !");
        human.Speak();
    }
}
//具体装饰类
public class CoolBoy extends DecoratedHuman{

    public CoolBoy(Human human) {
        super(human);
    }

    //在原类基础上增加了System.out.println("I am a CoolBoy !");
    @Override
    void Smile() {
        System.out.println("I am a CoolBoy !");
        human.Smile();
    }

    @Override
    void Cry() {
        System.out.println("I am a CoolBoy !");
        human.Cry();
    }

    @Override
    void Speak() {
        System.out.println("I am a CoolBoy !");
        human.Speak();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
装饰器模式Decorator Pattern)是一种结构型设计模式,它允许你动态地将行为添加到对象中,而无需使用继承。装饰器模式是一种包装模式,也就是说,它通过创建一个包装类来包装真正的对象。 在 Java 中,装饰器模式通常通过以下步骤实现: 1. 定义一个抽象的组件类,它是被装饰对象和装饰器对象的共同接口。 ```java public interface Component { void operation(); } ``` 2. 定义具体的组件类,它是被装饰对象的实现。 ```java public class ConcreteComponent implements Component { @Override public void operation() { System.out.println("具体组件的操作"); } } ``` 3. 定义抽象装饰器类,它继承自组件类,并包含一个组件对象的引用。 ```java public abstract class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { component.operation(); } } ``` 4. 定义具体的装饰器类,它继承自抽象装饰器类,实现了装饰器所具有的功能。 ```java public class ConcreteDecorator extends Decorator { public ConcreteDecorator(Component component) { super(component); } @Override public void operation() { super.operation(); addedBehavior(); } private void addedBehavior() { System.out.println("具体装饰器的操作"); } } ``` 5. 在客户端代码中使用装饰器来包装被装饰对象。 ```java Component component = new ConcreteComponent(); Component decorator = new ConcreteDecorator(component); decorator.operation(); ``` 以上就是 Java装饰器模式的实现步骤。通过这种方式,我们可以在运行时动态地添加新的行为,而不必对原有的类进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值