Last winter I had the chance to attend a talk by Robert.C.Martin(aka Uncle Bob) in Dublin. I think it was awesome. Uncle Bob is the author of Clean Code(I've got it signed by him in person :p) and also is a very influential person in the software industry.
I've been thinking to write something about some of the contributions he did to the industry and post it here on my blog since that talk.
One of the last things he said to us, was to read as much as we could...
Before starting to write this post I thought about those words for a while...
I think that it was a great advice.
Nobody negates that real experience is probably best, but reading books is also very important.
They carry the experiences of persons who were there before and also the rules and the principles those persons discovered and documented on their way.
Don't get me wrong, following rules and principles strictly does not always guarantee success("the world keeps changing") but understanding them can be of great help when facing great challenges.
We don't want all the above mentioned requirement changes to affect the class. If we don't seek for modularity at an early stage of development, we will very easily end up with difficult to maintain software.
What we need to do is think just on the one unique goal that the class will have.
Also we can think in what the class definitely will not do, so we can distiguish the other reasons to change that should not be there. Follow this way of thinking when fixing a violation of the SRP and it will help you detect the classes that you need to extract:
Principle #2
Open Close Principle
In the above code, if a new requirement arrives saying to make some other type of meal different than veg and non veg, the class will need to be modified. The given above example is not maintainability friendly.
Let's see how to use polymorphism to remove that conditional logic and improve the solution:
The OCP principle is very powerful but we also must have in mind that by adding levels of abstraction(as alternative you can also think about Composition versus inheritance), we also increase the complexity and it is very important to understand that this principle should be applied only in those places where there is more likely to be often requirement changes.
Also a wild duck can quack and swim.
But what about Duck toys?
As you see, some duck toys require batteries and also they don't really quack, they just play a sound. Even if there are no compilation errors and it looks tempting to include duck toy in this inheritance chain, this is clear violation of Liskov's substitution principle.
The reason is that if a client instantiates the base class, the derived class DuckToy, is not capable of replace it because the functionality is being affected.
One solution in this case could be to have a separate class by its own, to represent the duck toy.
Principle #4
Interface segregation principle
That was horrible uh? So there are many ways you can avoid this.
One example could be to combine specific interfaces as per needed:
Degresion: While writing this example I just remembered a post that I wrote time ago about the use of Marker interfaces, that is also another interesting way of getting flexibility using interfaces.
Principle #5
Dependency inversion principle
This principle says "Don't depend on anything concrete, depend only on things that are abstract." So make sure that all of your dependencies point at things that are abstract.
This will bring safety to your code and also make it flexible.
Probably you are thinking that this principle, can be a bit radical; but obiously, in real following it always strictly is just very difficult(maybe even impossible).
A tip that you can use to verify that you are following this principle when you call a function, is to program to the interface and not the realization.
One great example of this principle in practice is the Template design pattern. Lets have a look first at a common violation of the principle:
The above example is a badly coded class that makes 2 types of pizzas... There are many bad things in this piece of code, but I will just focus on the violation of the principle we are discussing.
Since every call done to the pizza object is to a concrete method, what we get is something very rigid and inflexible.
Every pizza has some ingredients that are mandatory, such as the base, tomato, cheese and oregano,but the rest are optional, so: why do we care about making calls to concrete methods, to set those extra ingredients, if is not even our concern?
In the following snippet of code, a template method is introduced to abstract the optional part and let sub-classes implement them.
The S.O.L.I.D principles, were identified by Robert C.Martin, but the Acronym was created by Michael Feathers in the year 2000, today they are well known in the world of object oriented sofwtare and many there is plenty literature on books and internet about them.
Just for the end of this post I would like to share with you a great podcast interview that I found on the web, were Uncle Bob, explains S.O.L.I.D in detail.
I've been thinking to write something about some of the contributions he did to the industry and post it here on my blog since that talk.
One of the last things he said to us, was to read as much as we could...
Before starting to write this post I thought about those words for a while...
I think that it was a great advice.
Nobody negates that real experience is probably best, but reading books is also very important.
They carry the experiences of persons who were there before and also the rules and the principles those persons discovered and documented on their way.
Don't get me wrong, following rules and principles strictly does not always guarantee success("the world keeps changing") but understanding them can be of great help when facing great challenges.
So finally I decided to write this post about one of the biggest contributions of Uncle Bob to the world of object oriented programming, and that is the S.O.L.I.D principles.
Principle #1
Single Responsibility Principle
If we have a class that has multiple responsibilities/features/reasons to change; Modifications done to it carry the risk of affecting other parts of the class(other responsibilities/features/reasons to change).
In other words: Classes that do more than one thing are difficult to maintain.
SRP says: "A class should have only one reason to change".
Lets have a look at an example:
As you can see, it is a clear example of a class with multiple reasons to change:public class BMI { //Stuff... public void calculate(int heightInCm,int weightInGrms) { //Implementation... } public void saveResults() { //Implementation... } public TrainingPlan getTrainingPlan() { //Implementation... } }
- Future business requirements might involve us changing the calculate method(e.g different metrics)
- Future business requirements might involve us changing the way the results are saved.
- Future business requirements might involve us changing the way the training plan is created based in the BMI.
We don't want all the above mentioned requirement changes to affect the class. If we don't seek for modularity at an early stage of development, we will very easily end up with difficult to maintain software.
What we need to do is think just on the one unique goal that the class will have.
Also we can think in what the class definitely will not do, so we can distiguish the other reasons to change that should not be there. Follow this way of thinking when fixing a violation of the SRP and it will help you detect the classes that you need to extract:
public class BMICalculator { //Stuff... public void calculate(int heightInCm,int weightInGrms) { //Implementation... } //Other methods that support the main goal of the class... } public class BMIToStorage { //Stuff... public void save(BMIResults results) { //Implementation... } //Other methods that support the main goal of the class... } public class TrainingPlanCreator { //Stuff... public TrainingPlan getTrainingPlan(BMIResults results) { //Implementation... } //Other methods that support the main goal of the class... }
Principle #2
Open Close Principle
The motivation behind the Open Close principle is to extend/change behaviour without modifying the existing code. This principle says:
"modules should be open for extension but close for modification".
You probably think, that this sounds very contradictory, but in many OO programming languages like Java, there are mechanisms that will allow you to do this.
One of those mechanisms is polymorphism. By defining abstract functions/methods
Let's have a look first at a violation of the open closed principle:
public class Chef { public void prepareMeal(Meal meal) { if(meal.type.equals("veg")) { prepareVeg(); } else if(meal.type.equals("nonVeg")){ prepareNonVeg(); } } private void prepareVeg() { //Implementation... } private void prepareNonVeg() { //Implementation... } } public class Meal { String type; } public class NonVeg extends Meal { public NonVeg() { type = "nonVeg"; } } public class Veg extends Meal { public Veg() { type = "veg"; } }
In the above code, if a new requirement arrives saying to make some other type of meal different than veg and non veg, the class will need to be modified. The given above example is not maintainability friendly.
Let's see how to use polymorphism to remove that conditional logic and improve the solution:
As you can see the solution is more flexible, now it is easier to maintain and also we got rid of an evil flag that at long term will cause only problems when manipulating it. The class Meal that contains sensitive methods is open for extension but closed for modification.public class Chef { public void prepareMeal(Meal meal) { meal.cook(); } } public abstract class Meal { public abstract void cook(); } public class NonVeg extends Meal { public void cook() { //... } } public class Veg extends Meal { public void cook() { //... } }
The OCP principle is very powerful but we also must have in mind that by adding levels of abstraction(as alternative you can also think about Composition versus inheritance), we also increase the complexity and it is very important to understand that this principle should be applied only in those places where there is more likely to be often requirement changes.
Principle #3
Liskov substitution principle
This principle is concern about sub-classes classes replacing the behavior of their base class.
If this occurs, the new classes can produce undesired effects when they are used/called in other parts of the program.
Liskov's Substitution Principle states that if a client is calling a base class, then the reference to the base class should be able to be replaced with a derived class without affecting the functionality of base class.
Lets have a look at an example of violation of this principle:
public class Duck { public void quack(){ //.. }; public void swim(){ //.. }; }
Also a wild duck can quack and swim.
But what about Duck toys?
public class DuckToy extends Duck { private boolean batteriesIncluded; public DuckToy(boolean batteriesIncluded) { //... } public void swim() { //...(This logic depends on the batteries) } public void playSound() { //... } }
As you see, some duck toys require batteries and also they don't really quack, they just play a sound. Even if there are no compilation errors and it looks tempting to include duck toy in this inheritance chain, this is clear violation of Liskov's substitution principle.
The reason is that if a client instantiates the base class, the derived class DuckToy, is not capable of replace it because the functionality is being affected.
public class Pond {
public static void main(String[] args) {
Duck wildDuck = new WildDuck();
Duck duckToy = new DuckToy(true);
wildDuck.quack();
//This should not quack
duckToy.quack();
//Compilation error, real ducks don't play sounds or use batteries
//duckToy.playSound();
}
}
One solution in this case could be to have a separate class by its own, to represent the duck toy.
public class DuckToy {
private boolean batteriesIncluded;
public DuckToy(boolean batteriesIncluded) {
//...
}
public void swim() {
//...(This logic depends on the batteries)
}
public void quack() {
//Duck Toys don't quack
}
public void playSound() {
//...
}
}
Principle #4
Interface segregation principle
This is a very simple to understand principle, it says that clients should not be forced to implement interfaces they don't use Just that simple. Have a look at a violation of this principle:
public interface Animal() { public void fly(); public void run(); public void swim(); } public class Dog implements Animal { public void fly() { //This is empty because dogs can't fly } public void run() { //Implementation for running } public void swim() { //Implementation for swimming } }
That was horrible uh? So there are many ways you can avoid this.
One example could be to combine specific interfaces as per needed:
public interface Runner() {
public void run();
}
public interface Swimmer() {
public void swim();
}
public interface Flyer() {
public void fly();
}
public class Dog implements Swimmer,Runner{
public void swim(){}
public void run(){}
}
public class Seagull implements Swimmer,Flyer{
public void swim(){}
public void fly(){}
}
Degresion: While writing this example I just remembered a post that I wrote time ago about the use of Marker interfaces, that is also another interesting way of getting flexibility using interfaces.
Principle #5
Dependency inversion principle
This principle says "Don't depend on anything concrete, depend only on things that are abstract." So make sure that all of your dependencies point at things that are abstract.
This will bring safety to your code and also make it flexible.
Probably you are thinking that this principle, can be a bit radical; but obiously, in real following it always strictly is just very difficult(maybe even impossible).
A tip that you can use to verify that you are following this principle when you call a function, is to program to the interface and not the realization.
One great example of this principle in practice is the Template design pattern. Lets have a look first at a common violation of the principle:
public class PizzaMaker {
public Pizza makeMeatPizza() {
Pizza pizza = new Pizza();
pizza.setBase(true);
pizza.setCheese(true);
pizza.setOregano(true);
pizza.setTomato(true);
pizza.setMeat(true);
cook(pizza);
}
public Pizza makeVeggiePizza() {
Pizza pizza = new Pizza();
pizza.setBase(true);
pizza.setCheese(true);
pizza.setOregano(true);
pizza.setTomato(true);
pizza.setMeat(false);
pizza.setVegetables(true);
cook(pizza);
}
private Pizza cook(Pizza pizza) {
//...
}
}
The above example is a badly coded class that makes 2 types of pizzas... There are many bad things in this piece of code, but I will just focus on the violation of the principle we are discussing.
Since every call done to the pizza object is to a concrete method, what we get is something very rigid and inflexible.
Every pizza has some ingredients that are mandatory, such as the base, tomato, cheese and oregano,but the rest are optional, so: why do we care about making calls to concrete methods, to set those extra ingredients, if is not even our concern?
In the following snippet of code, a template method is introduced to abstract the optional part and let sub-classes implement them.
public abstract class PizzaMaker {
//A method that has a call to an unimplemented abstract function,
//is known as a template method
public Pizza make() {
Pizza pizza = addBasicIngredients();
addSpecificIngredients(pizza);
cook(pizza);
return pizza;
}
public abstract void addSpecificIngredients(Pizza pizza);
private Pizza addBasicIngredients() {
Pizza pizza = new Pizza();
pizza.setBase(true);
pizza.setCheese(true);
pizza.setOregano(true);
pizza.setTomato(true);
return pizza;
}
private void cook(Pizza pizza) {
//...
}
}
public class MeatPizzaMaker extends PizzaMaker {
@Override
public void addSpecificIngredients(Pizza pizza) {
pizza.setMeat(true);
pizza.setVegetables(false);
}
}
public class VegetablesPizzaMaker extends PizzaMaker {
@Override
public void addSpecificIngredients(Pizza pizza) {
pizza.setMeat(false);
pizza.setVegetables(true);
}
}
The S.O.L.I.D principles, were identified by Robert C.Martin, but the Acronym was created by Michael Feathers in the year 2000, today they are well known in the world of object oriented sofwtare and many there is plenty literature on books and internet about them.
Just for the end of this post I would like to share with you a great podcast interview that I found on the web, were Uncle Bob, explains S.O.L.I.D in detail.
This comment has been removed by the author.
ReplyDeleteGreat article! For dependency inversion principle it will be helpful if you provide one more example.
Delete