mercredi 22 avril 2020

Design to build object based on external features

I was wondering on how to implement the following. 

I want to be able to build an object of class X and dynamically choose which fields to set based on a list of enum values. I have an object class with let’s say 10 instance variables, let’s call them field 1 to field 10. 

In addition I have an enum called Y with let’s say 5 values, enum 1 to enum 5. Each enum value should correspond 0, 1 or more fields. I’m not sure how to store this latter part.

For example, enum Y:

enum value 1 -> field 1,2,3

enum value 2 -> field 1,4,5

enum value 3 -> none

enum value 4 -> field 3, 6, 8

enum value 5 -> field 10

Now for every object of class X that I want to create, I will receive a list of enum values of Y, where each value corresponds to the fields. 

For example, I create an object and receive a list of enum value 1, 3, 5. This means I would have to only set the fields 1, 2, 3, 10 for the object of class X.

How to design something like this and make it flexible, so that for example I can change the fields that need to be set per enum value easily? Are there any design patterns that can be used for this?

Aucun commentaire:

Enregistrer un commentaire