8
\$\begingroup\$

I am studying about how to use an interface for decoupling, and I wrote a program. Does it actually implemented decoupling? Are there any possible modifications to make it better?

public partial class MainPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        IDBInterface idbInterface = new SQL();
        CommunicationChannel com = new CommunicationChannel();
        com.Update(idbInterface);
    }
}

interface IDBInterface
{
    void Update();
}

class SQL:IDBInterface
{
    public void Update()
    {
        MessageBox.Show("Please wait.. We are updating SQL Server...");
    }
}

class Oracle : IDBInterface
{
    public void Update()
    {
        MessageBox.Show("Please wait.. We are updating Oracle Server...");
    }
}

class CommunicationChannel
{
    public void Update(IDBInterface idb)
    {
        idb.Update();
    }
}
}
\$\endgroup\$

migrated from stackoverflow.com Mar 4 '13 at 13:46

This question came from our site for professional and enthusiast programmers.

  • 2
    \$\begingroup\$ Still you can decopule your actual code MainPage class by delegating some other type to create the concrete objects. For which you can refer to Inversion of Control and Creational design patters. The problem now is if dependency objects creation fails, then Mainpage object creation also fails. NO Dependency Injection \$\endgroup\$ – zenwalker Feb 25 '13 at 9:59
  • 2
    \$\begingroup\$ on a different note, its better to get rid of IDBInterface. Make it IDb or even better DbInterface.. \$\endgroup\$ – nawfal Feb 25 '13 at 10:07
5
\$\begingroup\$

Yes that's it, that is decoupled.

However, the benefits of this are not visible in your example because the look of SQL and IDBInteraface are the same.

It would become obvious if your example was of that kind:

interface IDBInterface
{
    void Update(string sqlQuery);
    void Insert(string sqlQuery);
    void Select(string sqlQuery);
}

class SQL:IDBInterface
{
    public void Update(string sqlQuery){/*implementation*/};
    public void Insert(string sqlQuery){/*implementation*/};
    public void Select(string sqlQuery){/*implementation*/};

    public void connect(){/*implementation*/};
    public void disconnect(){/*implementation*/};
    public void optimize(){/*implementation*/};;
    public void backup(){/*implementation*/};;
}

In such cases, when using the IDBInterface, you would not even mind about all the administrative work to be done ( connect, disconnect, optimize, backup).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.