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();
}
}
}
1 Answer 1
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).
IDBInterface
. Make itIDb
or even betterDbInterface
.. \$\endgroup\$