0

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.

4

1 回答 1

2

在OOP中我们有这个东西:单一责任原则。这基本上意味着每个类应该只有一个改变的理由(扩展时)。

从您的描述看来,“公司”似乎都是一个单独的实体(很可能是domain object的一些变体),它具有不同的属性并且可以创建或删除。然后它还具有工厂方面的功能,用于查找同一对象的多个实例。这似乎是一个错误。

你应该把这个结构的两个方面分开(我假设现在你有class AdminCompany extends Company{},这是问题的一部分)。

至于控制哪些用户组可以执行任务,您可能会发现这篇文章很有用。

于 2012-08-15T02:14:30.450 回答