Blog Infos
Author
Published
Topics
Published
Topics
photo created using Microsoft Designer

 

Introduction: In Kotlin, sealed classes and sealed interfaces are powerful tools for defining restricted hierarchies of types. In Android development, understanding when to use each can greatly enhance the structure and maintainability of your code. Let’s dive into practical examples to help you make informed decisions

Sealed Class: Managing State in Android UI

Use Case: Representing a closed hierarchy of states, such as UI states or network request states.

Practical Implementation:

sealed class ViewState {
    object Loading : ViewState()
    data class Success(val data: List<Item>) : ViewState()
    data class Error(val message: String) : ViewState()
}

// Usage example:
fun renderState(viewState: ViewState) {
    when (viewState) {
        is ViewState.Loading -> showLoading()
        is ViewState.Success -> showData(viewState.data)
        is ViewState.Error -> showError(viewState.message)
    }
}

When to Choose: Use sealed classes when you have a fixed set of related states or types that require exhaustive handling, such as UI states or network request states.

Job Offers

Job Offers


    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

No results found.

Jobs

Sealed Interface: Defining Behavior Across Unrelated Types

Use Case: Defining a closed set of types that implement certain behavior.

Practical Implementation:

sealed interface Loggable {
    fun log()
}

class FileLogger : Loggable {
    override fun log() {
        // Implementation for file logging
    }
}

class DatabaseLogger : Loggable {
    override fun log() {
        // Implementation for database logging
    }
}

// Usage example:
fun logAll(loggables: List<Loggable>) {
    loggables.forEach { it.log() }
}

When to Choose: Use sealed interfaces when you want to define a closed set of types that share common behavior but are otherwise unrelated, such as different types of loggers.

Conclusion: In Android development, choosing between sealed classes and sealed interfaces depends on the specific requirements of your project. Sealed classes are ideal for managing state in UI components, while sealed interfaces are useful for defining shared behavior across unrelated types. By understanding their practical implementations, you can make informed decisions to enhance the structure and maintainability of your Android codebase.

Share your experiences with sealed classes and interfaces in Android development!

About the author: Muhammad Mohsan is a seasoned Android developer with a decade of expertise in crafting innovative Android applications. He is deeply passionate about building robust Android apps and has a rich portfolio of projects. Connect with him on GitHub or LinkedIn to explore his work further.

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu