Wzorce projektowe – wzorzec Adapter

Wzorzec Adapter można zaimplementować jako Two Ways Adapter aczkolwiek jest to rzadko stosowane.

 public enum ShapeType {

    CIRCLE, RECTANGLE, TRIANGLE,RHOMBUS
} 
 public class TwoWaysAdapter implements Shape, GeometricShape {

private ShapeType shapeType;

public TwoWaysAdapter() {
    this(ShapeType.TRIANGLE);
 }

 public TwoWaysAdapter(ShapeType shapeType) {
    super();
    this.shapeType = shapeType;
}

  @Override
   public void draw() {
   switch (shapeType) {
   case CIRCLE:
   new Circle().draw();
   break;
   case RECTANGLE:
   new Rectangle().draw();
   break;
   case TRIANGLE:
   new Triangle().drawShape();
   break;
   case RHOMBUS:
   new Rhombus().drawShape();
   break;
  }
}

 @Override
 public void resize() {
 switch (shapeType) {
 case CIRCLE:
 new Circle().resize();
 break;
 case RECTANGLE:
 new Rectangle().resize();
 break;
 case TRIANGLE:
 System.out.println("Triangle can't be resized. Please create new    one with required values.");
 break;
case RHOMBUS:
 System.out.println("Rhombus can't be resized. Please create new one with required values.");
 break;
 }
}

 @Override
 public double area() {
 switch (shapeType) {
 case CIRCLE:
 case RECTANGLE:
 return 0.0d;
 case TRIANGLE:
 return new Triangle().area();
 case RHOMBUS:
 return new Rhombus().area();
 }
  return 0.0d;
}

 @Override
 public double perimeter() {
 switch (shapeType) {
 case CIRCLE:
 case RECTANGLE:
 return 0.0d;
 case TRIANGLE:
 return new Triangle().perimeter();
 case RHOMBUS:
 return new Rhombus().perimeter();
 }
 return 0.0d;
}

  @Override
   public void drawShape() {
   draw();
   }
} 

Zródła: https://dzone.com/articles/adapter-design-pattern-in-java

Leave a Comment

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *