-1

I have a class Company, this class has some methods to search, list, etc and is used for the visitors of my website. But I have a different class adminCompany, which works only, when there is an admin user logged in. This class has the other methods: update_info_company(), delete_company(), and whatever an admin can do

My questions are:

  • Should I have a single class (Company) to do everything and just restrict access to some methods?
  • Is this against the principles of OOP?.

PD: it`s just to keep cleaner and shorter classes.

tereško
  • 58,060
  • 25
  • 98
  • 150
DanStarck
  • 27
  • 3

1 Answers1

2

In OOP we have this thing: single responsibility principle. It basically means that each class should have only one reason to change ( when extended ).

From your description it seems that the "Company" is both a separate entity (most likely, some variation on domain object), which has distinct properties and can be created or removed. And then it also has aspects of factory, when it is used to find multiple instances of same object. This seems like a mistake.

You should separate the two aspects of this structure (I would assume that now you have class AdminCompany extends Company{} , which is part of the problem).

As for controlling which group of users can perform with tasks, you might find this post useful.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150