php设计模式之命令模式

  1. <?php
  2. header("Content-Type:text/html;charset=utf-8");
  3. /**
  4. *
  5. * @php设计模式之命令模式
  6. * @命令模式顾名思义,下达命令,接收命令,执行命令
  7. * @本例主要模拟古时皇帝给下面的人下达命令来解释什么是命令模式
  8. **/
  9. //执行命令
  10. class Minitary
  11. {
  12. //执行派兵
  13. public function battle()
  14. {
  15. echo '派兵打仗'.PHP_EOL;
  16. }
  17. }
  18. class Kitchen
  19. {
  20. //执行做饭
  21. public function cook()
  22. {
  23. echo '做好吃的';
  24. }
  25. }
  26. /**
  27. *命令统一执行接口
  28. **/
  29. interface Command{ public function execute();}
  30. /**
  31. *
  32. * 兵部需要执行 打仗命令
  33. **/
  34. class BattleCommand implements Command
  35. {
  36. //发兵令牌
  37. protected $_military;
  38. public function __construct(Minitary $minitary)
  39. {
  40. $this->_military=$minitary;
  41. }
  42. public function execute()
  43. {
  44. $this->_military->battle();
  45. }
  46. }
  47. /**
  48. *
  49. * 御膳房需要执行 做饭命令
  50. **/
  51. class CookCommand implements Command
  52. {
  53. //做饭令牌
  54. protected $_kitchen;
  55. public function __construct(Kitchen $kitchen)
  56. {
  57. $this->_kitchen=$kitchen;
  58. }
  59. public function execute()
  60. {
  61. $this->_kitchen->cook();
  62. }
  63. }
  64. /**
  65. *
  66. *接下来,我们看下太监能干什么?他只需要知道是什么命令,去传就可以了。
  67. **/
  68. class Taijian
  69. {
  70. /**
  71. *这是什么命令
  72. */
  73. protected $_command;
  74. public function __construct(Command $command)
  75. {
  76. $this->_command=$command;
  77. }
  78. public function sendCommand()
  79. {
  80. $this->_command->execute();
  81. }
  82. }
  83. /**
  84. *到此二B皇帝只需要发号施令就可以了
  85. *
  86. **/
  87. class King
  88. {
  89. public function doCommand()
  90. {
  91. //让一个太监给兵部发送派兵打仗的命令.
  92. $tj1=new Taijian(new BattleCommand(new Minitary));
  93. $tj1->sendCommand();
  94. //让一个太监给御善房发送做饭的命令.
  95. $tj2=new Taijian(new CookCommand(new Kitchen));
  96. $tj2->sendCommand();
  97. }
  98. }
  99. $king=new King;
  100. $king->doCommand();
  101. ?>
<?php
	header("Content-Type:text/html;charset=utf-8");
	/**
	 *
	 * @php设计模式之命令模式
	 * @命令模式顾名思义,下达命令,接收命令,执行命令
	 * @本例主要模拟古时皇帝给下面的人下达命令来解释什么是命令模式
	 **/

	//执行命令
	class  Minitary
	{
		//执行派兵
		public function battle()
		{
			echo '派兵打仗'.PHP_EOL;
		}
	}
	class Kitchen
	{
		//执行做饭
		public function cook()
		{
			echo '做好吃的';
		}
	}

	/**
	 *命令统一执行接口
	 **/

	interface Command{ public function execute();}

	/**
	 *
	 * 兵部需要执行 打仗命令
	 **/

	class BattleCommand implements Command 
	{
		//发兵令牌
		protected $_military;
		public function __construct(Minitary $minitary)
		{
			$this->_military=$minitary;
		}
		public function execute()
		{
			$this->_military->battle();
		}

	}

	/**
	 *
	 * 御膳房需要执行 做饭命令
	 **/

	class CookCommand implements Command
	{
		//做饭令牌
		protected $_kitchen;
		public function __construct(Kitchen $kitchen)
		{
			$this->_kitchen=$kitchen;
		}
		public function execute()
		{
			$this->_kitchen->cook();
		}
	}

	/**
	 *
	 *接下来,我们看下太监能干什么?他只需要知道是什么命令,去传就可以了。
	 **/
	
	class Taijian
	{
		/**
		 *这是什么命令
		 */
		protected $_command;
		public function __construct(Command $command)
		{
			$this->_command=$command;
		}
		public function sendCommand()
		{
			$this->_command->execute();
		}
	}
	/**
	 *到此二B皇帝只需要发号施令就可以了
	 *
	 **/
	class King
	{
		public function doCommand()
		{
			//让一个太监给兵部发送派兵打仗的命令.
			$tj1=new Taijian(new BattleCommand(new Minitary));
			$tj1->sendCommand();

			//让一个太监给御善房发送做饭的命令.
			$tj2=new Taijian(new CookCommand(new Kitchen));
			$tj2->sendCommand();
		}
	}
	$king=new King;
	$king->doCommand();
?>
  1. <span style="white-space: pre;"> </span>学习QQ群:193990134
<span style="white-space: pre;">				</span>学习QQ群:193990134
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值