Skip to content

Instantly share code, notes, and snippets.

@josphatmwania
Last active June 22, 2023 09:16
Show Gist options
  • Select an option

  • Save josphatmwania/936794a5a0f52e21bfe3bf60fe3487c3 to your computer and use it in GitHub Desktop.

Select an option

Save josphatmwania/936794a5a0f52e21bfe3bf60fe3487c3 to your computer and use it in GitHub Desktop.
Polymorphism and abstract class difference and their implementation
## The key idea behind polymorphism is that objects of different classes can be treated as objects of a common superclass or interface.
##This enables you to write code that can work with objects of different types, as long as they adhere to the defined interface or inheritance hierarchy.
## EXAMPLE
You might have a base class or interface called "Meal" that defines common properties and methods for different types of meals, such as breakfast, lunch, and dinner.
Each specific meal type would be a derived class that inherits from the "Meal" base class or implements the "Meal" interface.
## Abstraction is a concept that focuses on hiding unnecessary details and complexity while providing a simplified view of an object or system.
## For example, you might have an abstract "Repository" class that defines methods for fetching data from the TMDB API, filtering meals, and providing meal plans.
##Concrete subclasses would then implement these methods based on the specific implementation details, such as making network requests, parsing responses, and caching data.
# Data layer
interface MealDataSource {
suspend fun fetchMeals(): List<Meal>
suspend fun fetchFilters(): List<Filter>
suspend fun fetchMealPlans(): List<MealPlan>
}
class RemoteMealDataSource : MealDataSource {
override suspend fun fetchMeals(): List<Meal> {
// Make network request to TMDB API and retrieve meals
// Parse the response and return a list of Meal objects
}
override suspend fun fetchFilters(): List<Filter> {
// Make network request to TMDB API and retrieve filters
// Parse the response and return a list of Filter objects
}
override suspend fun fetchMealPlans(): List<MealPlan> {
// Make network request to TMDB API and retrieve meal plans
// Parse the response and return a list of MealPlan objects
}
}
class LocalMealDataSource : MealDataSource {
override suspend fun fetchMeals(): List<Meal> {
// Fetch meals from local cache and return
}
override suspend fun fetchFilters(): List<Filter> {
// Fetch filters from local cache and return
}
override suspend fun fetchMealPlans(): List<MealPlan> {
// Fetch meal plans from local cache and return
}
}
# Presentation
class MealViewModel(private val mealDataSource: MealDataSource) : ViewModel() {
private val _meals = mutableStateOf<List<Meal>>(emptyList())
val meals: State<List<Meal>> get() = _meals
private val _filters = mutableStateOf<List<Filter>>(emptyList())
val filters: State<List<Filter>> get() = _filters
private val _mealPlans = mutableStateOf<List<MealPlan>>(emptyList())
val mealPlans: State<List<MealPlan>> get() = _mealPlans
fun fetchMeals() {
viewModelScope.launch {
_meals.value = mealDataSource.fetchMeals()
}
}
fun fetchFilters() {
viewModelScope.launch {
_filters.value = mealDataSource.fetchFilters()
}
}
fun fetchMealPlans() {
viewModelScope.launch {
_mealPlans.value = mealDataSource.fetchMealPlans()
}
}
}
## Caching for bookmarked meals
class RemoteMealDataSource(private val localDataSource: LocalMealDataSource) : MealDataSource {
override suspend fun fetchMeals(): List<Meal> {
// Check if meals are available in the local cache
val cachedMeals = localDataSource.fetchMeals()
if (cachedMeals.isNotEmpty()) {
return cachedMeals
}
// If not available, make network request to TMDB API and retrieve meals
// Parse the response and save the meals to local cache
val remoteMeals = // fetch meals from the network
localDataSource.saveMeals(remoteMeals)
return remoteMeals
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment