Grokking SOLID Design Principles
Vote
0% completed
Real World Analogies and Code Example
In the previous lesson we defined the Open/Closed Principle: software should be open for extension and closed for modification. Now we apply it to a piece of code.
The Starting Point
Here is an Invoice class that prints an invoice and applies a discount.
There is nothing wrong with this class yet. It handles the one case the business has. The Open/Closed Principle says nothing about code that never changes.
The problem appears when a second kind of invoice arrives.
The Change That Exposes the Problem
Sales now needs international invoices
.....
.....
.....
Like the course? Get enrolled and start learning!
Keshav Garg
· 2 years ago
public class Invoice { private double amount; public Invoice(double amount) { this.amount = amount; } public double getAmount() { return amount; } // This method handles generating basic invoices public void generateInvoice() { System.out.println("Generating basic invoice for amount: " + amount); } // This method handles applying discounts on the invoice public void applyDiscount() { System.out.println("Applying discount on invoice: " + amount); } } public class InternationalInvoice extends Invoice { @Override public void generateInvoice() { System.out.println("Generating International invoice for amount: " + amount); } }
Show 3 replies
Reading Progress
0%