Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help with classes modeling
    primarykey
    data
    text
    <p>I've being studying some OO concepts, like design patterns, clean code and some other stuff and i still have some doubts about how to proceed. For instance, lets take a look at my example.</p> <p>I have a Person class that is a model. I want to add some validations to a person, like check if the age corresponds with the birth date and check if the name contains valid characters.</p> <p>I have two approachs, but i dont know wich one i should use.</p> <p>Approach one: I create a new class called: </p> <pre><code>class ValidatePerson {} </code></pre> <p>and the the class have the methods: "validateAge()" and "validateName()" and every vallidation that i need i will have to implement a new method.</p> <p>Approach two: I create a abstract class called: ValidatePerson {} that will have some commum methods to all validation and the i would have:</p> <pre><code>class ValidatePersonAge extends ValidatePerson { validate();} class ValidatePersonName extends ValidatePerson {validate();} </code></pre> <p>I want to choose for approach two, cause every new validation rule that i will have to add will be a new class and wont affect the others. Like, if i need to alter something in the commum method that the new rule needs, i could just overwrite it. On the first approach, i would have to add another method and then create another method or alter the one thats already working for the others, that could make then crash.</p> <p>The thing is that im kind of confused to all this, since im new to programming and i would like to see some help and explanation about it. I've also read that classes should be closed for changing but open for expanding (or something like this).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload