Proev og se paa denne kode:
<?php
// **** normal code ****
class OrderLine {
private $no;
private $descrip;
public function __construct($no, $descrip) {
$this->no = $no;
$this->descrip = $descrip;
}
public function getNo() {
return $this->no;
}
public function getDescrip() {
return $this->descrip;
}
public function toString() {
return sprintf("%d %s\n", $this->no, $this->descrip);
}
}
class Order {
private $customer;
private $lines;
public function __construct($customer, $lines) {
$this->customer = $customer;
$this->lines = $lines;
}
public function getCustomer() {
return $this->customer;
}
public function getLInes() {
return $this->lines;
}
function toString() {
$res = sprintf("%s\n", $this->customer);
foreach($this->lines as $line) {
$res .= $line->toString();
}
return $res;
}
}
// build domain model
$orders = array(new Order('Hansen', array(new OrderLine(2000, 'mursten'), new OrderLine(3, 'poser cement'))),
new Order('Jensen', array(new OrderLine(50, 'planker'))));
// test domain model
foreach($orders as $order) {
echo $order->toString();
}
// **** code for those withs trong dislike of OO ****
// please use associative array with keys 'no' and 'descrip' for order lines
// please use associative array with keys 'customer' and 'lines'
function orderline_tostring($orderline) {
return sprintf("%d %s\n", $orderline['no'], $orderline['descrip']);
}
function order_tostring($order) {
$res = sprintf("%s\n", $order['customer']);
foreach($order['lines'] as $line) {
$res .= orderline_tostring($line);
}
return $res;
}
// build domain model
$orders = array(array('customer' => 'Hansen', 'lines' => array(array('no' => 2000, 'descrip' => 'mursten'), array('no' => 3, 'descrip' => 'poser cement'))),
array('customer' => 'Jensen', 'lines' => array( array('no' => 50, 'descrip' => 'planker'))));
// test domain model
foreach($orders as $order) {
echo order_tostring($order);
}
?>
OO modellen og non-OO modeller er nogenlunde ekvivalente.
Og derfor kan non-OO modellen i et vist omfang vises med samme class diagram som OO modellen.
Det forudsaetter at non-OO modellen er lavet med bare en lille smule data struktur, men ........