Java装饰模式
目录
二、装饰模式中的角色
三、代码案例实现
一、装饰模式概念
装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任。换言之,客户端并不会觉得对象在装饰前和装饰后有什么不同。装饰模式可以在不使用创造更多子类的情况下,将对象的功能加以扩展。
二、装饰模式中的角色
三、代码案例实现
package com.lx.decorator;
public interface Component {
public void doThingA();
}
package com.lx.decorator;
public class ConcreteCompoment implements Component{
@Override
public void doThingA() {
System.out.println("do A Thing");
}
}
package com.lx.decorator;
public class Dercorator implements Component{
private Component component = null;
public Dercorator(Component component) {
this.component = component;
}
@Override
public void doThingA() {
System.out.println("do A thing");
}
}
package com.lx.decorator;
public class ConcreteDecoratorA extends Dercorator{
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void doThingA() {
super.doThingA();/*调用被包装类的方法*/
doThingB();
}
/*扩展功能*/
private void doThingB(){
System.out.println("do B thing");
}
}
package com.lx.decorator;
public class ConcreteDecoratorB extends Dercorator{
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void doThingA() {
super.doThingA();/*调用被包装类的方法*/
doThingC();
}
/*扩展功能*/
private void doThingC(){
System.out.println("do C thing");
}
}
杪&秋: 好的,主要是是我自己学习用的,谢谢提醒啊
SSS4362: 作者你好,你标题打错了,是SpringBoot吧,多打了个o啊
Marydon: 把美女交出来
皮卡!: 才学到这