コンポジットパターンの例を解説
本の説明に従いクラスを作ります。ここでは、共通のクラスである"Party"を作ります。 ソースは次のようになります。
<?php
class Party {
var $name; // 部門名/利用者名
/**
* 表示します
*/
function hyouji()
{
}
}
?>
利用者クラス"User"をつくります。出力メソッドhyoujiがあるので、テストケースもあわせて作ります。
テストのソースは次のようになります。
<?php
// UnitTest
require_once 'PHPUnit/GUI/HTML.php';
require 'Tests/UserTest.php';
// run Test Suites
$suite = array();
array_push($suite, new PHPUnit_TestSuite("UserTest"));
$result = new PHPUnit_GUI_HTML($suite);
$result->show();
?>
<?php
require_once 'PHPUnit.php';
require_once 'User.php';
class UserTest extends PHPUnit_TestCase {
function UserTest($name)
{
$this->PHPUnit_TestCase($name);
}
function testHyouji()
{
$user = new User("牛尾 剛");
$ret = $user->hyouji();
$this->assertEquals("牛尾 剛\n", $ret);
}
//CUPPA:End of Test Suite
}
?>
<?
require_once 'Party.php';
class User extends Party {
function User($name)
{
$this->name = $name;
}
function hyouji()
{
return $this->name . "\n";
}
}
?>
まずテストを作ります。
ソースは次のようになります。
<?php
require_once 'PHPUnit.php';
require_once 'User.php';
require_once 'Bumon.php';
class BumonTest extends PHPUnit_TestCase {
function BumonTest($name)
{
$this->PHPUnit_TestCase($name);
}
function setup()
{
$this->bumon = new Bumon("システム部門");
$user1 = new User("牛尾 剛");
$user2 = new User("古川 敦史");
$this->bumon->addParty($user1);
$this->bumon->addParty($user2);
}
function testUsers()
{
$user1 = new User("牛尾 剛");
$user2 = new User("古川 敦史");
$parties = array($user1, $user2);
$this->assertEquals("システム部門", $this->bumon->name);
$this->assertEquals($parties, $this->bumon->parties);
}
//CUPPA:End of Test Suite
}
?>
<?php
require_once 'Party.php';
class Bumon extends Party {
var $parties = array();
function Bumon($name)
{
$this->name = $name;
}
function addParty($party)
{
$this->parties[] = $party;
}
}
?>
表示メソッドを実装します。まずはテストを変更します。
ソースは次のようになります。
<?php
require_once 'PHPUnit.php';
require_once 'User.php';
require_once 'Bumon.php';
class BumonTest extends PHPUnit_TestCase {
function BumonTest($name)
{
$this->PHPUnit_TestCase($name);
}
function setup()
{
$this->bumon = new Bumon("システム部門");
$user1 = new User("牛尾 剛");
$user2 = new User("古川 敦史");
$this->bumon->addParty($user1);
$this->bumon->addParty($user2);
}
function testUsers()
{
$user1 = new User("牛尾 剛");
$user2 = new User("古川 敦史");
$parties = array($user1, $user2);
$this->assertEquals("システム部門", $this->bumon->name);
$this->assertEquals($parties, $this->bumon->parties);
}
function testHyoji()
{
$output_org = <<<__EOT__
システム部門
--------------------
牛尾 剛
古川 敦史
__EOT__;
$output = $this->bumon->hyouji();
$this->assertEquals($output_org, $output);
}
//CUPPA:End of Test Suite
}
?>
<?php
require_once 'Party.php';
class Bumon extends Party {
var $parties = array();
function Bumon($name)
{
$this->name = $name;
}
function addParty($party)
{
$this->parties[] = $party;
}
function hyouji()
{
$output = "\n";
$output .= $this->name . "\n";
$output .= "--------------------\n";
for ($i=0; $i<count($this->parties); $i++) {
$party = $this->parties[$i];
$output .= $party->hyouji();
}
return $output;
}
}
?>
実行用のクラスManagerクラスと実行ファイルrun.phpを作ります。
ソースは次のようになります。
<?
require_once 'Bumon.php';
require_once 'User.php';
class Manager {
function main()
{
$ushio = new User("牛尾さん");
$ando = new User("安藤さん");
$systemBumon = new Bumon("システム部");
$systemBumon->addParty($ushio);
$systemBumon->addParty($ando);
$mizukoshi = new User("水越さん");
$kawano = new User("河野さん");
$eigyoBumon = new Bumon("営業部");
$eigyoBumon->addParty($mizukoshi);
$eigyoBumon->addParty($kawano);
$jigyobu = new Bumon("製造事業部");
$jigyobu->addParty($systemBumon);
$jigyobu->addParty($eigyoBumon);
return $jigyobu->hyouji();
}
}
?>
<?php require_once 'Manager.php'; $manager = new Manager(); echo "<PRE>"; echo $manager->main(); echo "</PRE>\n"; ?>
作成したrun.phpを実行します。下のように表示されれば成功です。
[デモ]
Copyright © 1997-2003 by A.G.Labo
$Revision: 1 $