Classes should be open for extension
and closed to modification
.
Modification
= changing the code of an existing class.Extension
= adding new functionality.We should be able to add new functionality without touching the existing code for the class.
Whenever we modify the existing code, we are taking the risk of creating potential bugs.
So we should avoid touching the tested and reliable production code if possible.
The InvoicePersistence
class that was extracted out for persistence logic has a method saveToDatabase
.
Let's say we want to add an alternate method of saveToFile
.
Adding saveToFile
to InvoicePersistence
would be the first instinct move.
However, this requires the InvoicePersistence
to add a method every time a new persistence method needs to be added.
InvoicePersistence
class should've defined a method save()
with a common interface for persistence logic.
Then, new classes DatabasePersistence
and FilePersistence
should implement InvoicePersistence
.
save()
method defined in the InvoicePersistence
class.save()
method in each of these classes will have different implementation, but will have common interface signature.This common interface signature becomes a contract for polymorphism.
public class DatabasePersistence implements InvoicePersistence { @Override public void save(Invoice invoice) { // Save to DB } } public class FilePersistence implements InvoicePersistence { @Override public void save(Invoice invoice) { // Save to file } }
When your app extends to have multiple persistence classes, you can create a PersistenceManager
class that manages all persistence classes.
public class PersistenceManager { InvoicePersistence invoicePersistence; BookPersistence bookPersistence; public PersistenceManager ( InvoicePersistence invoicePersistence, BookPersistence bookPersistence ) { this.invoicePersistence = invoicePersistence; this.bookPersistence = bookPersistence; } }
With this interface design, you can pass any class that implements the InvoicePersistence
interface to the PersistenceManager
class (polymorphism).