vendredi 30 juin 2023

How to handle different input type and single output type in a single function in android kotlin

I have two enum classes and data class as below :

NamesEnum.kt

enum class NamesEnum(
    val option : String
) {
    AAkASH("Aakash Gupta"),
    ADITI("Aditi Deshpande")
}

StateEnum.kt

enum class StateEnum(
    val option : String
) {
    KAR("Karnataka"),
    MAH("Maharashtra")
}

StudentData.kt


data class StudentData(
    var student1: String = "",
    var student2: String = "",
    var state1: String = "",
    var state2: String = "",
   
){
    companion object{
        private var EMPTY_OBJECT: StudentData? = null
        fun getInstance(): StudentData? {
            if (EMPTY_OBJECT == null) EMPTY_OBJECT = StudentData()
            return EMPTY_OBJECT
        }
        fun clearReference(){
            EMPTY_OBJECT = null
        }
    }
}

I have to go through each enum classes and update the data in the data class:


fun updateData() : List<String>
{
  val list = NamesEnum.values()
        val nameList = ArrayList<String>()
        for (item in list){
            nameList.add(item.option)
        }
        return optionList
}

I want the same functionality as in updateData() function but without repeating the logic for every Enum class. Which design pattern do I have to choose to implement this?

Aucun commentaire:

Enregistrer un commentaire