What does it mean for a class to be open for extension and closed for modification, and how do you add behaviour without editing existing code?
It means that you should be able to add a new kind of thing by writing a new class, and without editing the classes that already work. Open for extension and closed for modification.
public class Main {
public static void main(String[] args) {
Order order = new Order();
order.add(new Line("Book", 20.0));
System.out.println(order.total());
}
}
And here is the code that follows it. The order holds a discount and it never asks what kind of discount it is.
public interface Discount {
double applyTo(double amount);
}
public class Order {
private final java.util.List<Line> lines = new java.util.ArrayList<>();
private final Discount discount;
public Order(Discount discount) {
this.discount = discount;
}
public void add(Line line) {
lines.add(line);
}
public double total() {
double sum = 0;
for (Line line : lines) {
sum = sum + line.price();
}
return discount.applyTo(sum);
}
}
And now a new kind of discount is one new class. The order does not change at all.
public class BuyOneGetOneOff implements Discount {
@Override
public double applyTo(double amount) {
return amount * 0.5;
}
}
And the version that breaks the principle, so you can see the difference.
public class Order {
private final java.util.List<Line> lines = new java.util.ArrayList<>();
// Every new kind of discount means editing this class.
public double total(boolean usePercent, double percent,
boolean useBuyOneGetOne, boolean useLoyalty) {
double sum = 0;
for (Line line : lines) {
sum = sum + line.price();
}
if (usePercent) {
sum = sum * (1 - percent / 100);
}
if (useBuyOneGetOne) {
sum = sum * 0.5;
}
if (useLoyalty) {
sum = sum - 10;
}
return sum;
}
}
And the version before the principle was broken is the one with a switch on the type of the discount, and it is worth showing because it is the common middle step.
public class Order {
public double total(String discountKind) {
double sum = 0;
for (Line line : lines) {
sum = sum + line.price();
}
if (discountKind.equals("percent")) {
sum = sum * 0.9;
} else if (discountKind.equals("buyone")) {
sum = sum * 0.5;
}
return sum;
}
}
And there are three things that are worth knowing about. The first one is that the switch is not as bad as the flags, because the method does not change shape. But every new kind still means an edit here, and the method grows with every new kind.
The second one is that the open closed principle is not a law that says never edit old code. It says that when you add a new kind of thing you should not have to. A bug fix in the existing code is fine.
The third one is that the principle is about the code that depends on the behaviour, not about the behaviour itself. So adding a new method to the Order is fine, as long as it does not force a change in the classes that use the Order.
the change does it break the principle
----------------------------- ----------------------------------
fix a bug in the order no, this is fine
add a method to the order no, as long as nobody has to change
add a new kind of discount no, it is a new class
add a flag to the total yes, the order has to be edited
So the rule to remember is this. A new kind of thing is a new class, and the old classes do not change.
The last question in this topic is about how to add a behaviour without changing the old code.