HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Using an interface for decoupling

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interfaceforusingdecoupling

Problem

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();
    }
}
}

Solution

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).

Code Snippets

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*/};;
}

Context

StackExchange Code Review Q#23435, answer score: 5

Revisions (0)

No revisions yet.