Solved

How interface provides decoupling to the client

Posted on 2010-11-18
4
198 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 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