书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然"codetitle">复制代码 代码如下:
<?php
/**
* 策略模式
* 定义了算法族,分别封装起来,让它们之间可以互相替换,
* 此模式让算法的变化独立于使用算法的客户。
*/
//飞行行为接口
interface FlyBehavior {
public function fly();
}
//呱呱叫行为接口
interface QuackBehavior {
public function quack();
}
//翅膀飞行
class FlyWithWings implements FlyBehavior {
public function fly() {
echo "I'm flying!!\n";
}
}
//不会飞
class FlyNoWay implements FlyBehavior {
public function fly() {
echo "I can't fly!\n";
}
}
class FlyRocketPowered implements FlyBehavior {
public function fly() {
echo "I'm flying with a rocket!\n";
}
}
class Qquack implements QuackBehavior {
public function quack() {
echo "Quack\n";
}
}
class Squeak implements QuackBehavior {
public function quack() {
echo "Squeak\n";
}
}
class MuteQuack implements QuackBehavior {
public function quack() {
echo "<< Silence \n";
}
}
abstract class Duck {
protected $quack_obj;
protected $fly_obj;
public abstract function display();

public function performQuack() {
$this->quack_obj->quack();
}
public function performFly() {
$this->fly_obj->fly();
}
public function swim() {
echo "All ducks float, even decoys!\n";
}
public function setFlyBehavior(FlyBehavior $fb) {
$this->fly_obj = $fb;
}
public function setQuackBehavior(QuackBehavior $qb) {
$this->quack_obj = $qb;
}
}

class ModelDuck extends Duck {
public function __construct() {
$this->fly_obj = new FlyNoWay();
$this->quack_obj = new MuteQuack();
}
public function display() {
echo "I'm a model duck!\n";
}
}

$model = new ModelDuck();
$model->performFly();
$model->performQuack();
//提供新的能力
$model->setFlyBehavior(new FlyRocketPowered());
$model->setQuackBehavior(new Squeak());
$model->performFly();
$model->performQuack();

?>

单件模式
复制代码 代码如下:
<?php
/**
* 单件模式
* 确保一个类只有一个实例,并提供一个全局访问点。
*/
class MyClass {
private static $uniqueInstance;
private function __construct() {

}
public static function getInstance() {
if (self::$uniqueInstance == null) {
self::$uniqueInstance = new MyClass();
}
return self::$uniqueInstance;
}
}
$myClass = MyClass::getInstance();
var_dump($myClass);
$myClass = MyClass::getInstance();
var_dump($myClass);
?>

工厂方法模式
复制代码 代码如下:
<?php
abstract class PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);

$pizza->prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
return $pizza;
}

public abstract function createPizza($type);
}
class NYPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new NYStyleCheesePizza();
} elseif ($type == "veggie") {
return new NYStyleVeggiePizza();
} elseif ($type == "clam") {
return new NYStyleClamPizza();
} elseif ($type == "papperoni") {
return new NYStylePapperoniPizza();
} else {
return null;

}
}
}
class ChicagoPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new ChicagoStyleCheesePizza();
} elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza();
} elseif ($type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
return null;
}
}
}
abstract class Pizza {
public $name;
public $dough;
public $sauce;
public $toppings = array();

public function prepare() {
echo "Preparing " . $this->name . "\n";
echo "Yossing dough...\n";
echo "Adding sauce...\n";
echo "Adding toppings: \n";
for ($i = 0; $i < count($this->toppings); $i++) {
echo " " . $this->toppings[$i] . "\n";
}
}

public function bake() {
echo "Bake for 25 minutes at 350\n";
}

public function cut() {
echo "Cutting the pizza into diagonal slices\n";
}

public function box() {
echo "Place pizza in official PizzaStore box\n";
}

public function getName() {
return $this->name;
}
}

class NYStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and cheese Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";

$this->toppings[] = "Grated Reggiano Cheese";
}
}

class NYStyleVeggiePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and veggie Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";

$this->toppings[] = "Grated Reggiano veggie";
}
}
class NYStyleClamPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and clam Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";

$this->toppings[] = "Grated Reggiano clam";
}
}
class NYStylePapperoniPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and papperoni Pizza";
$this->dough = "Thin Crust Dough";
$this->sauce = "Marinara Sauce";

$this->toppings[] = "Grated Reggiano papperoni";
}
}

class ChicagoStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "Chicago Style Deep Dish Cheese Pizza";
$this->dough = "Extra Thick Crust Dough";
$this->sauce = "Plum Tomato Sauce";

$this->toppings[] = "Shredded Mozzarella Cheese";
}

public function cut() {
echo "Cutting the pizza into square slices\n";
}
}

$myStore = new NYPizzaStore();
$chicagoStore = new ChicagoPizzaStore();
$pizza = $myStore->orderPizza("cheese");
echo "Ethan ordered a " . $pizza->getName() . "\n";

$pizza = $chicagoStore->orderPizza("cheese");
echo "Ethan ordered a " . $pizza->getName() . "\n";

?>

工厂模式
复制代码 代码如下:
<?php
abstract class PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);

$pizza->prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
return $pizza;
}

public abstract function createPizza($type);
}
class NYPizzaStore extends PizzaStore {
public function createPizza($type) {
$pizza = null;
$ingredientFactory = new NYPizzaIngredientFactory();
if ($type == "cheese") {
$pizza = new CheesePizza($ingredientFactory);
$pizza->setName('New York Style Cheese Pizza');
} elseif ($type == "veggie") {
$pizza = new VeggiePizza($ingredientFactory);
$pizza->setName('New York Style Veggie Pizza');
} elseif ($type == "clam") {
$pizza = new ClamPizza($ingredientFactory);
$pizza->setName('New York Style Clam Pizza');
} elseif ($type == "papperoni") {
$pizza = new PapperoniPizza($ingredientFactory);
$pizza->setName('New York Style Papperoni Pizza');
}
return $pizza;
}
}
class ChicagoPizzaStore extends PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new ChicagoStyleCheesePizza();
} elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza();
} elseif ($type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
return null;
}
}
}
interface PizzaIngredientFactory {
public function createDough();
public function createSauce();
public function createCheese();
public function createVeggies();
public function createPepperoni();
public function createClam();
}
class NYPizzaIngredientFactory implements PizzaIngredientFactory {
public function createDough() {
return new ThinCrustDough();
}
public function createSauce() {
return new MarinaraSauce();
}
public function createCheese() {
return new ReggianoCheese();
}
public function createVeggies() {
$veggies = array(
new Garlic(),
new Onion(),
new Mushroom(),
new RedPepper(),
);
return $veggies;
}
public function createPepperoni() {
return new SlicedPepperoni();
}
public function createClam() {
return new FreshClams();
}
}
class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
public function createDough() {
return new ThickCrustDough();
}
public function createSauce() {
return new PlumTomatoSauce();
}
public function createCheese() {
return new Mozzarella();
}
public function createVeggies() {
$veggies = array(
new BlackOlives(),
new Spinach(),
new EggPlant(),
);
return $veggies;
}
public function createPepperoni() {
return new SlicedPepperoni();
}
public function createClam() {
return new FrozenClams();
}
}
abstract class Pizza {
public $name;
public $dough;
public $sauce;
public $veggies = array();
public $cheese;
public $pepperoni;
public $clam;

public abstract function prepare();

public function bake() {
echo "Bake for 25 minutes at 350\n";
}

public function cut() {
echo "Cutting the pizza into diagonal slices\n";
}

public function box() {
echo "Place pizza in official PizzaStore box\n";
}

public function getName() {
return $this->name;
}

public function setName($name) {
$this->name = $name;
}

public function __toString() {

}
}

class CheesePizza extends Pizza {
public $ingredientFactory;

public function __construct(PizzaIngredientFactory $ingredientFactory) {
$this->ingredientFactory = $ingredientFactory;
}

public function prepare() {
echo "Preparing " . $this->name . "\n";
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this->cheese = $this->ingredientFactory->createCheese();
}
}

class ClamPizza extends Pizza {
public $ingredientFactory;

public function __construct(PizzaIngredientFactory $ingredientFactory) {
$this->ingredientFactory = $ingredientFactory;
}

public function prepare() {
echo "Preparing " . $this->name . "\n";
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this->cheese = $this->ingredientFactory->createCheese();
$clam = $this->ingredientFactory->createClam();
}
}

$nyPizzaStore = new NYPizzaStore();
$nyPizzaStore->orderPizza('cheese');
?>


12下一页阅读全文
标签:
PHP,面向对象,设计模式

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。