Where it’s good to use Enum?
The idea of using an Enum applies in the following cases:
It is a fixed set of elements. None can be created or destroyed.
The elements are immutable. That is, their contents never change, the internal values of each element are fixed.
They correspond to several distinct values with similar behaviors among themselves, only representing different values within a group. It is not convenient to use polymorphism.
These are good examples of enums:
Playing cards in a poker game: gold, hearts, clubs and swords.
Days of the week: Sunday, Monday, Tuesday, ..., Saturday.
Months of the year: January, February, March, April, ..., December.
Types of triangles: equilateral, isosceles or scalene.
Sex of a person: female or male.
They are good examples because no one can create or destroy a month, a day of the week or a deck of cards in a poker game (if they could, it would no longer be poker). Also, all of these elements have the same behavior, and unless you’re making an application with very peculiar rules or gambiarradas, there’s no point in talking about polymorphism or specific behaviors in these cases, as March has no specific methods that do not appear in the month of June and neither methods that appear in both, but with completely different implementations.
Where it’s good to use several related classes?
Already these here are not good examples of Enum and are better modeled as classes:
Animal: cat, dog, parrot, monkey, duck...
Geometric shapes: square, triangle, rhombus, ellipse...
The reason is that they are polymorphic. Because each figure has a different way of calculating the area and perimeter, in addition to having different internal attributes such as radius, side length, angle values, etc. They may also have different specific methods, because the ellipse will have the method getSemieixoMaior()
that has no and does not make sense to have in the triangle.
Each animal has a different behavior, some walk, others run, others fly, others swim, others sing (specific methods) and even two different animals that sing, sing in very different ways (polymorphism). In addition there will always be a new type of animal to include (i.e., it is not a fixed list).
Where it is best to use a single class with different field/attribute values?
These are better modeled with a single class that is not an Enum:
Musical styles: rock, samba, Axé, reggae, jazz, gospel...
List of cities.
List of storage deposits.
The reason is that each object is a new instance, but there is no polymorphism involved. From time to time they invent a new musical genre, and therefore it would be better to register them all in a database, a text file or csv, or even just instantiate them at some point of the execution and put them in a list (it is not a fixed list). The same goes for the list of cities, because cities change names, can be dismembered by neighborhood emancipation or merged by geopolitical reforms, new cities can be founded.
As for the list of storage depots, this one includes because it’s a real case I saw eight years ago. The company had deposits in four different cities and the Pogramador that made the system originally had the brilliant idea of putting them on an Enum. Guess what happened when they opened a new deposit?
Where it is best to define different attributes/fields in the same class?
These here are simply several attributes of the same class:
Characteristics an individual can have - weight, age, height...
Amounts descriptive of a payment order - name of the beneficiary, value of payment, date of payment...
In each of these cases, all elements contain a value within each instance of a given object type. The purpose of the values denoted by these elements is to describe and characterize the instance in which they are present.
The 4 cases we have
Well, based on that, then we have four cases:
Enum - Use when values are fixed and immutable and have no polymorphism.
A normal class with several different instances - Use when there is no polymorphism, but values are mutable or new instances can be created or existing instances destroyed.
Multiple classes with a common superclass or interface - Use when polymorphism.
Different getters/instance properties in a class - Use when it comes to distinct characteristics of the same thing, each with a different value. You store the basic values in attributes/fields and make them available through getters/properties. In cases where you have attributes whose values depend on other attributes (e.g., diameter and radius), the getter may have to calculate the value instead of just returning it directly from the field/attribute.
Your case
And now let’s look at your specific case:
Race: You may want to invent new races in the future and there is polymorphism involved, so use superclass or interface and each race has its specific class. If there is no polymorphism and it is only the case of different values for the fields/attributes, use the same class, create the necessary instances and add them to a list or a Map. I recommend using the design pattern Strategy.
Character class: You may want to invent new classes in the future and there is polymorphism involved (at least to model the gift), so use superclass or interface and each character class has its specific C# class. If there is no polymorphism and it is only the case of different values for the fields/attributes, use the same class of C#, create the necessary instances and add them to a list or a Map. The fact that a character can change class suggests the use of the design pattern state.
Attributes: I think these are the attributes/fields/getters/properties of your character. It is the set of distinct values that describe their characters.
Status effects: You may want to invent new ones anytime. Also, you are very likely to have polymorphism, so use superclass or interface and each effect has its specific class.
Did you really mean
Ladino
? Or would it bePaladino
? Besides, I think thatÍndice de Armadura
is an attribute, not a status effect.– Victor Stafusa
@Victorstafusa Yes, I meant
Ladino
.Índice de Armadura pode ser um efeito de status
like, for example, when you take a potion or use a magic to temporarily augment your armor. See the description of a rogue here http://rpgromaduke.weebly.com/classes-e-raccedilas.html– Michael Pacheco