设计模式之-java装饰模式

写这篇文章前,距离我发上一篇博客已经半年多了。这段时间,感慨之多.....再此自我孤独的哭个三秒中。下面开始的表演,设计模式之-java的装饰模式。废话不多说,先上代码吧,文字太多,我怕词穷。要深入了解代码的文字原理的,我也会具体的搞一部分。

先说下背景吧,我看到java的装饰模式前,我也是一年懵逼。让后我也不断的去度娘,还是有点懵逼,偶尔只记得这哥们是继承的一个替代者,大概这么回事啊。

先给各位看官介绍的几个角色吧,不要问我从哪里来的,不想打字太慢网上抄来的:

●  抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。

●  具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。

●  装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。

●  具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。

上才艺:

Component 抽象构件角色
/*
 *创建者:jfaith
 *创建时间:2020/10/15
 * 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
 */
public interface Component {
    public void sampleOperation();
}
ConcreteComponent 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类
public class ConcreteComponent implements Component {
    public void sampleOperation() {
        System.out.println("我是ConcreteComponent,想准备弄点事情!");
    }
    public void sampleOperation1() {
        System.out.println("我是ConcreteComponent,想准备弄点事情!");
    }

}
Decorator 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
public class Decorator implements Component{
    private Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void sampleOperation() {
        // 委派给构件
        component.sampleOperation();
    }
}

ConcreteDecoratorA 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。
public class ConcreteDecoratorA  extends Decorator{
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void sampleOperation() {
        super.sampleOperation();
        //相关业务逻辑
        System.out.println("我是ConcreteDecoratorA,这是我的业务逻辑");
    }
}
ConcreteDecoratorB 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。
public class ConcreteDecoratorB extends Decorator{
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void sampleOperation() {
        super.sampleOperation();
        //相关业务逻辑
        System.out.println("我是ConcreteDecoratorB,这是我的业务逻辑");
    }
}
ConcreteDecoratorC 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。
public class ConcreteDecoratorC extends Decorator{
    public ConcreteDecoratorC(Component component) {
        super(component);
    }

    @Override
    public void sampleOperation() {
        super.sampleOperation();
        //相关业务逻辑
        System.out.println("我是ConcreteDecoratorC,这是我的业务逻辑");
    }
}
MainTest 测试类
public class MainTest {
public static void main(String[] args) {
Component component = new ConcreteComponent();
ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(component);
ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(concreteDecoratorA);
ConcreteDecoratorC concreteDecoratorC = new ConcreteDecoratorC(concreteDecoratorB);
concreteDecoratorA.sampleOperation();
System.out.println("=====================================");
concreteDecoratorB.sampleOperation();
System.out.println("=====================================");
concreteDecoratorC.sampleOperation();
}
}

见证奇迹的地方来了,控制台输出:

我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorA,这是我的业务逻辑
=====================================
我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorA,这是我的业务逻辑
我是ConcreteDecoratorB,这是我的业务逻辑
=====================================
我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorA,这是我的业务逻辑
我是ConcreteDecoratorB,这是我的业务逻辑
我是ConcreteDecoratorC,这是我的业务逻辑

看出点啥了吗?啊,没看出,再表演一次

public class MainTest {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(component);
        ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(concreteDecoratorA);
        ConcreteDecoratorC concreteDecoratorC = new ConcreteDecoratorC(component);
        concreteDecoratorA.sampleOperation();
        System.out.println("=====================================");
        concreteDecoratorB.sampleOperation();
        System.out.println("=====================================");
        concreteDecoratorC.sampleOperation();
    }
}

控制台输出:

我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorA,这是我的业务逻辑
=====================================
我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorA,这是我的业务逻辑
我是ConcreteDecoratorB,这是我的业务逻辑
=====================================
我是ConcreteComponent,想准备弄点事情!
我是ConcreteDecoratorC,这是我的业务逻辑

还记得我上面说的吗,这哥们和继承有点像,知道了吧。对你没猜错,当你用继承不爽的时候,你随时可以搞一个装饰模式的东西出来替换掉继承。好了,记录完毕。今天项目上线,我他么还在写文章,赶紧去准备上线脚本吧,各位看官,我们下次再见。

原文地址:https://www.cnblogs.com/jfaith/p/13820773.html