Question: Does this code violate the OCP principle? Why? If it is violated, rewrite the code in a way to comply with the OCP Principle. Question1:

Does this code violate the OCP principle? Why?

If it is violated, rewrite the code in a way to comply with the OCP Principle.


Question1: Consider the code public class Shape { 1 Shape Type type;

 

Question1: Consider the code public class Shape { 1 Shape Type type; public class Square extends Shape $ } public Square() { this.type-ShapeType.SQUARE; } double side; public class Circle extends Shape 1 } public Circle() { this.type = ShapeType.CIRCLE; } double radius; Point center; public class Server 1 void drawSquare(Square square) 1 System.out.println("I'm drawing a square"); void drawCircle(Circle circle) System.out.println("I'm drawing a circle"); void drawAllShapes(List shapes) for (Shape shape: shapes) switch (shape.type) case SQUARE: draw Square((Square) shape); break; case CIRCLE: drawCircle((Circle) shape); break; 1. Does this code violate the OCP principle? Why? 2. If it is violated, rewrite the code in a way to comply with the OCP Principle.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Network Questions!