Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How interface provides decoupling to the client

Posted on 2010-11-18
4
Medium Priority
?
204 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

886 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