dimanche 1 avril 2018

Design pattern with decorator

enter image description here

public class DecoratorPatternDemo {
 public static void main(String[] args) {

  Shape circle = new Circle();

  Shape redCircle = new RedShapeDecorator(new Circle());

  Shape redRectangle = new RedShapeDecorator(new Rectangle());
  System.out.println("Circle with normal border");
  circle.draw();

  System.out.println("\nCircle of red border");
  redCircle.draw();

  System.out.println("\nRectangle of red border");
  redRectangle.draw();
 }
}

Any one can tell me what's the benefits of creating an instance of RedShapeDecorator with reference type Shape? ( i can understand the reason with Shape circle = new Circle() because this is polymorphism ) .But not with RedShapeDecorator. Is that the same if I do this
RedShapeDecorator redCircle = new RedShapeDecorator(new Circle()); instead of Shape redCircle = new RedShapeDecorator(new Circle()); .

Thank you .

Aucun commentaire:

Enregistrer un commentaire