c# - Convert to Design Interface -
we have login structure replace structure. however, keep both incase new structure not work.
what have right now
passwordcontrol.cs
public class passwordcontrol { public passwordcontrol() { // code here } public dologin() { // code here } }
main.cs
provide void btnlogin() { passwordcontrol pw = new passwordcontrol(); pw.dologin(); }
we convert design structure
iloginmanager.cs
interface iloginmanager { void dologin(); }
so can add second structure
passwordcontrol2.cs
public class passwordcontrol2 { public passwordcontrol() { // code here } public dologin() { // code here } }
how need modify code in main.cs take advantage of new design?
you want alternate between various implementations of same contract in runtime , that's why want use interface. interface contract: if class implements interface, provides services defined in interface. if class implements iloginmanager
interface, class contains public dologin()
method.
public interface iloginmanager { void dologin(); } public class passwordcontrol : iloginmanager { public passwordcontrol() { // code here } public void dologin() { // code here debug.writeline("passwordcontrol.dologin()"); } } public class passwordcontrol2 : iloginmanager { public passwordcontrol2() { // code here } public void dologin() { // code here debug.writeline("passwordcontrol2.dologin()"); } }
you said want determine iloginmanager
implementation when creating form. form can example read choice configuration file. means don't have recompile program when want change contract implementation, have change configuration file , restart program.
your form have following constructor:
public class myform { iloginmanager _loginmanager; public myform() { // let's assume read value 2 int choice = readchoicefromconfigfile(); if (choice == 1) { _loginmanager = new passwordcontrol(); } else { _loginmanager = new passwordcontrol2(); } } }
so when press login button, proper implementation used:
private void btnlogin() { // in our case passwordcontrol2.dologin() called _choice has been set 2 _loginmanager.dologin(); }
even better if responsibility of creation proper loginmanager
taken out of myform
form should not know details - wants object implements iloginmanager
should not care how it's made. ideally, you'll create iloginmanager
implementer before creating form , you'd pass form's dependency (you "inject" it, via constructor example; read dependency injection). this:
public class myform { iloginmanager _loginmanager; public myform(iloginmanager loginmanager) { if (loginmanager == null) { throw new argumentnullexception("loginmanager"); } _loginmanager = loginmanager; } } public static void main() { iloginmanager loginmanager = createloginmanager(); application.run(new myform(loginmanager)); } // factory method - creates desired instance implements given interface public static createloginmanager() { iloginmanager loginmanager = null; // let's assume read value 2 int choice = readchoicefromconfigfile(); if (choice == 1) { loginmanager = new passwordcontrol(); } else { loginmanager = new passwordcontrol2(); } return loginmanager; }