SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
Dependencies with
and without Guice
Outline

    What are dependencies?
    How dependencies cause problems
    Inversion of Control and Hollywood
    Guice – the solution for everything (almost)
            Providers
            The creation of a new object




15.04.2011                       Joel Krebs         2
What are dependencies?
- or why not to code only one class

    Classes depends on others, that comes with
     Object orienteted programming (OOP)
    Dependencies are classes, that are used by
     other classes in order to work
    Example:             Board




                   Die                Player




15.04.2011               Joel Krebs               3
Why are dependencies bad?
- well that depends...

    What are key features of 'good code'?
            Testable
            Modular
            Changable
            …
    Imagine that for huge applications...




15.04.2011                Joel Krebs         4
Inversion of Control
- what has Hollywood to do with all this?

    Inversion of Control (IoF) is a design pattern to
     reduce dependencies without any frameworks
    Difference to procedural architecture:
             Board              Die                Player




                      Die

                                           Board

                     Player

15.04.2011                    Joel Krebs                    5
Methods for Inversion of Control
- something everybody can do

    General rules:
            Use interfaces
            Use getters and setters
    1. Using factories
             IDie die = DieFactory.get()
    2. Using constructors injection
             public Board(IPlayer player, IDie die)
    3. Using setter injection
             public void setDie(IDie die)
15.04.2011                       Joel Krebs           6
Guice
- well, it works for Google, let's try

    Real dependency injection only works with
     frameworks (e.g. Guice, Spring)
    They can save you a lot of time and coding, but
     not for too small applications
    Guice needs you...
            ...to tell Guice wich Interface maps to which class
             => module
            ...to give him the necesarry information how to
             construct a object => Provider

15.04.2011                        Joel Krebs                       7
The Provider
- let's just copy & paste

import com.google.inject.Inject;
import com.google.inject.Provider;

public class GameProvider implements Provider<Game> {
  private Provider<IDie> dieProvider ;

    @Inject
    public GameProvider(Provider<IDie> dieProvider) {
      this.dieProvider = dieProvider;
    }

    @Override
    public Game get() {
      Player[] players = { new Player("Jack"), new Player("Jill")};
      Game game = new Game(12, players, dieProvider .get());

        game.setSquareToLadder(2, 4);
        game.setSquareToLadder(7, 2);
        game.setSquareToSnake(11, -6);
        game.setSquareToLargeSquare(4);
        game.setSquareToTrapDoor(10, -6);

        return game;
    }
}
15.04.2011                                  Joel Krebs                8
The Module
- different modules for testing and use
package snakes;

import com.google.inject.AbstractModule;

public class SnakesModule extends AbstractModule {
  @Override
  protected void configure() {
     bind(IDie.class)
      .to(Die.class);

        bind(Game.class)
         .toProvider(GameProvider.class);
    }
}


public class SnakesTestModule extends AbstractModule {
  @Override
  protected void configure() {
     bind(IDie.class)
      .toInstance(context.mock(IDie.class));

        bind(Game.class)
         .toProvider(GameProvider.class);
    }
}
15.04.2011                                  Joel Krebs   9
How we get new objects
- all roads lead to Rome...or a game

    So if I want a board game, what happens?
                          Main method
              Injector.getInstance(Game.class)




                         SnakesModule
              bind(Game.class)
               .toProvider(GameProvider.class)




                         GameProvider
              new Game(... dieProvider.get())


15.04.2011                  Joel Krebs           10
Improvements
- there's always a way to code better

    Use annotions that are provided by Guice
            @named
            @singelton
    Merge modules
    Merge providers




15.04.2011                Joel Krebs            11
Conclusions
- doesn't make it all more complicate?

    Reduce dependencies in general improves your
     code in many ways
    It needs a certain kind of mindset, but you get
     used to it
    Frameworks like Guice can improve your code
     as well and may save you a lot of time
            But I would not use them for too small projects



15.04.2011                        Joel Krebs                   12
Questions
- experiences, smart remarks, suggestions




                           Joel Krebs
                           joel.krebs@students.unibe.ch
                           @jomikr on
                           www.aleaiactaest.ch

15.04.2011         Joel Krebs                             13

Más contenido relacionado

Destacado

11프로그래밍 김영일
11프로그래밍 김영일11프로그래밍 김영일
11프로그래밍 김영일bit_blue
 
4 q09 presentation
4 q09 presentation4 q09 presentation
4 q09 presentationEquatorial
 
Vocabulary describing people
Vocabulary describing peopleVocabulary describing people
Vocabulary describing peoplejolehidy6
 
