Skip to content

Instantly share code, notes, and snippets.

@jsgao0
Created March 28, 2017 07:51
Show Gist options
  • Save jsgao0/94bc3a61d22405ec4e3a810041f7333a to your computer and use it in GitHub Desktop.
Save jsgao0/94bc3a61d22405ec4e3a810041f7333a to your computer and use it in GitHub Desktop.
An example of IOC and DI.
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();
}
}
public interface IHuman {
void doWork();
}
public class Man implements IHuman {
public void doWork() {
System.out.println("The man is working.");
}
}
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