Giter Club home page Giter Club logo

dagger2test's Introduction

a) What does Dagger do? Explain in words that an high-school Java programmer can understand.

Dagger is little design pattern.It use to simplifie complexcity of code with structured data. it make code lossley coupled which help to testing the code. Dagger has uses following annotations:

1) @Module and @Provides: define classes and methods which provide dependencies
2) @inject: request dependencies. Can be used on a constructor, a field, or a method
3) @Component: enable selected modules and used for performing dependency injection
4) @Singleton: Single instance of this provided object is created and shared.

b) Why do we need DI?

Dependancy injection is a software design pattern that implements object oriented programming for resolving dependencies. Dependency injection in application allow us to have Testable classes, Re-usable and interchangeable Components.


<b> without DI </b>

class CodffeeMaker{

private final Heater heater;
private final Pump pump

        coffeMaker()  {
            this.heate = new ElectricHeater();
             this.pump = new  Thermosiphon(heater);
        }
      
  coffee makeCoffee(){/* _ */}

}

class CoffeeMain{

      public static void main(String[] args){
      Coffee coffee = new  CoffeeMaker().makeCoffee();
    
    }

}

With Manual Di

class CoffeMaker{

private final Heater heater;
private final Pump pump;

CoffeeMaker(Heater heater,Pump pump){

      this.heater = checkNotNull(heater);
      this.pump = checkNOtNull(pump);
    
}

Coffee makeCoffee(){/* _ */}

}

class CoffeMain{

    public static void main(String[] args){
  
	      Heater heater = new ElectricHeater();
	      Pump pump = new Thermosiphon(heater);
	      Coffee coffee = new  CoffeeMaker(heater,pump).makeCoffee();
      
    }

}

c)How is Dagger implemented?

- We need to identify the dependent objects and its dependencies.
- Create a class with the @Module annotation, using the @Provides annotation for every method that returns a dependency.
- Request dependencies in your dependent objects using the @Inject annotation.
- Create an interface using the @Component annotation and add the classes with the @Module annotation created in the second step.
- Create an object of the @Component interface to instantiate the dependent object with its dependencies.

dagger2test's People

Contributors

androidvinay avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.