Home  >  Article  >  Backend Development  >  面向对象分析与设计疑点

面向对象分析与设计疑点

WBOY
WBOYOriginal
2016-06-13 12:10:03957browse

面向对象分析与设计疑问
各位大侠好,小弟接触面向对象也有段时间了,可每次设计新系统时都会遇到关于对象如何拆分与设计的问题,特来请教一番。
比如,现在的系统要求有:用户,角色,积分。
用户有:用户名,密码。
角色有:角色名。
积分有:积分值,所属用户,所属模块。

通过阅读网上的帖子,我觉得:
用户对象应该包含:用户名,密码,角色对象,积分对象
code: 
class User {
  public $username;
  public $password;
  public $role;
  public $score;
}

请问是这样设计的吗?可我总感觉哪地方不对,或许我觉得,角色对象和积分对象不作为用户对象的属性,当需要获取用户对象时,重新new一下角色对象,然后将用户对象传入,这样获取用户所对应的角色。这样设计合理吗?
------解决思路----------------------
你第一设计方案,本身并无不妥。只是写死了
而你的第二方案就灵活多了
至于是将用户传入角色,还是将角色传入用户,就是设计模式的选取了
------解决思路----------------------
積分可以分到另一個 class去做。
積分類只增刪改積分,不用處理會員其他資料。例如

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


具體要看需求。
------解决思路----------------------
上班無聊借此貼來練手並複雜面向對象基礎

<br /><?php<br />//用戶基類<br />class BaseUser{<br />	public $userName;<br />	public $password;<br />	<br />	function __construct($userName,$password){<br />		$this->userName = $userName;<br />		$this->password = $password;<br />	}<br />}<br /><br />//角色類<br />class Role{<br />	private $userName;<br />	public $roleName;<br />	<br />	function __construct($userName,$roleName){<br />		$this->userName = $userName;<br />		$this->roleName = $roleName;<br />	}<br />	<br />	function GetRoleUserName()<br />	{<br />		return $this->userName;<br />	}<br />}<br /><br />//積分類<br /><br />class Score{<br />   private $userName;<br />   private $module;<br />   public $score;<br />   <br />   function __construct($userName,$module,$score){<br />   	   $this->userName = $userName;<br />	   $this->module = $module;<br />	   $this->score = $score;<br />   }<br />   <br />	function GetScoreUserName()<br />	{<br />		return $this->userName;<br />	}<br />	<br />	function GetScoreModule()<br />	{<br />		return $this->module;<br />	}<br /><br />   <br />}<br /><br />//用戶類 繼承用戶基類<br />class User extends BaseUser{<br />	public $role;<br />	public $score;<br />	<br />	function __construct($userName,$password,$roleName,$score){<br />		parent::__construct($userName, $password);<br />		$this->role = new Role($userName,$roleName);<br />		$this->score = new Score($userName,"module",$score);<br />	}<br />}<br /><br />$user = new User("user9527","123","xiaoming",100);<br /><br />echo "userName: ".$user->userName;<br />echo "<br>";<br />echo "password: ".$user->password;<br />echo "<br>";<br />echo "roleName: ".$user->role->roleName;<br />echo "<br>";<br />echo "score: ".$user->score->score;<br />echo "<br>";<br /><br />/*<br />userName: user9527<br />password: 123<br />roleName: xiaoming<br />score: 100<br /> */<br /><br /><br /><br />//code end<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP中的戏法方法Next article:PHP中的幻术变量