private void setRedBorder ( Shape decoratedShape ) {
System . out . println ( " Border Color: Red " ) ;
}
}
步骤 5
使用 RedShapeDecorator 来装饰 Shape 对象。
DecoratorPatternDemo.java
public class DecoratorPatternDemo {
public static void main ( String [ ] args ) {
Shape circle = new Circle ( ) ;
ShapeDecorator redCircle = new RedShapeDecorator ( new Circle ( ) ) ;
ShapeDecorator redRectangle = new RedShapeDecorator ( new Rectangle ( ) ) ;
// Shape redCircle = new RedShapeDecorator(new Circle());
// Shape redRectangle = new RedShapeDecorator(new Rectangle());
System . out . println ( " Circle with normal border " ) ;
circle . draw ( ) ;
System . out . println ( " Circle of red border " ) ;
redCircle . draw ( ) ;
System . out . println ( " Rectangle of red border " ) ;
redRectangle . draw ( ) ;
}
}