1 装饰器模式
2 实现
interface Shape{
public void draw();
}
class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("rectangle");
}
}
class Circle implements Shape{
@Override
public void draw() {
System.out.println("circle");
}
}
abstract class ShapeDecorator implements Shape{
private Shape shape;
public ShapeDecorator(Shape shape){
this.shape = shape;
}
@Override
public void draw() {
shape.draw();
}
}
class RedShapeDecorator extends ShapeDecorator{
public RedShapeDecorator(Shape shape){
super(shape);
}
@Override
public void draw() {
super.draw();
border();
}
private void border(){
System.out.println("red border");
}
}
public class Test{
public static void main(String[] args){
RedShapeDecorator redShapeDecorator = new RedShapeDecorator(new Rectangle());
redShapeDecorator.draw();
}
}