Class that one of his members is object

50 Views Asked by At

Lets say I have class called Connection. One of his members is object that is instance of another class, class VerificationCode.

The class VerificationCode has 2 members: code and expiresAt.

Should I keep the VerificationCode and associate it with the member, or should I associate the 2 members code and expiresAt directly to the Connection class?

What are the advantages or disadvantages in terms of design/modeling?

Example:

Option 1:

Class VerificationCode{
code: string;
expiresAt: number;
}

Class Connection{
a: string;
verificationCode: VerificationCode;
}

Option 2:

Class Connection{
a: string;
code: string;
expiresAt: number;
}
1

There are 1 best solutions below

0
StepUp On

Option 1 is better for me. Because it has:

  • great separation of concerns. Now you have just two fields such as code and expiresAt, but when you will have methods for this variables, then it would be violation of Single Responsibility principle if you will edit code and expiresAt in class Connection
  • has high cohesion in classes. Read more what cohesion is here.