Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which class design is better? [closed]

Which class design is better and why?

public class User {     public String UserName;     public String Password;     public String FirstName;     public String LastName; }  public class Employee : User {     public String EmployeeId;     public String EmployeeCode;     public String DepartmentId; }  public class Member : User {     public String MemberId;     public String JoinDate;     public String ExpiryDate; } 

OR

public class User {     public String UserId;     public String UserName;     public String Password;     public String FirstName;     public String LastName; }  public class Employee {     public User UserInfo;     public String EmployeeId;     public String EmployeeCode;     public String DepartmentId; }  public class Member {     public User UserInfo;     public String MemberId;     public String JoinDate;     public String ExpiryDate; } 
like image 751
Ramesh Soni Avatar asked Sep 02 '08 04:09

Ramesh Soni


People also ask

Why should the open-closed principle be used when designing a class?

It means that you should put new code in new classes/modules. Existing code should be modified only for bug fixing. New classes can reuse existing code via inheritance. Open/closed principle is intended to mitigate risk when introducing new functionality.

What are the principles of class design?

Class Design Principles do not aim for code that works, but for code that can efficiently be worked on! A class (*) should have only one reason to change. Class PersonData is responsible for knowing the data of a person. Class CarFactory is responsible for creating Car objects.

What is the most accurate example of Liskov?

The classic example of the inheritance technique causing problems is the circle-elipse problem (a.k.a the rectangle-square problem) which is a is a violation of the Liskov substitution principle. A good example here is that of a bird and a penguin; I will call this dove-penguin problem.


2 Answers

The question is simply answered by recognising that inheritance models an "IS-A" relationship, while membership models a "HAS-A" relationship.

  • An employee IS A user
  • An employee HAS A userinfo

Which one is correct? This is your answer.

like image 79
1800 INFORMATION Avatar answered Oct 12 '22 16:10

1800 INFORMATION


I don't like either one. What happens when someone is both a member and an employee?

like image 21
Brad Wilson Avatar answered Oct 12 '22 14:10

Brad Wilson