This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketMachineTest { | |
private val printerMock = mockk<Printer>(relaxed = true) | |
@Test | |
fun `given ticket machine when adult ticket selected and paid then ticket printed`() { | |
val ticketMachine = TicketMachine(printerMock, Display(), CoinCounter()) | |
ticketMachine.selectTicket(TicketType.ADULT) | |
ticketMachine.insertCoin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class TicketMachineModule { | |
@Provides | |
@Singleton | |
fun printer(display: Display) = Printer(display) | |
@Singleton | |
@Provides | |
fun display(): Display = Display() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketMachine @Inject constructor( | |
private val printer: Printer, | |
private val display: Display, | |
private val coinCounter: CoinCounter, | |
) { | |
private var selectedTicketType: TicketType? = null | |
init { | |
display.show("Select ticket:") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class TicketType(val coins: Int) { | |
ADULT(2), | |
CHILD(1), | |
} | |
class Printer( | |
private val display: Display, | |
) { | |
fun printTicket(selectedTicketType: TicketType) { | |
display.show("Ticket printed: ${selectedTicketType.name}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Subcomponent( | |
modules = [HomeFragmentModule::class] | |
) | |
interface HomeFragmentComponent { | |
fun inject(homeFragment: HomeFragment) | |
} | |
@Module | |
class HomeFragmentModule(private val homeFragment: HomeFragment) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeFragment : BaseFragment(), HomeView { | |
@Inject lateinit var homeAdapter: HomeAdapter // :home | |
@Inject lateinit var sharedPreferences: SharedPreferences // :core | |
@Inject lateinit var apiService: ApiService // :network | |
@Inject lateinit var dummyRepository: DummyRepository // :home - constructor provided | |
@Inject lateinit var homeDummyRepository: HomeDummyRepository // :home - component provided | |
override fun inject() { | |
homeComponent.plus(HomeFragmentModule(this)).inject(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseFragment : Fragment() { | |
private object ModuleComponent : SingletonHolder<HomeComponent, Context>({ context -> | |
DaggerHomeComponent | |
.factory() | |
.create(CoreComponentHolder.getInstance(context.applicationContext as App)) | |
}) | |
val homeComponent: HomeComponent | |
get() = ModuleComponent.getInstance(requireContext()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HomeScope | |
@Component( | |
modules = [ | |
HomeModule::class | |
], | |
dependencies = [ | |
CoreComponent::class | |
] | |
) | |
interface HomeComponent { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class SingletonHolder<out T, in A>(private val constructor: (A) -> T) { | |
@Volatile private var instance: T? = null | |
fun getInstance(arg: A): T { | |
return when { | |
instance != null -> instance!! | |
else -> synchronized(this) { | |
if (instance == null) { | |
instance = constructor(arg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CoreComponentHolder { | |
private object Component : SingletonHolder<CoreComponent, Application>({ application -> | |
DaggerCoreComponent.factory().create(application) | |
}) | |
fun getInstance(application: Application): CoreComponent { | |
return Component.getInstance(application) | |
} | |
} |