<?php namespace StockCommandNS; //Command interface interface Order { public function execute(); } //Receiver class class StockTrade { public function buy() { print("You want to buy stocks\n"); } public function sell() { print("You want to sell stocks\n"); } } //Invoker class class Agent { public function placeOrder(Order $order) { $order->execute($order); } } //ConcreteCommand Class class GenericOrder implements Order { private $action; public function __construct($action) { $this->action = $action; } public function execute() { call_user_func($this->action); } } $stock = new StockTrade(); $bsc = new GenericOrder([$stock, 'buy']); $ssc = new GenericOrder([$stock, 'sell']); $agent = new Agent(); $agent->placeOrder($bsc); // Buy Shares $agent->placeOrder($ssc); // Sell Shares
You have javascript disabled. You will not be able to edit any code.