Last active
June 26, 2016 20:15
-
-
Save antic183/a906edcbbba542b49c2e0127c59e18be to your computer and use it in GitHub Desktop.
java dependency injection example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anwendung; | |
public class Bank | |
{ | |
Konto konto; | |
public Bank(Konto konto){ | |
this.konto = konto; | |
} | |
public String gibKontoNummer(){ | |
return konto.gibKontoNummer(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anwendung; | |
public class Gescheaftskonto implements Konto | |
{ | |
private int kontoNummer; | |
public Gescheaftskonto(int kontoNummer) { | |
this.kontoNummer = kontoNummer; | |
} | |
public String gibKontoNummer() { | |
return kontoNummer + "- Geschäftskonto!"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anwendung; | |
public interface Konto | |
{ | |
String gibKontoNummer(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package anwendung; | |
public class PrivatKonto implements Konto | |
{ | |
private int kontoNummer; | |
public PrivatKonto(int kontoNummer) { | |
this.kontoNummer = kontoNummer; | |
} | |
public String gibKontoNummer() { | |
return kontoNummer + "- privates Konto!"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import anwendung.Bank; | |
import anwendung.Gescheaftskonto; | |
import anwendung.PrivatKonto; | |
public class App | |
{ | |
public static void main(String[] args) { | |
// you can implement DI with setter injection or with constructor injection. | |
// below you see an example with constructor injection | |
Bank bankUbs = new Bank(new PrivatKonto(12545451)); // constructor injection PrivatKonto | |
Bank bankCreditSuisse = new Bank(new Gescheaftskonto(15405)); // constructor injection Gescheaftskonto | |
String ktNrUbs = bankUbs.gibKontoNummer(); | |
System.out.println("UBS Konto = " + ktNrUbs); | |
String ktNrCreditSwiss = bankCreditSuisse.gibKontoNummer(); | |
System.out.println("Credit Suisse = " + ktNrCreditSwiss); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment