SOLID: The Liskov Substitution Principle (LSP)
After a week of holidays, here I am again, back to coding and learning. I really needed a rest but, on the other hand, after a couple of days, I was missing my computer to bits.
¡Already the third letter of SOLID! Let's get into it.
This complex to understand principle can be described as follows:
"If s is a subtype of t, then we should be able to substitute objects of type t by objects of type s without altering the properties of the program."In other words, it is possible to substitute a given object with an object of a sub-class and expect it to react the same way and fulfill its contract.
But as usual, it is easier to understand if we have a look at a code snippet. Imagine our app currently sends all of our Notifications through Plain Text Email through SMTP. Our code would look something like this:
<?php $emailMessage = new EmailMessage('Laura', 'New Blog Post', 'Hi Laura, ...'); $smtp = new SMTPTransport ('smtp.example.com', 465);
$notifier = new Notifier(); $notifier->setTransport($smtp); $notifier->send($emailMessage);
What happens if, in the future, we also send Emails containing HTML instead of plain text? We would only need to add subclasses and pass them to the Notifier (caller), as this is completely independent from EmailMessage and SMTPTransport classes. So, substituting the parent class EmailMessage by a child class HTMLEmailMessage extending EmailMessage.
Again, it all comes down to set a hierarchy where we make a contract between the child and the parent. If we fulfill LSP, we will be able to extend the parent or replace it any of it's children without affecting the functionality as seen in the previous snippet.
Summarizing the SOLID principles reviewed till now, we could say:
- Single-Responsibility Principle: allows us developing small classes with precisely delimited responsibilities
- Liskov Substitution Principle: it allows us to use polymorphism in our code. Which means, if we write a function which takes a given argument, polymorphism allows us using any subtype of that argument without affecting the functionality of said method.
- Open-Closed Principle: the two previous principles are the key for this one, which allows us writing small classes with a solid hierarchy throughout our whole app.
As usual, understanding each of the principles is not the most difficult part, but applying it. In my case, I try not to get crazy trying to use them in my code straight away. I've been developing an app, and at least for the MVP, I have learnt that the most important thing is making it work and then applying some make-up to make you feel proud of it and make it look professional. And, last of all, if you manage, apply some of the things you learn on the way. But if you don't, I do encourage writing your own examples and testing them. Make them small and start abstracting the most simple concepts. And if this fails, read, read read. Read blogs, read manuals (though these sometimes leave you even more confussed), read forums, and find as many examples and snippets as you can, as one of them will make you finally understand the concept.
Happy week and happy coding everyone!
Comments
Post a Comment