S.O.L.I.D Principles for beginners (Part 5)
Before you start reading this blog post, please read Part 1, Part 2, Part 3 and Part 4 of the series, if you have not read it.
Dependency Inversion Principle
public class LocalShipment
{
public void TransferShipments()
{
Console.WriteLine("A complex process to transfer shipments");
}
}
The objective is to transfer shipments by an employee and for that, we create the employee class,which transfers the shipments when triggered with some events.
public class Employee
{
private readonly LocalShipment shipment;
public Employee()
{
shipment = new LocalShipment();
}
public void Transfer()
{
shipment.TransferShipment();
}
}
What if we need to add a new shipment type, International Shipment?
e.g.
public class LocalShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer local shipments");
}
}
public class InternationalShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer international shipments");
}
}
public class Employee
{
private readonly LocalShipment localShipment;
private readonly InternationalShipment internationalShipment;
public Employee(LocalShipment local, InternationalShipment international)
{
localShipment = local;
internationalShipment = international;
}
public void Transfer(bool isLocal)
{
if(isLocal)
{
localShipment.TransferShipment();
}
else
{
internationalShipment.TransferShipment();
}
}
}
This design will ruin all the SOLID principles.
Solution
public interface IShipment
{
void TransferShipment();
}
public class LocalShipment : IShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer local shipments");
}
}
public class InternationalShipment : IShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer international shipments");
}
}
public class Employee
{
private readonly IShipment shipment;
public Employee(IShipment shipment)
{
this.shipment = shipment;
}
public void Transfer()
{
shipment.TransferShipment();
}
}
In a way, all the principles are connected. If one of the principle is not followed, it will mess up the rest.
Please comment on the article, so that I can improve it which will be helpful for me as well as others.
Dependency Inversion Principle
Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
Let us take the same shipment example which is mentioned in the previous post (Part 3). We have 2 types of shipments defined. Local shipment and International shipments. Let is start our project with one type of shipment which had a method to transfer shipments. The implementation will be as follows
public class LocalShipment
{
public void TransferShipments()
{
Console.WriteLine("A complex process to transfer shipments");
}
}
The objective is to transfer shipments by an employee and for that, we create the employee class,which transfers the shipments when triggered with some events.
public class Employee
{
private readonly LocalShipment shipment;
public Employee()
{
shipment = new LocalShipment();
}
public void Transfer()
{
shipment.TransferShipment();
}
}
What if we need to add a new shipment type, International Shipment?
e.g.
public class LocalShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer local shipments");
}
}
public class InternationalShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer international shipments");
}
}
public class Employee
{
private readonly LocalShipment localShipment;
private readonly InternationalShipment internationalShipment;
public Employee(LocalShipment local, InternationalShipment international)
{
localShipment = local;
internationalShipment = international;
}
public void Transfer(bool isLocal)
{
if(isLocal)
{
localShipment.TransferShipment();
}
else
{
internationalShipment.TransferShipment();
}
}
}
This design will ruin all the SOLID principles.
Solution
public interface IShipment
{
void TransferShipment();
}
public class LocalShipment : IShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer local shipments");
}
}
public class InternationalShipment : IShipment
{
public void TransferShipment()
{
Console.WriteLine("A complex process to transfer international shipments");
}
}
public class Employee
{
private readonly IShipment shipment;
public Employee(IShipment shipment)
{
this.shipment = shipment;
}
public void Transfer()
{
shipment.TransferShipment();
}
}
In a way, all the principles are connected. If one of the principle is not followed, it will mess up the rest.
Please comment on the article, so that I can improve it which will be helpful for me as well as others.
Comments
Post a Comment