Concept · Core

Open / Closed (O)

Software should be OPEN for extension but CLOSED for modification — add new behaviour by adding code, not by editing code that already works.

Asked atAmazonMicrosoftStripe
step 1 / 8
1/2Editing for every new type
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 here
5}
State
shape kindsinside one method

AreaCalculator computes area with a type-switch INSIDE the method — it inspects what kind of shape it was handed.