package com.kang.patterndesign.decorator;
/**
* @author klr
* @create 2020-09-28-11:39
*/
public class DecoratorTest {
public static void main(String[] args) {
//对原有的第一代机器人进行装饰
new OtherDecorator(new RobotOne()).doMoreThing();
}
}
//机器人接口
interface Robot {
void doSomething();
}
//第一代机器人
class RobotOne implements Robot {
@Override
public void doSomething() {
System.out.println("唱歌");
}
}
//使用装饰器模式,在第一代机器人身上扩展
abstract class RobotDecorator implements Robot {
//组合
private Robot robot;
//使用构造器把第一代机器人传进来
public RobotDecorator(Robot robot) {
this.robot = robot;
}
@Override
public void doSomething() {
robot.doSomething();
}
public void doMoreThing() {
robot.doSomething();
System.out.println("跳舞");
}
}
class OtherDecorator extends RobotDecorator {
public OtherDecorator(Robot robot) {
super(robot);
}
}
网友评论