设计模式(备忘录模式&&组合模式)

<?php

//备忘录模式



class GameRole {

  private $attack,$defence,$lifetime;

  function __construct() {

      $this->getStatus();

  }



  function getStatus() {

      $this->attack = 100;

	  $this->defence = 100;

	  $this->lifetime = 100;

  }



  function displayStatus() {

      echo '当前的攻击力为:' . $this->attack;

      echo '当前的防御力为:' . $this->defence;

      echo '还有:' . $this->lifetime . '';

	  echo '<br/>';

  }

  //战斗消耗的体力

  function fight() {

      $this->attack = 10;

	  $this->defence = 20;

	  $this->lifetime = 30;

  }

  //保存状态

  function saveStatus() {

      return new RoleStatus($this->attack, $this->defence, $this->lifetime); 

  }



  function recoveryStatus(RoleStatus $status) {

      $this->attack = $status->attack;

	  $this->defence = $status->defence;

	  $this->lifetime = $status->lifetime;

  }



}

//游戏状态的存储类

class RoleStatus {

  public $attack,$defence,$lifetime;

  function __construct($attack, $defence, $lifetime) {

      $this->attack = $attack;

	  $this->defence = $defence;

	  $this->lifetime = $lifetime;

  }

}



//角色状态管理者类



class RoleStatusCaretaker {

  private $memento;



  function setMemento($memento) {

      $this->memento = $memento;

  }



  function getMemento() {

      return $this->memento;

  }

}

 

  $role = new GameRole();

  $role->displayStatus();



  //保存状态

  $statusAdmin = new RoleStatusCaretaker();

  $statusAdmin->setMemento($role->saveStatus());

  //决斗

  $role->fight();

  $role->displayStatus();

  

  $role->recoveryStatus($statusAdmin->getMemento());

  $role->displayStatus();

备忘录模式适应于,功能比较复杂又要保存历史记录的情况。

缺点是如果状态数据太多会消耗太多的内存。

<?php
//组合模式
abstract class Company {
    public $name;
function __construct($name) {
     $this->name = $name;
}
abstract function add(Company $company);
abstract function remove(Company $company);
abstract function display($depth);
abstract function lineOfDuty();
}

//具体公司类

class ConcreteCompany extends Company {
    public $children =  array();
    function __construct($name) {
     parent::__construct($name);
}

    function add(Company $company) {
     $this->children += array("$company->name" => $company);
}

function remove(Company $company) {
     unset($this->children[$company->name]);
}

function display($depth) {
     echo str_repeat('-', $depth) . $this->name . '<br/>';
foreach($this->children as $child) {
     $child->display($depth + 2);
}
}

function lineOfDuty() {
     foreach($this->children as $child) {
     $child->lineOfDuty();
}
}
}

//人力资源部
class HRDeparement extends Company {

    function add(Company $company) {
}

function remove(Company $company) {
}

function display($depth) {
     echo str_repeat('-', $depth) . $this->name . '<br/>';
}

function lineOfDuty() {
     echo $this->name . '员工招聘培训管理<br/>';
}
}

//财务部
class FinanceDeparement extends Company {

    function add(Company $company) {
}

function remove(Company $company) {
}

function display($depth) {
     echo str_repeat('-', $depth) . $this->name . '<br/>';
}

function lineOfDuty() {
     echo $this->name . '公司财务收支管理</br>';
}
}
$root = new ConcreteCompany('北京总公司');
$root->add(new HRDeparement('总公司人力资源部'));
$root->add(new FinanceDeparement('总公司财务部'));

$company1 = new ConcreteCompany('上海华东分公司');
$company1->add(new HRDeparement('华东分公司人力资源部'));
$company1->add(new FinanceDeparement('华东分公司财务部'));
$root->add($company1);

$office1 = new ConcreteCompany('南京办事处');
$office1->add(new HRDeparement('南京办事处人力资源部'));
$office1->add(new FinanceDeparement('南京办事处财务部'));$company1->add($office1);$office2 = new ConcreteCompany('深圳办事处');
$office2->add(new HRDeparement('深圳办事处人力资源部'));
$office2->add(new FinanceDeparement('深圳办事处财务部'));
$company1->add($office2);
echo '结构图<br/>';
$root->display(1);
echo '职责<br/>';
$root->lineOfDuty();

结果是:

结构图
-北京总公司
---总公司人力资源部
---总公司财务部
---上海华东分公司
-----华东分公司人力资源部
-----华东分公司财务部
-----南京办事处
-------南京办事处人力资源部
-------南京办事处财务部
-----深圳办事处
-------深圳办事处人力资源部
-------深圳办事处财务部
职责
总公司人力资源部员工招聘培训管理
总公司财务部公司财务收支管理
华东分公司人力资源部员工招聘培训管理
华东分公司财务部公司财务收支管理
南京办事处人力资源部员工招聘培训管理
南京办事处财务部公司财务收支管理
深圳办事处人力资源部员工招聘培训管理
深圳办事处财务部公司财务收支管理


组合模式适用于整体和部分都要被一致对待的情况,用户不用关心到底是处理的是一个叶子还是一个组合组件。

你可能感兴趣的:(备忘录模式)