Basic Design Question
-
Hello, i have a (probably) very basic design question. I need to create a Person Object which holds information like Name, Email, Age, and Gender. The problem is the Gender information. There can only be "Man" and "Woman". Soo i could either create a Man and Woman object which inherits from the Person object, or i could maybe store the information as an int, boolean or maybe even better as an Enumeration ? But i cant decide which is the "correct" way to do it. The Man and Woman object will be completly identical except for their type (how they are declared - new Man() and new Woman() ..). Or should I in this case go with the other option and store the information as an int/boolean/Enum in the Person object ? And is that how it is done when you have a value than can only be a certain different set of values ? Or something completly else ? There is no really difference as far as i can see, as i will either have to do checks like - if (myObj.Gender == "Man"), or if (myObj is "Man") of course i could do some overloading of the methods soo i dont even have the if else - like HandleThis(Man obj) and HandleThis(Woman obj). Maybe for the scalability, escpecially if it had been other information i had to store which could change by time it would probably be best to go with the inhertiance example. or maybe i should always go with that solution ? To some of you this might be a stupid question, but i cant really decide. Soo i hope someone can help me to tell me what is the "correct" way to do it and why. Martin Happy New Year everyone ! :)
-
Hello, i have a (probably) very basic design question. I need to create a Person Object which holds information like Name, Email, Age, and Gender. The problem is the Gender information. There can only be "Man" and "Woman". Soo i could either create a Man and Woman object which inherits from the Person object, or i could maybe store the information as an int, boolean or maybe even better as an Enumeration ? But i cant decide which is the "correct" way to do it. The Man and Woman object will be completly identical except for their type (how they are declared - new Man() and new Woman() ..). Or should I in this case go with the other option and store the information as an int/boolean/Enum in the Person object ? And is that how it is done when you have a value than can only be a certain different set of values ? Or something completly else ? There is no really difference as far as i can see, as i will either have to do checks like - if (myObj.Gender == "Man"), or if (myObj is "Man") of course i could do some overloading of the methods soo i dont even have the if else - like HandleThis(Man obj) and HandleThis(Woman obj). Maybe for the scalability, escpecially if it had been other information i had to store which could change by time it would probably be best to go with the inhertiance example. or maybe i should always go with that solution ? To some of you this might be a stupid question, but i cant really decide. Soo i hope someone can help me to tell me what is the "correct" way to do it and why. Martin Happy New Year everyone ! :)
just use and enumeration, inheritance from a person object would be too much normalization...its very unlikely humanity will converge into anything other than a man or woman, so a simple enumeration would be perfect. You might consider not having a default constructor and instead have a consturcture that requires a gender initialization.
normailization is good, but there is always to much of a good thing... -
Hello, i have a (probably) very basic design question. I need to create a Person Object which holds information like Name, Email, Age, and Gender. The problem is the Gender information. There can only be "Man" and "Woman". Soo i could either create a Man and Woman object which inherits from the Person object, or i could maybe store the information as an int, boolean or maybe even better as an Enumeration ? But i cant decide which is the "correct" way to do it. The Man and Woman object will be completly identical except for their type (how they are declared - new Man() and new Woman() ..). Or should I in this case go with the other option and store the information as an int/boolean/Enum in the Person object ? And is that how it is done when you have a value than can only be a certain different set of values ? Or something completly else ? There is no really difference as far as i can see, as i will either have to do checks like - if (myObj.Gender == "Man"), or if (myObj is "Man") of course i could do some overloading of the methods soo i dont even have the if else - like HandleThis(Man obj) and HandleThis(Woman obj). Maybe for the scalability, escpecially if it had been other information i had to store which could change by time it would probably be best to go with the inhertiance example. or maybe i should always go with that solution ? To some of you this might be a stupid question, but i cant really decide. Soo i hope someone can help me to tell me what is the "correct" way to do it and why. Martin Happy New Year everyone ! :)
I'd think you need to look to whether the Gender object will have any responsibility. If you are handling any methods such as: if (myObj.Gender == "Man"), or if (myObj is "Man") you might want want to utilize the state or strategy patterns (GoF) instead. http://ootips.org/[^] or http://www.dofactory.com/Patterns/Patterns.aspx[^] ed ~"Watch your thoughts; they become your words. Watch your words they become your actions. Watch your actions; they become your habits. Watch your habits; they become your character. Watch your character; it becomes your destiny." -Frank Outlaw.