动态的给一个对象添加一些额外的职责.
/**
* shape 接口
* @author fan
*
*/
public interface Shape {
public void draw();
}
/**
* circle 类
* @author fan
*
*/
public class Circle implements Shape {
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("circle");
}
}
/**
* rectandle 类
* @author fan
*
*/
public class RectAngle implements Shape{
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("rectangle");
}
}
/**
* shape的 抽象装饰类
* @author fan
*
*/
public abstract class ShapeDecorate implements Shape {
private Shape decorateshape;
public ShapeDecorate(Shape decorateshape) {
this.decorateshape = decorateshape;
}
public void draw() {
// TODO Auto-generated method stub
this.decorateshape.draw();
}
}
/**
* test
* @author fan
*
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shape circle = new ReadShapeDecorate(new Circle());
circle.draw();
Shape rectangle = new ReadShapeDecorate(new RectAngle());
rectangle.draw();
}
}
//res
circle
color is erd
rectangle
color is erd