of concerns of construction and use of objects ◦ Readability ◦ Reusability • Decreases coupling between a object and its dependency • Ease of refactoring
of concerns of construction and use of objects ◦ Readability ◦ Reusability • Decreases coupling between a object and its dependency • Ease of refactoring ◦ Smaller foused classes • Easy tests
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller() } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(???) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get<PaperBundle>()) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } single { Printer(???, ???) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } single { Printer(get(), get()) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } single { Printer(get(), get()) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single { Printer(get(), get()) } } val printerComponentsModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } } Kuala Lumpur
injected Printer instance val printer : Printer by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // ... } } Kuala Lumpur
injected Printer instance val printer : Printer by inject() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) printer.print() } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } single { Printer(get(), get()) } } Kuala Lumpur
Printer(private val paperBundle: PaperBundle, private val roller: Roller) val printerModule = module { single<PaperBundle> { PaperTray() } single<Roller> { PrinterRoller(get()) } single { Printer(lazy {get<PaperBundle>()}, get()) } } Kuala Lumpur
private val paperBundle by lazy { _paperBundle.get() } fun print() { paperBundle.getPaper() println("Printing.....") roller.rollPaper() } } * Lazy is dagger type Kuala Lumpur
private val paperBundle by lazy { _paperBundle.get() } fun print() { paperBundle.getPaper() println("Printing.....") roller.rollPaper() } } Kuala Lumpur
private val paperBundle by lazy { _paperBundle.get() } fun print() { paperBundle.getPaper() println("Printing.....") roller.rollPaper() } } Kuala Lumpur
private val paperBundle by lazy { _paperBundle.get() } fun print() { paperBundle.getPaper() println("Printing.....") roller.rollPaper() } } Kuala Lumpur
//create a session with id `ourSession` and qualifier `session` val ourSession = getKoin().createScope("ourSession",named("session")) //get session instance from scope val userSession : UserSession by ourSession.inject() //closing the scope ourSession.close()
{ Presenter() } } } Kuala Lumpur class MyActivity : AppCompatActivity() { // inject Presenter instance from current scope val presenter : Presenter by currentScope.inject()
ViewModel val detailViewModel: DetailViewModel by viewModel() // Eager inject ViewModel val detailViewModel: DetailViewModel = getViewModel() } Kuala Lumpur
printerViewModel by viewModel<PrinterViewModel>() } class PrinterHeaderFragment : Fragment() { private val weatherViewModel by sharedViewModel<PrinterViewModel>() } class PrinterListFragment : Fragment() { private val weatherViewModel by sharedViewModel<PrinterViewModel>() } Kuala Lumpur
private val paperBundle: PaperBundle by inject() @Test fun `check roll paper status before and after print`() { startKoin { modules(printerModule) } } } Kuala Lumpur
private val paperBundle: PaperBundle by inject() @Test fun `check roll paper status before and after print`() { startKoin { modules(printerModule) } declareMock<PaperBundle>() } } Kuala Lumpur
private val paperBundle: PaperBundle by inject() @Test fun `check roll paper status before and after print`() { startKoin { modules(printerModule) } declareMock<PaperBundle>() given(paperBundle.canRollPaper()).will { true } printer.print() } } Kuala Lumpur
private val paperBundle: PaperBundle by inject() @Test fun `check roll paper status before and after print`() { startKoin { modules(printerModule) } declareMock<PaperBundle>() given(paperBundle.canRollPaper()).will { true } printer.print() verify(paperBundle, times(1)).canRollPaper() verify(paperBundle, times(1)).setPaperRolled() } } Kuala Lumpur
private val paperBundle: PaperBundle by inject() @Test fun `check roll paper status before and after print`() { startKoin { modules(printerModule) } declareMock<PaperBundle>() given(paperBundle.canRollPaper()).will { true } printer.print() verify(paperBundle, times(1)).canRollPaper() verify(paperBundle, times(1)).setPaperRolled() } } Kuala Lumpur