Home php教程 php手册 从魔兽争霸看PHP设计模式

从魔兽争霸看PHP设计模式

Jun 21, 2016 am 08:56 AM
php

前段时间看到有人用魔兽来解释设计模式,感觉很有意思,于是我把它改了改,又添加了些设计模式内容,今天发出来。有些地方借鉴了前人的内容,没有注明,请前人不要见怪啊。

这里用大家感兴趣的魔兽3来讨论PHP的几种常见的设计模式:单件模式、策略模式、工厂模式、观察者模式。今天就讲这四个吧,以后继续。

这些设计模式,都是针对面向对象来说的,所以都用PHP5,另外在这里我想说的是PHP4从2008年8月8日(我记得是和北京奥运会同一天,没查证,呵呵)的时候官方就发了最后一个PHP4的补丁,这意味这PHP4的时代已经终结,所以,我建议大家现在就别理PHP4吧,就以PHP5来说吧。

一、单件模式:

问题的提出:

某些应用程序资源是独占的,因为有且只有一个此类型的资源。例如,通过数据库句柄到数据库的连接是独占的。您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销,在获取单个页面的过程中更是如此。

问题的解决:

那么下面我们就开始玩魔兽吧。首先双击war3.exe,这时候就开始运行魔兽了。我们用代码来实现吧。

class War3
{

public function __construct()

{

echo "War3 is Running.","
";

}
}
$war = new War3();

运行!很好,输出

War3 is Running.

我们已经可以开始游戏了,但是,如果我在代码末尾再加入

$war2 = new War3();

$war3 = new War3();

会怎么样呢?我们试试,输出结果:

War3 is Running.

War3 is Running.

War3 is Running.

完了,如果不小心双击了两次就开了3个魔兽,那如果再双击几次,那电脑肯定爆掉。。。我们还是来想想解决方法吧。

既然我们不能这么随意的就把这个类实例化了,那么我们就把构造函数改成私有方法。

class War3
{

private function __construct()

{

echo "War3 is Running.","
";

}
}

可是私有变量外部是无法访问的,这样以来,我们就连一个都打不开了啊。别急,我们再给他加一个不用通过实例化,外部也能访问的函数,那就是静态函数,
class War3
{

private function __construct()

{

echo "War3 is Running.","
";

}

public static function runWar()

{

}
}

通过这个静态的方法runWar()我们来控制类War3的实例化,那么还缺上一个标识,我们再创建一个标识,通过这个标识来表示我们的类是否已经实例化,如果实例化,直接返回句柄就行了。

把类修改成

class War3
{

protected static $_instance = null;

private function __construct()

{

echo "War3 is Running.","
";

}

public static function runWar()

{

if (null === self::$_instance) {

self::$_instance = new self();

}


return self::$_instance;

}
}

当然,我们运行魔兽时的实例化也要换种方法,就通过
$war = War3::runWar();
就能开始玩魔兽了,好了,下面把完整的代码附上来:

class War3
{

protected static $_instance = null;

private function __construct()

{

echo "War3 is Running.","
";

}

public static function runWar()

{

if (null === self::$_instance) {

self::$_instance = new self();

}


return self::$_instance;

}
}

$war = War3::runWar();
$war2 = War3::runWar();
$war3 = War3::runWar();

运行一下,结果是:

War3 is Running.

太好了,我双击了这么多次,也就只运行了一个魔兽,现在随便你怎么打开,机子都不会爆掉了。

这就是传说中的单价模式,主要用于一些很占资源的而且实例仅有一个实例就够用的东西,比如,zend framework中的Zend_Controller_Front前端控制器,就是采用单价模式来设计的,大家有兴趣的话可以看看那个。

二、策略模式:

问题的提出:

在此模式中,算法是从复杂类提取的,因而可以方便地替换。例如,如果要更改搜索引擎中排列页的方法,则策略模式是一个不错的选择。思考一下搜索引擎的几个部分 —— 一部分遍历页面,一部分对每页排列,另一部分基于排列的结果排序。在复杂的示例中,这些部分都在同一个类中。通过使用策略模式,您可将排列部分放入另一个类中,以便更改页排列的方式,而不影响搜索引擎的其余代码。

问题的解决:

呵呵,不讲那么复杂,刚才魔兽好不容易打开了,我们还是玩魔兽好了。
下面我们选battle,哇好多种族啊,有人族(Human),兽族(ORC),暗夜精灵族(Nighy Elf),不死族(Undead)。我选精灵族(Nighy Elf),再选一个精灵族和两个兽族(ORC),一个兽族和我是一家的,另一个精灵族和兽族是另一家的。

每一个玩家在进入游戏后都会得到一些资源,如一个大厅,五个小精灵(苦工)和一个矿山。这些可以称为是初始化的一些东西,这里我们就可以用到策略模式来封装这些初始化。

进入正题,首先我们来构建一个玩家类:

class player
{

//玩家名字

protected $_name;

//种族

protected $_race;

//队伍

protected $army;

//建筑

protected $building;

//人口

protected $population;

//黄金

protected $gold;

//木材

protected $wood;

//构造函数,设定所属种族

public function __construct($race)

{

$this->race = $race;


}

//__get()方法用来获取保护属性

private function __get($property_name)

{

if(isset($this->$property_name)) {

return($this->$property_name);

}

else {

return(NULL);

}

}

//__set()方法用来设置保护属性

private function__set($property_name,$value)

{

$this->$property_name=$value;

}
}

接着,我们再建一个玩家初始化的接口,

interface initialPlayer
{

//制造初始化的部队

public function giveArmy($player);

//制造初始化的建筑

public function giveBuilding($player);

//初始化资源

public function giveSource($player);
}

好了,到这里我们就该对这个接口来实现了,为了方便,我只选了两个种族,就只写这两个种族的初始化了:

首先是精灵族:

class NighyElfInitial implements initialPlayer
{

//制造初始化的部队

public function giveArmy($player)

{

//五个小精灵

for($i=0; $i

{

$creator = new CreatArms();//这个是创建部队类,在后面得工厂模式中会用到,这里我就不多说了

$player->army[] = $creator->Creat('Wisp','./Arms/');

}

}

//制造初始化的建筑

public function giveBuilding($player)

{

$creator = new CreatBuildings();

//一个基地

$player->building[] = $creator->Creat('TownHall','./Buildings/');

//一个矿场

$player->building[] = $creator->Creat('Mine','./Buildings/');

}

//初始化人口上限

public function giveSource($player)

{

$player->population= 10;

$player->gold= 1000;

$player->wood= 100;

}
}

接下来是兽族:

class ORCInitial implements initialPlayer
{

//制造初始化的部队

public function giveArmy($player)

{

//五个苦工

for($i=0; $i

{

$creator = new CreatArms();//这个是创建部队类,在后面得工厂模式中会用到,这里我就不多说了

$player->army[] = $creator->Creat('Peon','./Arms/');

}

}

//制造初始化的建筑

public function giveBuilding($player)

{


$creator = new CreatBuildings();

//一个基地

$player->building[] = $creator->Creat('TownHall','./Buildings/');

//一个矿场

$player->building[] = $creator->Creat('Mine','./Buildings/');

}

//初始化人口上限

public function giveSource($player)

{

$player->population= 10;

$player->gold= 1000;

$player->wood= 100;

}
}



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles