Should we always favor polymorphism over enums?

前端 未结 6 581
北荒
北荒 2021-01-31 18:59

After watching: The Clean Code Talks -- Inheritance, Polymorphism, & Testing

I checked my code and noticed a few switch statements can be refactored into polymorphis

6条回答
  •  萌比男神i
    2021-01-31 19:20

    I think Enums are useful when values are known & a few in number.
    Also, Enums are named constants in a way.

    Enums don't have any other state stored with it (except the value of it).
    Polymorphism would be helpful when you have different states of the conditions (and conditions require more state than relying in a single variable).

提交回复
热议问题