Welcome to the Design Pattern for Beginners article series. Thanks to all, special thanks to those who were with us from the beginning. If you are new to this series then I suggest you go through all the previous articles.
In this article we will learn how to implement the Implement Decouple Architecture in applications. Decoupling is very important in application development. It helps up to attach and detach components of any class without affecting another class.
The main goal of decoupling is to reduce dependency. Let's use an example to understand the concept. Think about a Desktop computer. If the monitor does not work then we just unplug it and replace it with a new one, even a different brand would work.
If the hard disk does not work then we just unplug it (hope you know how to unplug it! Ha ..Ha) and install a new one. Now, think of how to decouple each and every component? When we are changing one, it does not at all affect other components and the computer is running happily.
Now, our purpose is to implement the same kind of concept in software development. OK, one thing has been forgotten for explanation, tight coupling. Yes, it indicates that two entities are very much dependent on each other. If we change one then it will definitely effect another one.
OK, no more discussion. Let's implement the concept of decoupling.
How to implement?
Hmm…, one big question is, how to implement it? The solution is an interface. As the name suggests, it will be in the middle of two classes.
How it will come?
Let's see with an example. Here we will communicate with two classes using an interface.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace InterfaceService
- {
- public interface IService
- {
- void Serve();
- }
- public class Server : IService
- {
- public void Serve()
- {
- Console.WriteLine("Server executes");
- }
- }
- public class Consumer
- {
- IService obj;
- public Consumer(IService temp)
- {
- this.obj = temp;
- }
- public void Execute()
- {
- obj.Serve();
- }
- }
- }
Let's discuss the code above. At first we have created an Interface called IService. Then we implemented the IService interface within the Server class. Now, let's discuss the Consumer class. Within the Consumer constructor we are assigning an object of the Iservice interface and this object is being set to a local objet of the IService interface.
Now you can see that nowhere in the Consumer class have we specified a class name server and hence we are achieving decoupling.
Let's call the consumer class from the client code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace DependencyInjection
- {
- class Program
- {
- static void Main(string[] args)
- {
- Consumer C = new Consumer(new Server());
- C.Execute();
- Console.ReadLine();
- }
- }
- }
Within the Main function we are creating an object of the Consumer class by passing an object of the server class. And we are calling the Execute function to execute the service.
Here is sample output.
Now the question is, how will it help us? What is the facility for that? Let's change the class name from Server to Server123.
- public class Server123 : IService
- {
- public void Serve()
- {
- Console.WriteLine("Server executes");
- }
- }
- public class Consumer
- {
- IService obj;
- public Consumer(IService temp)
- {
- this.obj = temp;
- }
- public void Execute()
- {
- obj.Serve();
- }
- }
See, we have changed the server class name but does not change anything in the Consumer class. Now, just create an object of the new server class (Server123) from the client, as in the following.
- static void Main(string[] args)
- {
- Consumer C = new Consumer(new Server123());
- C.Execute();
- Console.ReadLine();
- }
And you will get the same output. Now again change the Class name from Consumer to Consumer123.
Have a look at the following code.
- public class Server : IService
- {
- public void Serve()
- {
- Console.WriteLine("Server executes");
- }
- }
- public class Consumer123
- {
- IService obj;
- public Consumer123(IService temp)
- {
- this.obj = temp;
- }
- public void Execute()
- {
- obj.Serve();
- }
- }
Here is the client code:
- static void Main(string[] args)
- {
- Consumer123 C = new Consumer123(new Server());
- C.Execute();
- Console.ReadLine();
- }
Now, we have tuned up the Consumer class and within the Main() function (Client code) but it does not even touch the server class. And we will get the same output.
This is the advantage of decouple architecture. Hope you understood the concept.