Created
March 28, 2017 07:51
-
-
Save jsgao0/94bc3a61d22405ec4e3a810041f7333a to your computer and use it in GitHub Desktop.
An example of IOC and DI.
This file contains 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
public class Human { | |
private IHuman human; | |
public Human(IHuman human) { | |
this.human = human; | |
} | |
public void doSomeWork() { | |
human.doWork(); | |
} | |
public static void main(String[] args) | |
{ | |
Human human = new Human(new Woman()); | |
human.doSomeWork(); | |
} | |
} |
This file contains 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
public interface IHuman { | |
void doWork(); | |
} |
This file contains 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
public class Man implements IHuman { | |
public void doWork() { | |
System.out.println("The man is working."); | |
} | |
} |
This file contains 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
public class Woman implements IHuman { | |
public void doWork() { | |
System.out.println("The woman is working."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment