public function run() { $duck = new MallarDuck(); $duck->performFly(); $duck->performQuack(); $model = new ModelDuck(); $model->performFly(); $model->setFlyBehavior(new FlyRocketPowered()); $model->performFly(); }
public static function main(array $args = array()) { $mallard = new MallardDuck(); $mallard->performQuack(); $mallard->performFly(); $model = new ModelDuck(); $model->performFly(); $model->setFlyBehavior(new FlyRocketPowered()); $model->performFly(); }
<?php function __autoload($class_name) { include $class_name . '.php'; } $mallard = new MallardDuck(); $mallard->display(); $mallard->swim(); $mallard->performQuack(); $mallard->performFly(); $model = new ModelDuck(); $model->display(); $model->swim(); $model->performQuack(); $model->performFly();
$this->quackBehavior = new Qquack(); $this->flyBehavior = new FlyNoWay(); } public function display() { println("I'm a real Mallard duck"); } } println(SEPARATE, '策略模式', SEPARATE); showImgs('1.png', '1_1.png'); $mallar = new MallardDuck(); $mallar->display(); $mallar->performQuack(); $mallar->performFly(); println(); $model = new ModelDuck(); $model->display(); $model->performQuack(); $model->performFly(); #改变模型鸭的行为 println('change quack:'); $model->setQuackBehavior(new Squack()); $model->performQuack(); println(SEPARATE, 'END', SEPARATE); /** * 输出: I'm a real Mallard duck << Quack >> I can't fly I'm a real Model duck
<?php require_once 'Ducks/DecoyDuck.php'; require_once 'Ducks/MallardDuck.php'; require_once 'Ducks/ModelDuck.php'; require_once 'Ducks/RedHeadDuck.php'; require_once 'Ducks/RubberDuck.php'; $mallard = new MallardDuck(); $rubber = new RubberDuck(); $decoy = new DecoyDuck(); $model = new ModelDuck(); $mallard->performQuack(); $rubber->performQuack(); $decoy->performQuack(); echo "<hr/>"; $mallard->performFly(); $rubber->performFly(); $decoy->performFly(); echo "<hr/>"; $model->performFly(); $model->setFlyBehavior(new FlyRocketPowered()); $model->performFly();