Best object-oriented way to solve the problem

Asked

Viewed 100 times

1

Suppose I am modeling a Paycheck. However, each entry in a Paycheck I chose to model as a ItemContracheque. That is, to represent my Paycheck as a whole, I can use a simple list of ItemContracheque.

Then came the doubt, if only a list of ItemContracheque can represent an entire Paycheck, since it has no additional attribute, I still need to create this Paycheck object that would have only one attribute (a list of ItemContracheque)?

What would be the most OO practice to adopt here? Create this object Counter check with only one attribute or represent a Paycheck using only a list of ItemContracheque? And the main thing, why choose one over the other?

This modeling will be used in the return of one of the webservice methods I am making.

With Java codes to improve the issue:

public class Contracheque {
    private List<ItemContracheque> itensContracheque;
}

or just use:

List<ItemContracheque> itensContracheque;

That is, it would define the return of my method: or an object Contracheque or a List<ItemContracheque>.

1 answer

3


I don’t think this has anything to do with OOP directly, it’s just a form of modeling.

If you have an object that is a paycheck then you should have a type just to indicate it. Conceptually this is correct. And the items must be composed within that object (shall inherit nothing, as I have already answered in a recent question). It is correct for obvious reasons, because if the concept says there is such an object, it must be created. No matter what’s inside him, no matter how big.

Now, to say that you only have the items seems to have a modeling error there. That’s virtually impossible. Sure, you can define it any way you want, but objects aren’t usually that simple. And as far as I know a paycheck, and I’ve worked with payroll, it’s composed of several attributes. Does this paycheck have no number? Competence? Who does it belong to? Total value, among other values?

  • Thanks for the answer. I get it! On your questions: yes, there may even be other attributes, but to supply the need for what I need to do, they are not necessary. So you don’t have to put them on the object.

  • I still think it’s unlikely, but if you say...

  • Hahaha How much skepticism! I will accept yours as an answer, it was very satisfactory. Thanks for the help, bigown! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.