Skip to content

Commit

Permalink
#7 Two commands for the price of one
Browse files Browse the repository at this point in the history
  • Loading branch information
mustofa-id committed Jan 7, 2020
1 parent 4738049 commit 81957c6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
@@ -1,6 +1,7 @@
package id.mustofa.atm.factory

import dagger.Component
import id.mustofa.atm.module.HelloWorldModule
import id.mustofa.atm.module.LoginCommandModule
import id.mustofa.atm.module.SystemOutModule
import id.mustofa.atm.router.CommandRouter
Expand All @@ -12,7 +13,13 @@ import id.mustofa.atm.router.CommandRouter
* we can annotate it with @Component to have Dagger generate an implementation
* for us: DaggerCommandRouterFactory.
*/
@Component(modules = [LoginCommandModule::class, SystemOutModule::class])
@Component(
modules = [
HelloWorldModule::class,
LoginCommandModule::class,
SystemOutModule::class
]
)
interface CommandRouterFactory {

// CommandRouter constructor should annotated with @Inject
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/id/mustofa/atm/module/HelloWorldModule.kt
Expand Up @@ -2,12 +2,16 @@ package id.mustofa.atm.module

import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
import dagger.multibindings.StringKey
import id.mustofa.atm.model.HelloWorldCommand
import id.mustofa.atm.model.base.Command

@Module
abstract class HelloWorldModule {

@Binds
@IntoMap
@StringKey("hello")
abstract fun helloWorldCommand(command: HelloWorldCommand): Command
}
4 changes: 4 additions & 0 deletions src/main/kotlin/id/mustofa/atm/module/LoginCommandModule.kt
Expand Up @@ -2,12 +2,16 @@ package id.mustofa.atm.module

import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
import dagger.multibindings.StringKey
import id.mustofa.atm.model.LoginCommand
import id.mustofa.atm.model.base.Command

@Module
abstract class LoginCommandModule {

@Binds
@IntoMap
@StringKey("login")
abstract fun loginCommand(command: LoginCommand): Command
}
10 changes: 3 additions & 7 deletions src/main/kotlin/id/mustofa/atm/router/CommandRouter.kt
Expand Up @@ -11,13 +11,9 @@ import javax.inject.Inject
* The @Inject annotation indicates to Dagger that when we ask for a
* CommandRouter, Dagger should call new CommandRouter().
*/
class CommandRouter @Inject constructor(command: Command) {

private val commands = mutableMapOf<String, Command>()

init {
commands[command.key()] = command
}
class CommandRouter @Inject constructor(
private val commands: Map<String, @JvmSuppressWildcards Command>
) {

fun route(input: String): Status {
val inputs = split(input)
Expand Down

0 comments on commit 81957c6

Please sign in to comment.