Google Guice

5,325 views

Published on

10-th meetuo of Java User Group of Lviv

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,325
On SlideShare
0
From Embeds
0
Number of Embeds
1,427
Actions
Shares
0
Downloads
109
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Google Guice

  1. 1. Google guice It’s easy © Skelia 201 1
  2. 2. Agenda <ul><li>Pattern Dependency injection </li></ul><ul><li>Why we need DI </li></ul><ul><li>Google guice introduction </li></ul><ul><li>Guice simple injection </li></ul><ul><li>Integration with Servlets </li></ul><ul><li>Integration with Guice </li></ul><ul><li>Philosophy </li></ul><ul><li>Instead of summary </li></ul><ul><li>Q&A </li></ul>© Skelia 2011
  3. 3. DI or Don't Call Me, I Will Call You <ul><li>Dependency injection  ( DI ) in  object-oriented   computer programming  is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an  external dependency  – a reference – to a  software component . In technical terms, it is a  design pattern  that separates behavior from  dependency resolution , thus decoupling highly dependent components. </li></ul>© Skelia 2011 Wikipedia: http://en.wikipedia.org/wiki/Dependency_injection
  4. 4. DI or Don't Call Me, I Will Call You <ul><li>Dependency injection  ( DI ) in  object-oriented   computer programming  is a technique that indicates to a part of a program which other parts it can use, i.e. to supply an  external dependency  – a reference – to a  software component . In technical terms, it is a  design pattern  that separates behavior from  dependency resolution , thus decoupling highly dependent components. </li></ul>© Skelia 2011 Wikipedia: http://en.wikipedia.org/wiki/Dependency_injection
  5. 5. DI or Don't Call Me, I Will Call You © Skelia 2011
  6. 6. DI or Don't Call Me, I Will Call You © Skelia 2011 Wild programmer Spaghetti Architecture
  7. 7. Google Guice © Skelia 2011 Google-Guice Current version 3.0 Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and above, brought to you by Google.
  8. 8. Google Guice © Skelia 2011
  9. 9. Google Guice (Module) © Skelia 2011 <ul><li>Module Types: </li></ul><ul><li>Non-obvious </li></ul><ul><li>AbstractModule </li></ul><ul><li>ServletModule </li></ul>
  10. 10. Google Guice (Binding) © Skelia 2011 <ul><li>Binding types: </li></ul><ul><li>Bind implementation </li></ul><ul><li>bind(PaymentCashImpl.class) </li></ul><ul><li>Bind implementation to interface bind(Payment.class).to(PaymentCardImpl.class) </li></ul><ul><li>Bind multiple implementation to interface </li></ul><ul><li>bind(Payment.class).annotatedWith(Cash.class).to(PaymentCashImpl.class) </li></ul><ul><li>bind(Payment.class).annotatedWith(Names.named(&quot;Cash&quot;)).to(PaymentCashImpl.class) </li></ul>
  11. 11. Google Guice (Injection) © Skelia 2011 <ul><li>Injection types : </li></ul><ul><li>Constructor </li></ul><ul><li>Method </li></ul><ul><li>Field (not recommended) </li></ul><ul><li>@Inject private @Card Payment payment; </li></ul><ul><li>@ Named(&quot;PaymentType&quot;) @Inject private String text; </li></ul>
  12. 12. Google Guice (Provides) © Skelia 2011   public class PayModule extends AbstractModule{   protected void configure() {     }     @Provides     Payment providePayment(){         CashPayment pay = new CashPayment();         pay.setAmount(100);         return pay;     } @Provides List<String> providesListOfString() { return new ArrayList<String>(); } @Inject public void setList(List<String> list)
  13. 13. Google Guice (Scope) © Skelia 2011
  14. 14. Google Guice (AOP) © Skelia 2011
  15. 15. Google Guice (Sample) © Skelia 2011
  16. 16. Google Guice (Servlet integration) © Skelia 2011
  17. 17. Google Guice (Sample) © Skelia 2011
  18. 18. Google Guice (Integration) © Skelia 2011 Mobile RoboGuice - Guice for Android. <ul><li>Other Languages </li></ul><ul><ul><li>Groovy-Guice - Groovy </li></ul></ul><ul><ul><li>Ninject - .NET </li></ul></ul><ul><ul><li>Snake Guice - Python </li></ul></ul><ul><ul><li>Smartypants-IOC - Flex/AS3 </li></ul></ul><ul><ul><li>Dawn - AS3 </li></ul></ul>
  19. 19. Google Guice (Integration) © Skelia 2011
  20. 20. Google Guice (philosophy) © Skelia 2011
  21. 21. Google Guice (philosophy) © Skelia 2011
  22. 22. Instead of Conclusion © Skelia 2011
  23. 23. Thank you ! © Skelia 2011

×