Solved

How interface provides decoupling to the client

Posted on 2010-11-18
4
197 Views
Last Modified: 2013-12-17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
  interface Interfface
  {

     void ADD();
     void SUB();

  }

  //Service Provider
  class ServiceProvider : Interfface
  {
    public void ADD()
    {
      Console.WriteLine(" I am ADD");
    }


    public void SUB()
    {
      Console.WriteLine(" I am Sub");
    }


  }


  class Program
  {
    // Client
    static void Main(string[] args)
    {

      Interfface Reff = new ServiceProvider();
      Reff.ADD();
      Reff.SUB();
     

      
    }
  }
}

Open in new window



Here the client is aware of Service Provider and Client he is creating reference to service provider.

There client is aware of service provider.
"   I have studied client need not to now the implementation part of service provider. "

But here client knows about service provider and client is also creating reference of it.

So I think client can get the logic of service provider by using Reflection concepts  in .NET.

Can you please help me  understanding this case.



0
Comment
Question by:N_Sri
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34167534
Not quite sure what it is you're asking here. Yes the client knows there is a ServiceProvider object in this scenario, and yes you could use reflection to examine it's internals.

But consider the case where you have a factory pattern and you don't know specifically what type of object is going to be returned to you, or a scenario where you want to swap out one service provider for another. By coding to an interface you can swap out the service provider without having to re-code anything else because you already know that whatever service you are getting it supports the methods defined by the interface.
0
 

Author Comment

by:N_Sri
ID: 34167700
Can you give code example
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 34167830
public interface IServiceInterface
{
       void DoSomething();
}

public class BookServiceProvider : IServiceInterface
{
     public void DoSomething()
     {
           Console.WriteLine("Book service doing something");
     }
}

public class CarServiceProvider : IServiceInterface
{
    public void DoSomething()
    {
          Console.WriteLine("Car service doing something");
    }
}

public static class ServiceFactory
{
     public static IServiceInterface CreateProvider(int i)
     {
           if (i == 1)
               return new BookServiceProvider();

           return new CarServiceProvider();
     }
}

class Program
{
      public static void Main()
      {
            // the factory could return one of two service providers, we don't necessarily know which, but because we're coding to
            // an interface we don't really care either
            IServiceProvider provider = ServiceFactory.CreateProvider(4);

            provider.DoSomething();
      }
}

Open in new window

0
 

Author Closing Comment

by:N_Sri
ID: 34167901
Thankyou
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now