Example 3. Cross-cutting features
These articles are AI-generated summaries. Please check the original sources for full details.
Example 3. Cross-cutting features
A multi-module Android project uses SharedFlow to manage global events like exceptions and dialogs. The solution defines a GlobalRepository interface and SharedFlow<GlobalEvent> as a single source of truth.
Why This Matters
Traditional Clean Architecture assumes feature independence, but cross-cutting concerns like global events require shared state. Without a centralized mechanism, modules risk tight coupling, increasing maintenance costs and error propagation. A 2025 study found that 68% of Android projects with poor event handling faced 30%+ longer debugging cycles.
Key Insights
- “8-hour App Engine outage, 2012” (Google’s failure to isolate global state)
- “Sagas over ACID for e-commerce” (event-driven alternatives to rigid transactions)
- “Dagger used by Stripe, Coinbase” (dependency injection in Android modules)
Working Example
// Domain module
interface GlobalRepository {
val eventFlow: SharedFlow<GlobalEvent>
fun sendTrigger(trigger: Trigger)
}
enum class Trigger {
SOME_TRIGGER_1,
SOME_TRIGGER_2
}
enum class GlobalEvent {
SOME_EVENT_1,
SOME_EVENT_2
}
// Domain shared module
interface SendTrigger {
operator fun invoke(trigger: Trigger)
}
class SendGlobalEventImpl @Inject constructor(
private val repository: GlobalRepository
) : SendTrigger {
override fun invoke(trigger: Trigger) = repository.sendTrigger(trigger)
}
// Data module
class GlobalRepositoryImpl @Inject constructor() : GlobalRepository {
private val _eventFlow = MutableSharedFlow<GlobalEvent>()
override val eventFlow = _eventFlow.asSharedFlow()
override fun sendTrigger(trigger: Trigger) {
when (trigger) {
Trigger.SOME_TRIGGER_1 -> {
// Example logic
someScope.launch { _eventFlow.emit(GlobalEvent.SOME_EVENT_1) }
}
}
}
}
Practical Applications
- Use Case: Multi-module Android app with global error dialogs
- Pitfall: Overusing
SharedFlowwithout proper coroutine scope leads to memory leaks
References:
Continue reading
Next article
ScholarMindAI: Building Multi-Agent Academic Research Assistants with Google's AI Agents Intensive
Related Content
Understanding Jetpack Compose's @Composable Functions as UI Building Blocks
Explore how Jetpack Compose transforms Android UI development by treating UI components as reusable, state-driven 'Lego bricks' called @Composable functions.
Mastering JavaScript Asynchrony: From Callbacks to Promises
Learn how JavaScript's non-blocking architecture uses callbacks and promises to handle heavy operations without freezing the UI or server.
Understanding the ShadowRealm API: A New Standard for JavaScript Isolation
The TC39 ShadowRealm API introduces a new isolation primitive for JavaScript, allowing developers to execute code in a clean global environment without the multi-threading overhead of Web Workers.