Технологические основы для построения облачной среды
Технологические основы для построения облачной средыТехнологические основы для построения облачной среды
Технологические основы для построения облачной средыpcweek_ua
 
Mathematics Olympiad 2
Mathematics Olympiad 2Mathematics Olympiad 2
Mathematics Olympiad 2Edy Wihardjo
 
2nd mini project instructions
2nd mini project instructions2nd mini project instructions
2nd mini project instructionsWilder Moncada
 
Il secondo numero del nostro giornalino-Marzo 2016
Il secondo numero del nostro giornalino-Marzo 2016Il secondo numero del nostro giornalino-Marzo 2016
Il secondo numero del nostro giornalino-Marzo 2016infocannizzaro
 
System Furniture Design
System Furniture DesignSystem Furniture Design
System Furniture Designlbridge89
 
Mijn derde portret in oliever van Koningin Maxima Saskia Vugts Portretschilder
Mijn derde portret in oliever van Koningin Maxima  Saskia Vugts PortretschilderMijn derde portret in oliever van Koningin Maxima  Saskia Vugts Portretschilder
Mijn derde portret in oliever van Koningin Maxima Saskia Vugts PortretschilderSaskia Vugts Portretschilder
 
The Only Way is Essex - Frail to Raging.
The Only Way is Essex - Frail to Raging.The Only Way is Essex - Frail to Raging.
The Only Way is Essex - Frail to Raging.bellahoy
 
Görög nyár(4)+ani (nx power lite)
Görög nyár(4)+ani (nx power lite)Görög nyár(4)+ani (nx power lite)
Görög nyár(4)+ani (nx power lite)VarganeAnny
 
Workshop 2015 recuperaciones grado 8 vo
Workshop 2015 recuperaciones grado 8 voWorkshop 2015 recuperaciones grado 8 vo
Workshop 2015 recuperaciones grado 8 vojolehidy6
 
耶穌的文字學
耶穌的文字學耶穌的文字學
耶穌的文字學chengchunhao
 
発達の心配なお子さんのためのiPad活用講座
発達の心配なお子さんのためのiPad活用講座発達の心配なお子さんのためのiPad活用講座
発達の心配なお子さんのためのiPad活用講座Naoya Sangu
 
Projekty na lekcji języka obcego.
Projekty na lekcji języka obcego.Projekty na lekcji języka obcego.
Projekty na lekcji języka obcego.Monika Wisła
 
Fotoalbum(2)+ani (nx power lite)
Fotoalbum(2)+ani (nx power lite)Fotoalbum(2)+ani (nx power lite)
Fotoalbum(2)+ani (nx power lite)VarganeAnny
 

Destacado (20)

Best Work
Best WorkBest Work
Best Work
 
11프로그래밍 김영일
11프로그래밍 김영일11프로그래밍 김영일
11프로그래밍 김영일
 
4 q09 presentation
4 q09 presentation4 q09 presentation
4 q09 presentation
 
Vocabulary describing people
Vocabulary describing peopleVocabulary describing people
Vocabulary describing people
 
Svd7 ts hich2
Svd7 ts hich2Svd7 ts hich2
Svd7 ts hich2
 
Технологические основы для построения облачной среды
Технологические основы для построения облачной средыТехнологические основы для построения облачной среды
Технологические основы для построения облачной среды
 
Mathematics Olympiad 2
Mathematics Olympiad 2Mathematics Olympiad 2
Mathematics Olympiad 2
 
2nd mini project instructions
2nd mini project instructions2nd mini project instructions
2nd mini project instructions
 
Il secondo numero del nostro giornalino-Marzo 2016
Il secondo numero del nostro giornalino-Marzo 2016Il secondo numero del nostro giornalino-Marzo 2016
Il secondo numero del nostro giornalino-Marzo 2016
 
System Furniture Design
System Furniture DesignSystem Furniture Design
System Furniture Design
 
Mijn derde portret in oliever van Koningin Maxima Saskia Vugts Portretschilder
Mijn derde portret in oliever van Koningin Maxima  Saskia Vugts PortretschilderMijn derde portret in oliever van Koningin Maxima  Saskia Vugts Portretschilder
Mijn derde portret in oliever van Koningin Maxima Saskia Vugts Portretschilder
 
The Only Way is Essex - Frail to Raging.
The Only Way is Essex - Frail to Raging.The Only Way is Essex - Frail to Raging.
The Only Way is Essex - Frail to Raging.
 
