php – 什么是更好的设计?

我有以下课程:

class User {

  public function setName($value) { ... }
  public function setEmailAddress($value) { ... }
  public function setUsername($value) { ... }
  public function getName() { ... }
  public function getEmailAddress() { ... }
  public function getUsername() { ... }

  public function isGroupAdministrator($groupId) { ... }
  public function isMemberOfGroup($groupId) { ... }
  public function isSiteAdministrator() { ... }
  public function isRoot() { ... }
  public function hasModulePermission($moduleId, $recordId, $permissionCode) { ... }
  public function hasGroupPermission($groupId, $permissionCode) { ... }
  public function hasContentPermission($recordId, $permissionCode) { ... }
  public function hasModulePermission($moduleId, $recordId, $permissionCode) { ... }
  public function canLogIn() { ... }
  public function isLoggedIn() { ... }
  public function setCanLogIn($canLogIn) { ... }

}

这会变成“神级吗?”

我不确定我是否应该分开这门课.这样做的问题是该类的方法被其域用于确定是否在网页上显示给定的UI元素,因此类中没有任何行为.

我想我可以将权限相关的方法放在一些Permission类中,使这些方法保持静态(例如.:: userIsGroupAdministrator(…),:: userIsMemberOfGroup(…):: userHasGroupPermission(…),:: userHasContentPermission(…))

关于这门课程如何更好的任何建议?

如果您已经运行了代码,那么重构一下就可以了.如果不是,我会看下面的内容:


class User {
  String username
  String name
  String emailAddress
  Boolean active
  Integer permission # A bitmask of the statics in the Permission class
}

class Group {
  String name
}

class UserGroupMapping {
  User user
  Group group
  Boolean administrator
}

class Permission {
  static IS_ROOT = 1
  static IS_SITE_ADMIN = 2
}

class Session {
  User user
  Boolean logged_in
}

其余部分真正属于服务类:

class SecurityService {
  static public function hasModulePermission($user, $module, $record, $permission) { ... }
  static public function hasGroupPermission($user, $group, $permission) { ... }
  static public function hasContentPermission($user, $record, $permission) { ... }
  static public function hasModulePermission($user, $module, $record, $permission) { ... }
}
翻译自:https://stackoverflow.com/questions/730856/what-is-a-better-design

转载注明原文:php – 什么是更好的设计?