Heroes Good, little brother contact object-oriented also has a period of time, can each design a new system will encounter on the object how to split and design problems, special to consult.
For example, the current system requirements are: users, roles, points.
Users are: User name, password.
Roles are: role names.
Points are: The integral value, the owning user, the owning module.
By reading the online post, I think:
User objects should contain: User name, password, role object, Integration object
Code
Class User {
public $username;
Public $password;
Public $role;
Public $score;
}
Is this how it was designed? But I always feel that the place is wrong, perhaps I think, the role object and the integration object is not a property of the user object, when need to get the user object, re-new the Role object, and then pass the user object, so as to get the user's corresponding role. Is this a reasonable design?
Reply to discussion (solution)
If you add a new element after the role, the integration as a user attribute, do I have to change the code? In that case, the user object will become a great big thing.
However, if you do not act as an attribute, the roles and integrals are indeed owned by the user.
Your first design has nothing wrong with itself. It's just a dead letter.
And your second plan is much more flexible.
As to whether to pass the user into the role or pass the role on to the user, the design mode is selected
? can be divided into another one? class to do.
The only increase in the points of the division, not the rationale? Other materials. For example
Class score{ private $_ouser; Public function __construct ($oUser) { $this->_ouser = $oUser; } Public function Add () { } public function Update () {} }
Depends on the demand.
Thanks for the two-bit reply. My knowledge of object-oriented is still too deficient, as the moderator said, I am looking at the design pattern of the article, hoping to deepen my understanding of object-oriented.
To go to work? To borrow this??? Hand??? Face the elephant base?
UserName = $userName; $this->password = $password;}} Role: Class Role{private $userName;p ublic $roleName, function __construct ($userName, $roleName) {$this->username = $ userName; $this->rolename = $roleName;} function Getroleusername () {return $this->username;}} Class score{private $userName; Private $module; Public $score; function __construct ($userName, $module, $score) {$this->username = $userName; $this->module = $module; $this->score = $score; } function Getscoreusername () {return $this->username;} function Getscoremodule () {return $this->module;} }//use?? Extends Baseuser{public $role;p ublic $score; function __construct ($userName, $password, $roleName, $ Score) {parent::__construct ($userName, $password); $this->role = new Role ($userName, $roleName); $this->score = New Score ($userName, "module", $score);}} $user = New User ("user9527", "123", "xiaoming", +), echo "UserName:". $user->username;echo "
"; echo" Password: ". $user->password;echo"
"; echo" RoleName: ". $user->role->rolename;echo"
"; echo" Score: ". $user->score->score;echo"
";/*username:user9527password:123rolename:xiaomingscore:100 *///code End