The principle of segregation of interfaces is very simple, says not to create interfaces with methods that the class does not use, the interfaces should be as small as possible.
So this rule can be created for this rule in the style:
Interfaces should be small, so that later the classes do not implement methods they do not need, the interfaces should be concret and as specific as possible.
It is better to use abstract classes to create base types, because they can describe a specific type, have appropriate attributes and methods, interfaces are stateless so they should not describe business logic. Interfaces should only inform the programmer about any behavior.
Look at the code below:
namespace InterfaceSegregation
{
interface IQuantity
{
void DisplayQuantityLiter();
void DisplayQuantityKilo();
}
abstract class TypeProduct
{
public abstract void GetTypeProductsCount(int quantity);
}
class Fruit : TypeProduct, IQuantity
{
public int NumberFruits { get; set; }
public override void GetTypeProductsCount(int quantity)
{
NumberFruits += quantity;
}
public void DisplayQuantityLiter()
{
throw new NotImplementedException();
}
public void DisplayQuantityKilo()
{
//display the amount of kilos
}
}
class Drink : TypeProduct, IQuantity
{
public int NumberDrinks { get; set; }
public override void GetTypeProductsCount(int quantity)
{
NumberDrinks += quantity;
}
public void DisplayQuantityLiter()
{
//display the number of liters
}
//the DisplayQuantityKilo method does not match, so it is not used in this class
public void DisplayQuantityKilo()
{
throw new NotImplementedException();
}
}
}
This interface is incorrect because not all methods from this interface match and used.
It’s better to do two separate interfaces like this:
namespace InterfaceSegregation
{
interface IQuantityKilo
{
void DisplayQuantityKilo();
}
interface IQuantityLiter
{
void DisplayQuantityLiter();
}
abstract class TypeProduct
{
public abstract void GetTypeProductsCount(int quantity);
}
class Fruit : TypeProduct, IQuantityKilo
{
public int NumberFruits { get; set; }
public override void GetTypeProductsCount(int quantity)
{
NumberFruits += quantity;
}
public void DisplayQuantityKilo()
{
//display the amount of kilos
}
}
class Drink : TypeProduct, IQuantityLiter
{
public int NumberDrinks { get; set; }
public override void GetTypeProductsCount(int quantity)
{
NumberDrinks += quantity;
}
public void DisplayQuantityLiter()
{
//display the number of liters
}
}
}
As you can see by dividing the interfaces, we keep order. Thanks to this, derived classes do not have methods that they do not need.
Summary
That’s all about Interface segregation principle.
This content also you can find on my blog http://devman.pl/programtech/solid-principles-4-interface-segregation-principle/
If you recognise it as useful, share it with others so that others can also use it.
Leave upvote and follow and wait for next articles :) .
And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂
– site on fb: Devman.pl-Sławomir Kowalski
– group on fb: DevmanCommunity
Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.