Görög nyár(4)+ani (nx power lite)
Görög nyár(4)+ani (nx power lite)Görög nyár(4)+ani (nx power lite)
Görög nyár(4)+ani (nx power lite)
 
Workshop 2015 recuperaciones grado 8 vo
Workshop 2015 recuperaciones grado 8 voWorkshop 2015 recuperaciones grado 8 vo
Workshop 2015 recuperaciones grado 8 vo
 
Saludable profile
Saludable profileSaludable profile
Saludable profile
 
Physical constants
Physical constantsPhysical constants
Physical constants
 
耶穌的文字學
耶穌的文字學耶穌的文字學
耶穌的文字學
 
発達の心配なお子さんのためのiPad活用講座
発達の心配なお子さんのためのiPad活用講座発達の心配なお子さんのためのiPad活用講座
発達の心配なお子さんのためのiPad活用講座
 
Projekty na lekcji języka obcego.
Projekty na lekcji języka obcego.Projekty na lekcji języka obcego.
Projekty na lekcji języka obcego.
 
Fotoalbum(2)+ani (nx power lite)
Fotoalbum(2)+ani (nx power lite)Fotoalbum(2)+ani (nx power lite)
Fotoalbum(2)+ani (nx power lite)
 

Similar a Brief introduction into dependencies

Desenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos androidDesenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos androidDiego Figueredo
 
Feedback on Part 1 of the CSLP
Feedback on Part 1 of the CSLPFeedback on Part 1 of the CSLP
Feedback on Part 1 of the CSLPStephen Gilmore
 
10 Big Ideas from Industry
10 Big Ideas from Industry10 Big Ideas from Industry
10 Big Ideas from IndustryGarth Gilmour
 
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in ActionBill Scott
 
Java ide comparision
Java ide comparisionJava ide comparision
Java ide comparisionRaj
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in ActionBill Scott
 
Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012Hong Le Van
 
Fed Up Of Framework Hype Dcphp
Fed Up Of Framework Hype DcphpFed Up Of Framework Hype Dcphp
Fed Up Of Framework Hype DcphpTony Bibbs
 
David Thiel - Secure Development On iOS
David Thiel - Secure Development On iOSDavid Thiel - Secure Development On iOS
David Thiel - Secure Development On iOSSource Conference
 
ISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art, LLC
 
Lean engineering for lean/balanced teams: lessons learned (and still learning...
Lean engineering for lean/balanced teams: lessons learned (and still learning...Lean engineering for lean/balanced teams: lessons learned (and still learning...
Lean engineering for lean/balanced teams: lessons learned (and still learning...Balanced Team
 
Giving (and taking) back - Mike Fiedler
Giving (and taking) back - Mike FiedlerGiving (and taking) back - Mike Fiedler
Giving (and taking) back - Mike FiedlerDevopsdays
 
Tdd in android (mvp)
Tdd in android (mvp)Tdd in android (mvp)
Tdd in android (mvp)Prateek Jain
 

Similar a Brief introduction into dependencies (20)

Desenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos androidDesenvolvimento moderno de aplicativos android
Desenvolvimento moderno de aplicativos android
 
Feedback on Part 1 of the CSLP
Feedback on Part 1 of the CSLPFeedback on Part 1 of the CSLP
Feedback on Part 1 of the CSLP
 
10 Big Ideas from Industry
10 Big Ideas from Industry10 Big Ideas from Industry
10 Big Ideas from Industry
 
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
8 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
 
Integreation
IntegreationIntegreation
Integreation
 
Java ide comparision
Java ide comparisionJava ide comparision
Java ide comparision
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
 
Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012
 
Intellij idea features
Intellij idea featuresIntellij idea features
Intellij idea features
 
Fed Up Of Framework Hype Dcphp
Fed Up Of Framework Hype DcphpFed Up Of Framework Hype Dcphp
Fed Up Of Framework Hype Dcphp
 
David Thiel - Secure Development On iOS
David Thiel - Secure Development On iOSDavid Thiel - Secure Development On iOS
David Thiel - Secure Development On iOS
 
Intelli j idea-report
Intelli j idea-reportIntelli j idea-report
Intelli j idea-report
 
ISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin MultiplatformISS Art. How to do IT. Kotlin Multiplatform
ISS Art. How to do IT. Kotlin Multiplatform
 
Lean engineering for lean/balanced teams: lessons learned (and still learning...
Lean engineering for lean/balanced teams: lessons learned (and still learning...Lean engineering for lean/balanced teams: lessons learned (and still learning...
Lean engineering for lean/balanced teams: lessons learned (and still learning...
 
Android ndk: Entering the native world
Android ndk: Entering the native worldAndroid ndk: Entering the native world
Android ndk: Entering the native world
 
Sikuli
SikuliSikuli
Sikuli
 
Giving (and taking) back - Mike Fiedler
Giving (and taking) back - Mike FiedlerGiving (and taking) back - Mike Fiedler
Giving (and taking) back - Mike Fiedler
 
Tdd in android (mvp)
Tdd in android (mvp)Tdd in android (mvp)
Tdd in android (mvp)
 
Enabling Lean at Enterprise Scale: Lean Engineering in Action
Enabling Lean at Enterprise Scale: Lean Engineering in ActionEnabling Lean at Enterprise Scale: Lean Engineering in Action
Enabling Lean at Enterprise Scale: Lean Engineering in Action
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Brief introduction into dependencies

  • 2. Outline  What are dependencies?  How dependencies cause problems  Inversion of Control and Hollywood  Guice – the solution for everything (almost)  Providers  The creation of a new object 15.04.2011 Joel Krebs 2
  • 3. What are dependencies? - or why not to code only one class  Classes depends on others, that comes with Object orienteted programming (OOP)  Dependencies are classes, that are used by other classes in order to work  Example: Board Die Player 15.04.2011 Joel Krebs 3
  • 4. Why are dependencies bad? - well that depends...  What are key features of 'good code'?  Testable  Modular  Changable  …  Imagine that for huge applications... 15.04.2011 Joel Krebs 4
  • 5. Inversion of Control - what has Hollywood to do with all this?  Inversion of Control (IoF) is a design pattern to reduce dependencies without any frameworks  Difference to procedural architecture: Board Die Player Die Board Player 15.04.2011 Joel Krebs 5
  • 6. Methods for Inversion of Control - something everybody can do  General rules:  Use interfaces  Use getters and setters  1. Using factories IDie die = DieFactory.get()  2. Using constructors injection public Board(IPlayer player, IDie die)  3. Using setter injection public void setDie(IDie die) 15.04.2011 Joel Krebs 6
  • 7. Guice - well, it works for Google, let's try  Real dependency injection only works with frameworks (e.g. Guice, Spring)  They can save you a lot of time and coding, but not for too small applications  Guice needs you...  ...to tell Guice wich Interface maps to which class => module  ...to give him the necesarry information how to construct a object => Provider 15.04.2011 Joel Krebs 7
  • 8. The Provider - let's just copy & paste import com.google.inject.Inject; import com.google.inject.Provider; public class GameProvider implements Provider<Game> { private Provider<IDie> dieProvider ; @Inject public GameProvider(Provider<IDie> dieProvider) { this.dieProvider = dieProvider; } @Override public Game get() { Player[] players = { new Player("Jack"), new Player("Jill")}; Game game = new Game(12, players, dieProvider .get()); game.setSquareToLadder(2, 4); game.setSquareToLadder(7, 2); game.setSquareToSnake(11, -6); game.setSquareToLargeSquare(4); game.setSquareToTrapDoor(10, -6); return game; } } 15.04.2011 Joel Krebs 8
  • 9. The Module - different modules for testing and use package snakes; import com.google.inject.AbstractModule; public class SnakesModule extends AbstractModule { @Override protected void configure() { bind(IDie.class) .to(Die.class); bind(Game.class) .toProvider(GameProvider.class); } } public class SnakesTestModule extends AbstractModule { @Override protected void configure() { bind(IDie.class) .toInstance(context.mock(IDie.class)); bind(Game.class) .toProvider(GameProvider.class); } } 15.04.2011 Joel Krebs 9
  • 10. How we get new objects - all roads lead to Rome...or a game  So if I want a board game, what happens? Main method Injector.getInstance(Game.class) SnakesModule bind(Game.class)  .toProvider(GameProvider.class) GameProvider new Game(... dieProvider.get()) 15.04.2011 Joel Krebs 10
  • 11. Improvements - there's always a way to code better  Use annotions that are provided by Guice  @named  @singelton  Merge modules  Merge providers 15.04.2011 Joel Krebs 11
  • 12. Conclusions - doesn't make it all more complicate?  Reduce dependencies in general improves your code in many ways  It needs a certain kind of mindset, but you get used to it  Frameworks like Guice can improve your code as well and may save you a lot of time  But I would not use them for too small projects 15.04.2011 Joel Krebs 12
  • 13. Questions - experiences, smart remarks, suggestions Joel Krebs joel.krebs@students.unibe.ch @jomikr on www.aleaiactaest.ch 15.04.2011 Joel Krebs 13