-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-open-closed-principle.java
More file actions
122 lines (101 loc) · 3.42 KB
/
02-open-closed-principle.java
File metadata and controls
122 lines (101 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Open-Closed Principle (OCP)
// Classes should be open for extension but closed for modification
// This means you should be able to add new functionality without changing existing code
// BAD EXAMPLE - Violates Open-Closed Principle
class BadShapeCalculator {
public double calculateArea(Object shape) {
if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return rectangle.width * rectangle.height;
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return Math.PI * circle.radius * circle.radius;
}
// Adding new shapes requires modifying this method
return 0;
}
}
// GOOD EXAMPLE - Follows Open-Closed Principle
// Abstract base class that defines the contract
abstract class Shape {
public abstract double calculateArea();
//this class could be an interface as well
}
// Concrete implementations
class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
// New shape can be added without modifying existing code
class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
@Override
public double calculateArea() {
return 0.5 * base * height;
}
}
// Calculator class that works with any Shape
class GoodShapeCalculator {
public double calculateTotalArea(Shape[] shapes) {
double totalArea = 0;
for (Shape shape : shapes) {
totalArea += shape.calculateArea();
}
return totalArea;
}
}
// Demo class to show the principle in action
public class OpenClosedPrincipleDemo {
public static void main(String[] args) {
System.out.println("=== Open-Closed Principle Demo ===\n");
// Shape calculation example
System.out.println("Shape Area Calculations:");
Shape[] shapes = {
new Rectangle(5, 3),
new Circle(4),
new Triangle(6, 4)
};
GoodShapeCalculator calculator = new GoodShapeCalculator();
System.out.println("Total area: " + calculator.calculateTotalArea(shapes));
}
}
/*
Key Points of Open-Closed Principle:
1. OPEN FOR EXTENSION: You can add new functionality by creating new classes
- New shapes can be added by extending the Shape abstract class
2. CLOSED FOR MODIFICATION: Existing code doesn't need to be changed
- GoodShapeCalculator doesn't need modification when adding Triangle
3. Benefits:
- Reduces risk of breaking existing functionality
- Makes code more maintainable and flexible
- Enables polymorphism and runtime behavior selection
4. How to achieve OCP:
- Use abstract classes and interfaces
- Apply inheritance and polymorphism
- Design with extension points in mind
This principle helps create robust, extensible systems that can grow
without requiring changes to existing, tested code.
*/