Review and Redesign a Shape Controller
Review a shape hierarchy and HTTP controller, then redesign their responsibilities, invariants and persistence failure behavior.
For this exercise, assume a stored-shape service must keep two existing HTTP journeys: GET /shapes/:id/type returns the kind string, and POST /shapes/:id/translate moves the shape by the supplied offsets and returns committed x/y coordinates as JSON.
Review the following flawed Java-like practice fixture, adapted and condensed from a reported OOP and best-practice review. It is input to critique, not runnable framework code or an implementation request.
// Authored, condensed Java-like practice input adapted from the report.
// Framework/repository/request/response helpers are not supplied executable code.
class ShapesController {
private ObjectRepository<Shape> repo;
private DBConnection db;
public ShapesController() {
db = new DBConnection("root", "");
repo = new ObjectRepository<Shape>("shapes");
}
// GET /shapes/:id/type
public Response type(Request req) {
Shape s = repo.find(req.id);
switch (s.type) {
case 1: return new OkResponse("rectangle");
case 2: return new OkResponse("circle");
case 3: return new OkResponse("point");
default: return new BadRequestResponse();
}
}
// POST /shapes/:id/translate
public Response translate(Request req) {
Shape s = repo.find(req.id);
int dx = req.params.getInt("x-offset");
int dy = req.params.getInt("y-offset");
s.setX(s.getX() + dx);
s.setY(s.getY() + dy);
db.update("UPDATE Shapes SET x=" + s.getX() + ",y=" + s.getY()
+ " WHERE id=" + req.id);
String json = "{\"x\":" + s.getX() + ",\"y\":" + s.getY() + "}";
return new OkResponse(json, "application/json");
}
}
class Shape {
public int type = 0;
// Top-left position and dimensions of the rendering bounding box.
protected int x, y, width, height;
public Shape(int x, int y, int width, int height) {
this.x=x; this.y=y; this.width=width; this.height=height;
}
public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public void setX(int x) { this.x=x; }
public void setY(int y) { this.y=y; }
public void setWidth(int width) { this.width=width; }
public void setHeight(int height) { this.height=height; }
public double getArea() { throw new UnsupportedOperationException(); }
}
class Rectangle extends Shape {
public Rectangle(int x, int y, int width, int height) {
super(x,y,width,height); type=1;
}
public double getArea() { return width * height; }
}
class Circle extends Shape {
public Circle(int x, int y, int width, int height) {
super(x,y,width,height); type=2;
}
public void setWidth(int width) { this.width=width; this.height=width; }
public void setHeight(int height) { this.height=height; this.width=height; }
public double getArea() { return Math.PI * squareNum(width / 2); }
public double squareNum(double value) { return value * value; }
}
class Point extends Shape {
private int weight; // Controls rendered size, not geometric area.
public Point(int x, int y, int weight) { super(x,y,0,0); type=3; this.weight=weight; }
public double getArea() { return 0; }
public int getWeight() { return weight; }
}For this exercise, preserve rectangle/circle area and rendering bounding-box meaning, and Point zero geometric area with rendering weight kept separate. Rectangle and circle sizes are positive finite values; choose and state a coherent numeric/overflow and resize contract.
Malformed parameters and absent IDs need explicit unsuccessful outcomes. One request runs at a time.
Assume the persistence boundary atomically commits a coordinate change or definitely fails without durable change; a validation failure or definite storage failure must not expose a partially moved shape or return success. Ambiguous acknowledgement and concurrent moves are outside scope.
Produce a prioritized critique tied to concrete consequences, a revised object/responsibility model, focused contracts and ordered success/failure traces with test scenarios. Preserve the two external result forms while changing internal design as needed.
Rendering implementation, database deployment, authentication design, extra CRUD routes and an executable patch are outside scope. A bounded follow-up adds one new shape kind whose geometry is already defined. No named pattern is required.