Wzorce projektowe – wzorzec Adapter

Teraz chcemy aby nasza aplikacja została rozbudowa o nowe figury geometryczne które zostały stworzone albo przez inny zespól w naszej organizacji albo są one dostępne w zewnętrznym Api. Poniżej implementacja dodatkowych figur geometrycznych z innym iterfejsem niż nasz: GeometricShape oraz z klasami trójkata i rombu. Interface GeometricShape pelni rolę – Adaptee czyli interfejsu który potrzebujemy przystosować do naszej aplikacji

 public interface GeometricShape {

      double area();
      double perimeter();
      void drawShape();
} 

 public class Triangle implements GeometricShape {

    private final double a;
    private final double b;
    private final double c;

    public Triangle() {
        this(1.0d, 1.0d, 1.0d);
    }

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double area() {
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }

    @Override
    public double perimeter() {
        return a + b + c;
    }

    @Override
    public void drawShape() {
    System.out.println("Drawing Triangle with area: " + area() + " and perimeter: " + perimeter());
    }

 public class Rhombus implements GeometricShape {

    private final double a;
    private final double b;

    public Rhombus() {
        this(1.0d, 1.0d);
    }

    public Rhombus(double a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public double area() {
        double s = a * b;
        return s;
    }

    @Override
    public double perimeter() {
        return 2 * (a + b);
    }

    @Override
    public void drawShape() {
    System.out.println("Drawing Rhombus with area: " + area() + " and perimeter: " + perimeter());
    }
public class GeometricShapeObjectAdapter implements Shape {

 private GeometricShape adaptee;

 public GeometricShapeObjectAdapter(GeometricShape adaptee) {
    super();
     this.adaptee = adaptee;
      }

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

@Override
public void resize() {
System.out.println(" can't be resized. Please create new one with required values.");
      }
}

Stworzenie nowej klasy wywoławczej wraz z wynikiem na konsoli:

 public class ObjectAdapterMain {

public static void main(String[] args) {
           
 System.out.println("Creating drawing of shapes...");

Drawing drawing = new Drawing();
drawing.addShape(new Rectangle());
drawing.addShape(new Circle());
drawing.addShape(new GeometricShapeObjectAdapter(new Triangle()));
drawing.addShape(new GeometricShapeObjectAdapter(new Rhombus()));

System.out.println("Drawing...");
drawing.draw();

System.out.println("Resizing...");
drawing.resize();
      }
} 
Creating drawing of shapes...
Drawing...
Drawing Rectangle
Drawing Circle
Drawing Triangle with area: 0.4330127018922193 and perimeter: 3.0
Drawing Rhombus with area: 1.0 and perimeter: 4.0

Resizing...
Resizing Rectangle
Resizing Circle

Triangle object can't be resized. Please create new one with required values.

Rhombus object can't be resized. Please create new one with required values. 

Teraz urzycie wzorca ale za pomocą dziedziczenia. W tym podejściu zostaną stworzone osobne adaptery dla klasy trójkąta i rombu.

 public class TriangleAdapter extends Triangle implements Shape {

      public TriangleAdapter() {
      super()
      }

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

      @Override
      public void resize() {
      System.out.println("Triangle can't be resized. Please create new one with required values.");
      }
 }

Klas Romb

 public class RhombusAdapter extends Rhombus implements Shape {

      public RhombusAdapter() {
      super();
      }

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

      @Override
      public void resize() {
      System.out.println("Rhombus can't be resized. Please create new one with required values.");
     }
} 

Klasa Main wraz z wynikiem na konsoli:

 public class ClassAdapterMain {

  public static void main(String[] args) {

        System.out.println("Creating drawing of shapes...");

        Drawing drawing = new Drawing();
        drawing.addShape(new Rectangle());
        drawing.addShape(new Circle());
        drawing.addShape(new TriangleAdapter());
        drawing.addShape(new RhombusAdapter());

        System.out.println("Drawing...");
        drawing.draw();

        System.out.println("Resizing...");
        drawing.resize();
    }
} 
 
Wynik:

Creating drawing of shapes...
Drawing...
Drawing Rectangle
Drawing Circle
Drawing Triangle with area: 0.4330127018922193 and perimeter: 3.0
Drawing Rhombus with area: 1.0 and perimeter: 4.0

Resizing...
Resizing Rectangle
Resizing Circle

Triangle can't be resized. Please create new one with required values.
Rhombus can't be resized. Please create new one with required values. 

Leave a Comment

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