Solved

How interface provides decoupling to the client

Posted on 2010-11-18
4
196 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
Comment Utility
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
Comment Utility
Can you give code example
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
Thankyou
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

14 Experts available now in Live!

Get 1:1 Help Now