Enums VS Classes VS Interfaces

后端 未结 3 1311
梦如初夏
梦如初夏 2020-12-09 10:42

I have been reading a lot of posts on this site regarding the usage of constants.

Question: When should I use Enums for constants, vs using classes or interfaces. <

3条回答
  •  温柔的废话
    2020-12-09 11:05

    It sounds like almost everything you've listed for both numbers 1 and 2 belong in configuration files or database tables.

    Do you want to re-compile code when your employee's get a raise or a page name changes?

    Unless there is a compelling reason everything else that is constant should be modeled as an enum. This way you realize the benefits of fast object equality comparisons and you avoid problems associated String constants.

    The scope of those enums however is application specific. If the enumeration is only used by a class that it should be a private enum. If it is shared by multiple classes then it should be in its own class definition file.

提交回复
热议问题