Work on the DI example. Added second Wizard with injected Tobacco.

This commit is contained in:
Ilkka Seppala 2015-05-22 23:37:42 +03:00
parent 8b9e829b09
commit b2bfdb1752
7 changed files with 42 additions and 17 deletions

View File

@ -0,0 +1,15 @@
package com.iluwatar;
public class AdvancedWizard implements Wizard {
private Tobacco tobacco;
public AdvancedWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
}

View File

@ -3,7 +3,10 @@ package com.iluwatar;
public class App {
public static void main( String[] args ) {
Wizard wizard = new Wizard();
wizard.smoke();
SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke();
}
}

View File

@ -1,9 +1,4 @@
package com.iluwatar;
public class OldTobyTobacco implements Tobacco {
@Override
public void smoke() {
System.out.println(String.format("Smoking %s", this.getClass().getSimpleName()));
}
public class OldTobyTobacco extends Tobacco {
}

View File

@ -0,0 +1,4 @@
package com.iluwatar;
public class SecondBreakfastTobacco extends Tobacco {
}

View File

@ -0,0 +1,10 @@
package com.iluwatar;
public class SimpleWizard implements Wizard {
private OldTobyTobacco tobacco = new OldTobyTobacco();
public void smoke() {
tobacco.smoke(this);
}
}

View File

@ -1,7 +1,8 @@
package com.iluwatar;
public interface Tobacco {
public abstract class Tobacco {
public void smoke();
public void smoke(Wizard wizard) {
System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName()));
}
}

View File

@ -1,10 +1,7 @@
package com.iluwatar;
public class Wizard {
public interface Wizard {
private OldTobyTobacco tobacco = new OldTobyTobacco();
public void smoke() {
tobacco.smoke();
}
void smoke();
}