开发者

Using Enums for Range Selection in OO Application

开发者 https://www.devze.com 2023-03-24 14:44 出处:网络
I am working on a project that has a large number of forms that users will be filling out to create a final report. A majority of these questions are multiple choice. I am struggling with the proper a

I am working on a project that has a large number of forms that users will be filling out to create a final report. A majority of these questions are multiple choice. I am struggling with the proper architecture to the classes within the application.

For sake of conversation lets say this is an auto accident report. An object-oriented approach would dictate that we create s开发者_如何学Pythontructures based on the information contained in each question. So we have classes such as "Vehicle", "Accident", etc. But what about a question such as "what speed was the vehicle moving upon collision" where users are presented with a choice of 4 answers?

  • Less than 10 mph
  • 11-30mph
  • 31-50mph
  • More than 50 mph

What is the correct way to store this information in a class? My first consideration was an enum:

enum CollisionSpeed {
   LessThan10,
   ElevenToThirty,
   ThirtyOneToFifty,
   MoreThanFifty
}

But that feels dirty as it is basing the data structure on the presentation. Also if down the road they change the ranges, we have to change the enum as well.

So the question is, how do you handle range selections like this with an object-oriented approach?

Thanks!


If it is really generic and you have a large number of questions, you could invastigate whether denormalization is the right approach. Instead of modeling each individual topic case by case, you could provide an abstraction layer, so that e.g. business users can add further questions without IT involvement later. Simplistic example (pseudo coded):

class QuestionEntity {
    String question;
    enum type; // e.g. open question (freetext), closed question (check boxes), etc.
    String[] values // if it's a closed question
}

As mentioned above, this is fairly simplified. As I do know your exact requirements, I cannot judge whether it'd make sense in your case. In fact it's about developing a DSL for your specific business use cases.

Maybe the answer is not as concrete as you expected, but it came to my mind when I read your question...

0

精彩评论

暂无评论...
验证码 换一张
取 消