PageRenderTime 135ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java

https://gitlab.com/akkhil2012/java-design-patterns
Java | 67 lines | 14 code | 5 blank | 48 comment | 0 complexity | 7e357a56958093d43d81b6cb90429b59 MD5 | raw file
  1. /**
  2. * The MIT License
  3. * Copyright (c) 2014 Ilkka Seppälä
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. package com.iluwatar.dependency.injection;
  24. import com.google.inject.Guice;
  25. import com.google.inject.Injector;
  26. /**
  27. *
  28. * Dependency Injection pattern deals with how objects handle their dependencies. The pattern
  29. * implements so called inversion of control principle. Inversion of control has two specific rules:
  30. * - High-level modules should not depend on low-level modules. Both should depend on abstractions.
  31. * - Abstractions should not depend on details. Details should depend on abstractions.
  32. * <p>
  33. * In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a
  34. * naive implementation violating the inversion of control principle. It depends directly on a
  35. * concrete implementation which cannot be changed.
  36. * <p>
  37. * The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
  38. * implementation but abstraction. It utilizes Dependency Injection pattern allowing its
  39. * {@link Tobacco} dependency to be injected through its constructor. This way, handling the
  40. * dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
  41. * <p>
  42. * The third example takes the pattern a step further. It uses Guice framework for Dependency
  43. * Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
  44. * used to create {@link GuiceWizard} object with correct dependencies.
  45. *
  46. */
  47. public class App {
  48. /**
  49. * Program entry point
  50. *
  51. * @param args command line args
  52. */
  53. public static void main(String[] args) {
  54. SimpleWizard simpleWizard = new SimpleWizard();
  55. simpleWizard.smoke();
  56. AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
  57. advancedWizard.smoke();
  58. Injector injector = Guice.createInjector(new TobaccoModule());
  59. GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
  60. guiceWizard.smoke();
  61. }
  62. }