Does enums rewrite Java's open / closed principle?

In java, if we use an enumeration, and ultimately we want to add / remove an attribute to / from this enumeration, therefore, using it, we violate the open / closed principle in solid principles.

If so, what is the best way to use enums?

+4
source share
3 answers

The answer is no, since OCP does not apply (and cannot apply) to Enums. Enumerations must be complete (contain all possible values) and static (static = final, non-mutable). You can see them as a small finite set of Value objects.

-, , "- " , .

. , , , , , . ( ) , , . , , , , , ... :).

, , Enums OCP ( : D), OCP , , , , . Enum , , , , , . / . final. final OCP, ( ) .

, , Enums - Value. VO , OCP.

- , , OCP (Tapestry 5) , PITA . , , .

+7

. Java Open/Closed, .

" " Java , , enum, Open/Closed.

@MarkRotteveel: "Open/closed - , , ".

+2

Open/Closed, SOLID , . .

, - :

enum Fruits {apple, pear, banana}
enum Food extends Fruits {chicken}

:

1) Java .

2)

3) -

Enum , , , ( ). , .

0
source

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


All Articles