AreaCalculator
+ area(s): double
Editing for every new type
1double area(Shape s){2 if (s is Circle) return PI*r*r;3 else if (s is Rect) return w*h;4 // add Triangle? EDIT here5}
State
shape kindsinside one method
Software should be OPEN for extension but CLOSED for modification — add new behaviour by adding code, not by editing code that already works.
1double area(Shape s){2 if (s is Circle) return PI*r*r;3 else if (s is Rect) return w*h;4 // add Triangle? EDIT here5}
AreaCalculator computes area with a type-switch INSIDE the method — it inspects what kind of shape it was handed.