分享
 
 
 

在数据库中使用对象的好处

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

显而易见,这对我们控制存取对象的数据有很大帮助。如果一个程序员已经直接地存取username的信息,以上代码的变化将会破坏他的代码。然而我们可以使用(类的)存取方法,就像上面代码中注释的那样,添加一个验证的功能而不需要改变任何其他的东西。注意username的验证(例子当中是不能超过12字节)代码是独立在setUsername()方法之外的。从验证到存储到数据库的过程轻而易举。而且,这是个非常好的单凭经验的方法,一个方法或一个类需要做的越少,它的重复使用的机会将会越大。这在你开始写一个子类时更加明显,假如你需要一个子类,并且又要跳过(忽略)父类方法(行为)中的一些特殊的细节,如果(针对这个细节的)方法很小而又精细,(修改它)只是一瞬间的过程,而如果这个方法非常臃肿,针对多种目的,你可能将在复制子类中大量代码中郁闷而终。

比方说,假如Admin是User类的一个子类。我们对adamin的用户可能会有不同的,相对苛刻一些的密码验证方法。最好是跨过父类的验证方法和整个setUsername()方法(在子类中重写)。

更多关于存取器(Accessor)

下面是一些其他的例子来说明如何使存取器用的更有效果。很多时候我们可能要计算结果,而不是简单的返回数组中的静态数据。存取方法还能做的一个有用的事情就是更新(updating)缓存中的值。当所有的变动(对数据的所有操作)都要通过setX()方法的时候,这正是我们根据X来重置缓存中的值的时刻。

于是我们的这个类层次变得更加明了:

内部变量$_data的处理被替换成受保护的私有方法(private methods)_getData()和_setData()

这类方法被转移到被称作记录(Record)的抽象的超级类(super class),当然它是User类下的子类

这个记录类(Record class)掌握所有存取数组$_data的细节,在内容被修改之前调用验证的方法,以及将变更的通知发给记录(Records),就像发给中心对象存储(ObjectStore)实例。

<?php

class User extends Record {

// --- OMITTED CODE --- //

/**

* Do not show the actual password for the user, only some asterixes with the same strlen as the password value.

*/

function password() {

$passLength = strlen($this->_getData('password'));

return str_repeat('*', $passLength);

}

/**

* Setting the user password is not affected.

*/

function setPassword($newPassword) {

$this->_setData('password', $newPassword);

}

/**

* fullName is a derived attribute from firstName and lastName

* and does not need to be stored as a variable.

* It is therefore read-only, and has no 'setFullname()' accessor method.

*/

function fullName() {

return $this->firstName() . " " . $this->lastName();

}

/**

* Spending limit returns the currency value of the user's spending limit.

* This value is stored as an INT in the database, eliminating the need

* for more expensive DECIMAL or DOUBLE column types.

*/

function spendingLimit() {

return $this->_getData('spendingLimit') / 100;

}

/**

* The set accessor multiplies the currency value by 100, so it can be stored in the database again

* as an INT value.

*/

function setSpendingLimit($newSpendLimit) {

$this->_setData('spendingLimit', $newSpendLimit * 100);

}

/**

* The validateSpendingLimit is not called in this class, but is called automatically by the _setData() method

* in the Record superclass, which in turn is called by the setSpendingLimit() method.

*/

function validateSpendingLimit(&$someLimit) {

if (is_numeric($someLimit) AND $someLimit >= 0) {

return true;

} else {

throw new Exception("Spending limit must be a non-negative integer"); //PHP5 only

}

}

}

/**

* Record is the superclass for all database objects.

*/

abstract class Record {

var $_data = array();

var $_modifiedKeys = array(); // keeps track of which fields have changed since record was created/fetched

/**

* Returns an element from the $_data associative array.

*/

function _getData($attributeName) {

return $this->_data[$attributeName];

}

/**

* If the supplied value passes validation, this

* sets the value in the $_data associative array.

*/

function _setData($attributeName, $value) {

if ($this->validateAttribute($attributeName, $value)) {

if ($value != $this->_data[$attributeName]) {

$this->_data[$attributeName] = $value;

$this->_modifiedKeys[] = $attributeName;

$this->didChange();

} else {

// the new value is identical to the current one

// no change necessary

}

}

}

/**

* For an attribute named "foo", this looks for a method named "validateFoo()"

* and calls it if it exists. Otherwise this returns true (meaning validation passed).

*/

function validateAttribute($attributeName, &$value) {

$methodName = 'validate' . $attributeName;

if (method_exists($this, $methodName)) {

return $this->$methodName($value);

} else {

return true;

}

}

function didChange() {

// notify the objectStore that this record changed

}

}

?>

现在我们拥有了一个抽象的超级类(Record),我们可以将User类里面大量的代码转移出来,而让这个User的子类来关注User的特殊项目如存取和验证方法。你可能已经注意到在我们的这个纪录类(Record class)没有任何的SQL代码。这并不是疏忽或者遗漏!对象存储类(ObjectStore class)(隐藏在第二部分)将负责所有和数据库的交互,还有我们的超级类Record的实例化。这样使我们的Record类更加瘦小而又有效率,而这对于评价我们处理大量对象的效率的时候是个重要因素。

如果你有兴趣看看这篇文章基于的完整的代码(不会出现如文中出现的所有的语法错误),可以给我的邮箱发信

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有