.Net 3.5 Extensions Part 2
In my previous post I have talk about "what is a method extension" and "how we can use that in our development". Let’s move into more detail.
Extension has change the way we design our applications. With previous versions of .net, which didn’t supports features like multiple inheritances, but with extensions we can design our applications to meet all those functionality. So here what I’m going to show is how we can program to interfaces using method extensions.
To start with we will take simple interface class scenario. We use interface to implement multiple features to a given class. Below is example of implementing multiple inheritances on a single class using interfaces.
public interface IFlyable {
void Fly();
}
Public interface IRunnable {
void Run();
}
Public interface ISwimming {
Void Swim();
Void BreatheInWater();
}
Public class Bird: IFlyable, IRunnable, ISwimming {
Public class Animal() {
}
Public void Fly() {
//Implement flying logic
}
Public void Run() {
//Implement Running Logic
}
Public void Swim() {
//Implement Swim Logic
}
Public void BreatheInWater() {
//Implement Breathe in water logic
}
}
What if there is a another class which needs to implement the same Swim logic in ISwimming, infact lets say all the classes which implement ISwimming has the same swim logic, but BreatheInWater logic is differs. How do we achieve this?
Public class Fish: ISwimming {
Public void Swim() {
//Implement same swim logic
}
Public void BreatheInWater() {
//Implement different Breathe in water logic
}
}
We could achieve this using some of the design patterns, but here I just wanted to show how easily we can do this using method extensions. Since Swim has the same logic in every class we can see as it’s a perfect method to use as an extensions. So we can implement the Swim logic in one place. To do that we need to remove the Swim method from ISwimming interface and place that in out method extension class as below.
public static void Swim(this ISwimming swim) {
//Implment the same swim logic
}
Note that we have this keyword before the ISwimming interface. This tells the compiler that ISwimming type has Swim method extension. So after implement the method extensions for the ISwimming interface we could modify the code as below.
public interface IFlyable {
void Fly();
}
Public interface IRunnable {
void Run();
}
Public interface ISwimming {
Void BreatheInWater();
}
Public class Bird: IFlyable, IRunnable, ISwimming {
Public class Animal() {
}
Public void Fly() {
//Implement flying logic
}
Public void Run() {
//Implement Running Logic
}
Public void BreatheInWater() {
//Implement Breathe in water logic
}
}
Public class Fish: ISwimming {
Public void BreatheInWater() {
//Implement different Breathe in water logic
}
}
Please note that we have removed the Swim method from the interface as well as from all the implementation classes. We only wrote the Swim method in our extensions class. This makes our code cleaner and easier to write. So when ever we create an object which implements ISwimming interface we get Swim method as an extension.
So here I covered how we can easily use Method extensions in our interfaces. Hope this helps!