Created
July 29, 2018 02:59
-
-
Save bz0/bf633893203001a7646f11231cb7af8a to your computer and use it in GitHub Desktop.
GoF Compositeパターン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class OrganizationEntry{ | |
private $code; | |
private $name; | |
public function __construct($code, $name){ | |
$this->code = $code; | |
$this->name = $name; | |
} | |
public function getCode(){ | |
return $this->code; | |
} | |
public function getName(){ | |
return $this->name; | |
} | |
//必ず子クラスでこのメソッドを実装要 | |
public abstract function add(OrganizationEntry $entry); | |
public function dump(){ | |
echo $this->code . ":" . $this->name . "<br>\n"; | |
} | |
} | |
class Group extends OrganizationEntry{ | |
private $entries; | |
public function __construct($code, $name){ | |
parent::__construct($code, $name); | |
$this->entries = array(); | |
} | |
public function add(OrganizationEntry $entry){ | |
array_push($this->entries, $entry); | |
} | |
public function dump(){ | |
parent::dump(); | |
foreach($this->entries as $entry){ | |
$entry->dump(); | |
} | |
} | |
} | |
class Employee extends OrganizationEntry{ | |
public function __construct($code, $name){ | |
parent::__construct($code, $name); | |
} | |
public function add(OrganizationEntry $entry){ | |
throw new Exception('method not allowed'); | |
} | |
} | |
$root_entry = new Group("001", "本社"); | |
$root_entry->add(new Employee("00101", "CEO")); | |
$root_entry->add(new Employee("00102", "CTO")); | |
$group1 = new Group("010", "○○支店"); | |
$group1->add(new Employee("01001", "支店長")); | |
$group1->add(new Employee("01002", "佐々木")); | |
$group1->add(new Employee("01003", "鈴木")); | |
$group1->add(new Employee("01004", "吉田")); | |
$group2 = new Group("110", "△△支店"); | |
$group2->add(new Employee("11001", "川村")); | |
$group1->add($group2); | |
$root_entry->add($group2); | |
$group3 = new Group("020", "××支店"); | |
$group3->add(new Employee("02001", "荻原")); | |
$group3->add(new Employee("02002", "田島")); | |
$group3->add(new Employee("02003", "白井")); | |
$root_entry->add($group3); | |
$root_entry->dump(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment