mercredi 22 avril 2020

Design pattern to set certain fields in object based on enum values

So basically what I want to do is fill/not fill certain fields in an object based off a list of enums.

For example the object in question:

case class Car(brand: String,
              model: String,
              wheels: Int,
              airbags: Boolean,
              seats: Int,
               cruiseControl: Boolean,
               photo: String,
               parkingAssist: Boolean)

This is the full object, and depending on list of Java enum types I want to set certain fields. Also combinations are possible and they might overlap.

enum CarType {
  BASIC_CAR,
  LUXURY_CAR,
  SAFE_CAR,
  PICTURE_CAR
}

So basically depending on the CarType I would like to set certain fields of Car. For example:

BASIC_CAR need to se brand, model, wheels and seats.

LUXURY_CAR need to set brand, model, cruiseControl and parkingAssist

SAFE_CAR need to set airbags

PICTURE_CAR need to set parkingAssist

So I will receive a list of these enums and then need to create the object. For example I will have one object with BASIC_CAR, LUXURY_CAR, SAFE_CAR which means the object I create will have to have all fields except parkingAssist.

So in the end, the enums relate to certain field and basically on the (combination of) enums I require to set certain fields in the object. What would be a good design for this?

Aucun commentaire:

Enregistrer un commentaire