传输对象模式
2017-05-30 本文已影响41人
散装咖啡
传输对象模式主要作用-适用于在不同层传输数据。例如在UI层和service层传输数据的时候,可以避免在内存中不断复制,节省资源的开销。
` ``
//transferObject
class StudentVO
{
private $name;
private $rollNo;
public function __construct($name, $rollNo)
{
$this->name = $name;
$this->rollNo = $rollNo;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getRollNo()
{
return $this->rollNo;
}
public function setRollNo($rollNo)
{
$this->rollNo = $rollNo;
}
}
class StudentBO
{
public $students;
public function updateStudent($student)
{
$this->students[$student->getRollNo()]->setName($student->getName());
echo "Student: Roll No " . $student->getRollNo() . ", updated in the database";
}
public function __construct(){
$this->students['Robert'] = new StudentVO("Robert",0);
$this->students['John'] = new StudentVO("John",1);
}
public function deleteStudent($student)
{
//foreach ($this->students $a $v) {
//if (get_class)
//}
if (isset($this->students[$student->name])) {
unset($this->students[$student->name]);
}
//students.remove(student.getRollNo());
echo "Student: Roll No " . $this->students->getRollNo() . ", deleted from database";
}
public function getAllStudents()
{
return $this->students;
}
public function getStudent($rollNo)
{
return $this->students[$rollNo->name];
}
}
$StudentBO = new StudentBO();
foreach ($StudentBO->getAllStudents() as $student) {
echo "Student: [RollNo : " . $student->getRollNo() . ", Name : " . $student->getName() . " ]";
}
//$all = $StudentBO->getAllStudents();
//var_dump($all);exit;
//$one = $all[0];
//$one->setName("Michael");
//$one->updateStudent($one);
//获取学生
$StudentBO->getStudent(0);
echo "Student: [RollNo : " . $StudentBO->getRollNo() . ", Name : " . $StudentBO->getName() . " ]";
参考文章 http://www.runoob.com/design-pattern/transfer-object-pattern.html