Convert enum to class and vice versa

We currently have a business object that can be represented as an enumeration or as a class.

Implementing a class is simpler and makes business logic more understandable. But there is a possibility that the requirements will change by 50%, and the presentation of the transfer will make our life easier.

Specific example

The entity has a title and color. Color is editable, so there are 2 ways

  • entity - this enumeration - there is another class with a mapping from an object to its color.
  • entity - this class is another vial for color, no problem.

Requirement for future change - there must be rules associated with each object

  • entity is an enumeration - rules are hardcoded in code
  • entity is a class - mapping requires several more classes, as well as a user interface that allows the user to specify them.

If the rule set is static, the second option is brute force.

So, if we need to convert the class to enum, are there any recommendations on this process?

EDIT

The set of objects is limited and is unlikely to be changed by the user.

Thanks in advance.

+4
source share
4 answers

If you need some functions from enumerations, and some of classes, you can use them:

public class ColoredTitle { private String color; private Title title; public ColoredTitle(String color, Title title) { this.color = color; this.title = title; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getHeading() { return title.heading; } enum Title { FRONT_PAGE("Front Page"), FOOTER_TITLE("Footer Title"); private String heading; Title(String title) { this.heading = title; } } } 
+1
source

Assuming Entity means JPA entities.

You can use enum to return to the outside world and inside the object, you can have a property that represents it.

 @Entity class Entity { @Column Integer ruleIndex = 0;//Illustration purpose only public Color getColor() { // ruturn color based on rule that will be applied by ruleindex } } enum Color { BLUE(0), BLACK(1), WHITE(2); private int ruleIndex = 0; private Color(int ruleIndex) { this.ruleIndex = ruleIndex; } } 

Update

It is not recommended to use enumerations as objects. You can use a single inheritance strategy instead

 @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="DISC", discriminatorType=STRING,length=20) public class Color {......} @Entity @DiscriminatorValue("Blue") public class Blue extends Color {......} @Entity @DiscriminatorValue("Green") public class Green extends Color {......} 

This allows you to store all the data in one table and allows you to also identify data based on objects.

+1
source

If it has something editable, you will need an entity class at some point, so go to the entity class first.

If you need to implement a fixed set of rules, implement them as an enumeration with hard-coded rules and add a field to your entity class that displays this enumeration.

You can list the enumeration on entities in this way:

 enum MyRule { RULE1, RULE2; // implement hard-coded rule } @Entity class Myentity { @Enumerated(/* optionally specify EnumType, default is ordinal */) @Column MyRule rule; } 
+1
source

Enumerations can have methods similar to regular classes.

 public enum Tree { FIR("Fir Tree"), BIRCH("Birch Tree"); private String desc; public Tree(String desc) { this.desc = desc; } public String getDesc() { return desc; } public String getRandom() { return "abc"; } } 
0
source

Source: https://habr.com/ru/post/1438829/


All Articles