Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. //An IShapeVisitor is a function over IShapes
  2. interface IShapeVisitor<R> extends IFunc<IShape, R> {
  3.   R visitCircle(Circle c);
  4.   R visitSquare(Square s);
  5.   R visitRect(Rect r);
  6. }
  7.  
  8. //ShapeArea is a function object over IShapes that computes their area
  9. class ShapeArea implements IShapeVisitor<Double> {
  10. // Everything from the IShapeVisitor interface:
  11.   public Double visitCircle(Circle c) { return Math.PI * c.radius * c.radius; }
  12.   public Double visitSquare(Square s) { return s.side * s.side; }
  13.   public Double visitRect(Rect r) { return r.w * r.h; }
  14.  
  15. // Everything from the IFunc interface:
  16. public Double apply(IShape s) { return s.accept(this); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement