I am currently coursing Computer Engineering and I remember a professor of a class called Introduction to Informational Systems saying that two classes related by a 1:1 cardinality does not make sense.
For example: I have the Client class and the Telephone class. Let’s supose that the client can only have one phone. The professor said that does not make sense creating the Telephone class, and telephone should be an attribute of the Client class. I absolutely agree with him.
But now I’m taking the Software Engineering class and the professor (not the same) did not make any comments about this issue, and now I’m really confused about this.
What is the correct approach?
I would say your Introduction to Information Systems professor was correct. And your SE professor, too (assuming his lack of comments makes him a contrarian). They are each right depending on your requirements and the domain you’re working with. But without any other details, it’s hard to model this for you, and I would lean towards what your CE professor had said. Keep in mind all those fun little principles you learned: KISS, DRY, etc., and apply them to your problem.
If
Clientwill never ever possibly have more than one telephone number and no other entity in your domain needs a telephone number, then a separateTelephoneclass isn’t necessary. In the real world, if your requirements are vague, find out more information from your client.If somebody down the road decides
Clients can take on more than one telephone number, or another entity is introduced into your domain that needs a telephone number, this is a fairly easy refactoring to accomplish.So with that in mind, let’s say your
Clienthad a separateAddressclass that included the telephone number instead. Maybe thatAddressclass gets re-used by another class, maybeInvoiceorShipment, where anAddresscould be shared or applied in both cases. In this example, you might wantAddress(Telephone) to be its own class.In your example,
Telephonemight be a little too contrived. You’d want it to be a separate class for re-use if it had many properties (AreaCode,InternationalPrefix,Number, etc.), but ifClientjust needed a string-value calledTelephonethat a user would be typing in merely for reference, then it probably doesn’t make sense to be its own class.