Questions about object-oriented analysis and design

Source: Internet
Author: User
Good questions about object-oriented analysis and design. it may take some time for the younger brother to get in touch with object-oriented design. every time you design a new system, you will encounter problems about how to split and design objects, let's talk about it.
For example, the current system requirements include users, roles, and points.
Users: user name and password.
Roles include: Role name.
Points: points, users, and modules.

By reading online posts, I think:
The user object should include: user name, password, role object, and point object.
Code:
Class User {
Public $ username;
Public $ password;
Public $ role;
Public $ score;
}

Is it designed in this way? But I always feel that something is wrong. maybe I think that the role object and the point object are not the attributes of the user object. when you need to get the user object, try again the role object, then pass in the user object to obtain the role corresponding to the user. Is the design reasonable?


Reply to discussion (solution)

If roles and credits are used as user attributes, do I have to modify the code if new elements are added later? In this way, the user object will become huge.
However, if it is not used as an attribute, the role and points are indeed owned by the user.

There is nothing wrong with your first design. It's just written to death.
Your second solution is much more flexible.
To pass a user into the role or pass the role to the user, the design mode is selected.

? Can be divided into another one? Class.
? Points? Add only? Change? Points, no? Why ?? Others? Material. For example

class score{    private $_oUser;    public function __construct($oUser){        $this->_oUser = $oUser;    }    public function add(){    }    public function update(){   }}


? Depends on requirements.

Thank you for your reply. I still lack the object-oriented knowledge. as the moderator said, I am reading the design pattern article, hoping to deepen my understanding of object-oriented.

Work? Chat with this ??? Hands ??? Oriented? Elephant base?

 UserName = $ userName; $ this-> password = $ password ;}// role? Class Role {private $ userName; public $ roleName; function _ construct ($ userName, $ roleName) {$ this-> userName = $ userName; $ this-> roleName = $ roleName;} function GetRoleUserName () {return $ this-> userName ;}}//? Points? 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 ?? ? Commitment? ? Class User extends BaseUser {public $ role; public $ 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", 100); echo "userName :". $ user-> userName; echo"
"; Echo" password: ". $ user-> password; echo"
"; Echo" roleName: ". $ user-> role-> roleName; echo"
"; Echo" score: ". $ user-> score; echo"
";/* UserName: user9527password: 123 roleName: xiaomingscore: 100 * // code end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.