Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public static void main(String[] args) {
// Old objects
Line line = new Line();
Rectangle rectangle = new Rectangle();
Circle circle = new Circle();

// Adapt them with adapters
List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new LineAdapter(line));
shapes.add(new RectangleAdapter(rectangle));
shapes.add(new CircleAdapter(circle));

// Code can now operate using common interface
int x1 = 7;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ca.uwaterloo.cs446.designpatterns.adapter;

public class CircleAdapter implements Shape {
private Circle adaptee;

public CircleAdapter(Circle circle) {
this.adaptee = circle;
}

@Override
public void draw(int x1, int y1, int x2, int y2) {
// Center is the difference between the two points
int centerX = (Math.max(x1, x2) - Math.min(x1, x2)) / 2;
int centerY = (Math.max(y1, y2) - Math.min(y1, y2)) / 2;

// radius is the minimum distance between x and y
int radius = Math.min(Math.abs(x1 - x2), Math.abs(y1 - y2));
adaptee.draw(centerX, centerY, radius);
}
}