package com.mybatis.designpatterns.decorator;
abstract class Component { // 通用的抽象方法类
public abstract void run();
public abstract void swing();
}
package com.mybatis.designpatterns.decorator;
abstract class Decorator extends Component{
protected Component component;
public void setComponent(Component component) {
this.component = component;
}
}
package com.mybatis.designpatterns.decorator;
public class CreateComponentA extends Decorator {
@Override
public void run() {
component.run();
System.out.println("A....再跑步");
}
@Override
public void swing() {
component.swing();
System.out.println("A....再游泳");
}
}
package com.mybatis.designpatterns.decorator;
public class CreateComponentB extends Decorator {
@Override
public void run() {
System.out.println("B....再跑步");
}
@Override
public void swing() {
System.out.println("B....再游泳");
}
}
package com.mybatis.designpatterns.decorator;
public class Main {
public static void main(String[] args) {
CreateComponentA createComponentA = new CreateComponentA();
CreateComponentB createComponentB = new CreateComponentB();
createComponentA.setComponent(createComponentB);
createComponentA.run();
createComponentA.swing();
}
}
console打印信息:
B....再跑步
A....再跑步
B....再游泳
A....再游泳
感受:还是要熟悉运用抽象多态的特点 ,暂时想不出业务中哪里可以用到这种设计模式