Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How interface provides decoupling to the client

Posted on 2010-11-18
4
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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 2000 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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