|
1 | 1 | /** |
2 | 2 | * The MIT License |
3 | 3 | * Copyright (c) 2014-2016 Ilkka Seppälä |
4 | | - * |
| 4 | + * <p> |
5 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | 6 | * of this software and associated documentation files (the "Software"), to deal |
7 | 7 | * in the Software without restriction, including without limitation the rights |
8 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 | 9 | * copies of the Software, and to permit persons to whom the Software is |
10 | 10 | * furnished to do so, subject to the following conditions: |
11 | | - * |
| 11 | + * <p> |
12 | 12 | * The above copyright notice and this permission notice shall be included in |
13 | 13 | * all copies or substantial portions of the Software. |
14 | | - * |
| 14 | + * <p> |
15 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26 | 26 | import com.google.inject.Injector; |
27 | 27 |
|
28 | 28 | /** |
29 | | - * |
| 29 | + * |
30 | 30 | * Dependency Injection pattern deals with how objects handle their dependencies. The pattern |
31 | 31 | * implements so called inversion of control principle. Inversion of control has two specific rules: |
32 | 32 | * - High-level modules should not depend on low-level modules. Both should depend on abstractions. |
|
36 | 36 | * naive implementation violating the inversion of control principle. It depends directly on a |
37 | 37 | * concrete implementation which cannot be changed. |
38 | 38 | * <p> |
39 | | - * The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete |
40 | | - * implementation but abstraction. It utilizes Dependency Injection pattern allowing its |
41 | | - * {@link Tobacco} dependency to be injected through its constructor. This way, handling the |
42 | | - * dependency is no longer the wizard's responsibility. It is resolved outside the wizard class. |
| 39 | + * The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible. |
| 40 | + * They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection |
| 41 | + * pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard}) |
| 42 | + * or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's |
| 43 | + * responsibility. It is resolved outside the wizard class. |
43 | 44 | * <p> |
44 | | - * The third example takes the pattern a step further. It uses Guice framework for Dependency |
| 45 | + * The fourth example takes the pattern a step further. It uses Guice framework for Dependency |
45 | 46 | * Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then |
46 | 47 | * used to create {@link GuiceWizard} object with correct dependencies. |
47 | 48 | * |
48 | 49 | */ |
49 | 50 | public class App { |
50 | 51 |
|
51 | | - /** |
52 | | - * Program entry point |
53 | | - * |
54 | | - * @param args command line args |
55 | | - */ |
56 | | - public static void main(String[] args) { |
57 | | - SimpleWizard simpleWizard = new SimpleWizard(); |
58 | | - simpleWizard.smoke(); |
| 52 | + /** |
| 53 | + * Program entry point |
| 54 | + * |
| 55 | + * @param args command line args |
| 56 | + */ |
| 57 | + public static void main(String[] args) { |
| 58 | + SimpleWizard simpleWizard = new SimpleWizard(); |
| 59 | + simpleWizard.smoke(); |
| 60 | + |
| 61 | + AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); |
| 62 | + advancedWizard.smoke(); |
59 | 63 |
|
60 | | - AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); |
61 | | - advancedWizard.smoke(); |
| 64 | + AdvancedSorceress advancedSorceress = new AdvancedSorceress(); |
| 65 | + advancedSorceress.setTobacco(new SecondBreakfastTobacco()); |
| 66 | + advancedSorceress.smoke(); |
62 | 67 |
|
63 | | - Injector injector = Guice.createInjector(new TobaccoModule()); |
64 | | - GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class); |
65 | | - guiceWizard.smoke(); |
66 | | - } |
| 68 | + Injector injector = Guice.createInjector(new TobaccoModule()); |
| 69 | + GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class); |
| 70 | + guiceWizard.smoke(); |
| 71 | + } |
67 | 72 | } |
0 commit comments