Interface | Full Explanation (Definition, usage, structure and example with C#)
Introduction
Hello, my name is Daniela Barazarte and I want to welcome you to this complete explanation of the interface. This explanation will be intuitive and simple as well as all the explanations that are part of #DetectaLaLogica.
If you prefer videos, here is a complete tutorial made by me on YouTube, it's in Spanish but it also has subtitles: https://www.youtube.com/watch?v=UbZORJzMQec
Theory
Definition of each word
"Design pattern"
- Pattern: is a type of repetitive events or objects
- Design: creative activity whose purpose is to project objects that are useful and aesthetic
"Interface"
- Interface: zone of communication or action of one system on another
Full definition
The interface design pattern is a specification of methods and properties that a class must provide, and any class that implements that interface is committed to providing those methods and properties.
#DetectTheLogic
The word "Interface" is defined as "zone of communication or action of one system on another". The Interface design pattern is so called because an interface is talking to a class to tell it what parameters that class should have. It's like a contract where the interface declares what components it has and the class must have those components as well.
Use
So that
The interface is used to define a common interface that can be implemented by several different classes. This pattern allows these classes to have different behaviors while adhering to the same interface, providing flexibility and modularity to the application.
When and how
- By having multiple classes that must comply with a specific set of behaviors
- By having a common structure for plugins or external modules that must comply with a specific set of behaviors
- When creating an application that must be compatible with different architectures of distributed systems
- When creating an application that should be easily extensible and modular
Practice
Exercise
You have to create a console project where you have several animals and you can write in the console the sound that each one of them makes
Method: Interface
The orange part (above): is an Animal interface
The blue part (bottom): are implementations of that interface, in this case, 3 animals (cat, dog, bird)
public interface Animal
{
void MakeSound();
void CallFriend();
}
public class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Wow! Woof!");
}
public void CallFriend()
{
Console.WriteLine("Come on friend dog!");
}
}
public class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Meow! Meow!");
}
public void CallFriend()
{
Console.WriteLine("Come on friend cat!");
}
}
public class Bird : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Tweet Tweet! Wow!");
}
public void CallFriend()
{
Console.WriteLine("Come on friend bird!");
}
}
public class program
{
public static void Main()
{
IAnimal myCat = new Cat();
IAnimal myDog = new Dog();
IAnimal myBird = new Bird();
myCat.MakeSound();
myCat.CallFriend();
myDog.MakeSound();
myDog.CallFriend();
myBird.MakeSound();
myBird.CallFriend();
}
}
Having an "Animal" interface that implements "Bird", "Dog" and "Cat" is beneficial because it provides a useful abstraction that allows for a high level of flexibility and extensibility in software design. By defining an "Animal" interface, you establish certain behaviors and characteristics that all animals must have, such as the ability to move and breathe.
Implementing this interface on "Bird", "Dog" and "Cat" ensures that they all have these common characteristics and can be used interchangeably in code that requires an "Animal" object. This allows the code to be more modular and scalable, as new animal types that conform to the "Animal" interface can be easily added without having to change existing code.
Importance
The interface design pattern has several benefits:
- Abstraction: Interfaces allow defining a set of methods and properties that must be implemented by a class that implements them. This allows you to abstract the implementation of a class behind an interface, making your code easier to understand and maintain.
- Separation of concerns: By defining interfaces, you can clearly separate the user interface from the business logic. This allows the different parts of an application to communicate with each other through well-defined interfaces.
- Polymorphism: Interfaces allow polymorphism, which means that a class that implements an interface can be treated as if it were of that type of interface. This allows classes that implement the same interface to be used interchangeably in different parts of the code, making it easier to reuse and maintain your code.
Farewell
(Remember) #DetectaLaLógica: The word "Interface" is defined as "zone of communication or action of one system on another". The Interface design pattern is so called because an interface is talking to a class to tell it what parameters that class should have. It's like a contract where the interface declares what components it has and the class must have those components as well.
You can practice this topic in my GitHub repository (C# language code): https://github.com/danielabarazarte/DetectaLaLogica
Thank you very much for reading, if you have any questions you can comment and you can also follow me to see more posts of this style, thanks <3.
Top comments (0)