PHP设计模式学习
1.抽象类abstract
抽象函数:
abstract public function trick($whatever);
包含至少一个抽象函数的类就是抽象类,它其中也可以包含具体函数:
<?php abstract class OneTrickAbstract { public $storeHere; abstract public function trick($whatever); } ?>
抽象类的子类必须实现父类的抽象方法:
<?php include_once('OneTrickAbstract.php'); class OneTrickConcrete extends OneTrickAbstract { public function trick($whatever) { $this->storeHere="An abstract property"; return $whatever . $this->storeHere; } }
$worker=new OneTrickConcrete();
echo $worker->trick("From an abstract origin...");
?>
PHP不支持多重继承,但是支持使用Trait以实现同样的功能(没展开讲)。
2.接口interface
接口与抽象类中都包含有抽象函数,但是接口中不能包含变量和具体方法(一般用I开头的标示符):
<?php interface IMethodHolder { public function getInfo($info); public function sendInfo($info); public function calculate($first,$second); } ?>
接口的实现必须使用implements关键字:
<?php include_once('IMethodHolder.php'); class ImplementAlpha implements IMethodHolder { public function getInfo($info) { echo "This is NEWS! " . $info . "<br/>"; } public function sendInfo($info) { return $info; } public function calculate($first,$second) { $calulated = $first * $second; return $calulated; } public function useMethods() { $this->getInfo("The sky is falling..."); echo $this->sendInfo("Vote for Senator Snort!") . "<br/>"; echo "You make $" . $this->calculate(20,15) . " in your part-time job<br/>"; } } $worker=new ImplementAlpha(); $worker->useMethods(); ?>
接口中不能有变量,但是可以有常量:
<?php interface IConnectInfo { const HOST ="localhost"; const UNAME ="phpWorker"; const DBNAME = "dpPatt"; const PW ="easyWay"; function testConnection(); } ?>
在使用中,双冒号:
$server=IConnectInfo::HOST;
3.类型提示type hinting
看代码:
Interface //IProduct.php <?php interface IProduct { function apples(); function oranges(); } ?> FruitStore Implementation //FruitStore.php <?php include_once('IProduct.php'); class FruitStore implements IProduct { public function apples() { return "FruitStore sez--We have apples. <br/>"; } public function oranges() { return "FruitStore sez--We have no citrus fruit.<br/>"; } } ?> CitrusStore Implementation //CitrusStore.php <?php include_once('IProduct.php'); class CitrusStore implements IProduct { public function apples() { return "CitrusStore sez--We do not sell apples. <br/>"; } public function oranges() { return "CitrusStore sez--We have citrus fruit.<br/>"; } } ?> Object with type hinting //UseProducts.php <?php include_once('FruitStore.php'); include_once('CitrusStore.php'); class UseProducts { public function __construct() { $appleSauce=new FruitStore(); $orangeJuice=new CitrusStore(); $this->doInterface($appleSauce); $this->doInterface($orangeJuice); } //IProduct is type hint in doInterface() function doInterface(IProduct $product) { echo $product->apples(); echo $product->oranges(); } } $worker=new UseProducts(); ?>
类型提示一般用接口或抽象类,不能使用string、int等简单类型,可以使用数组类型。
上例中的类型提示,使得doInterface方法可以兼容两种具体实现的接口类。
4.设计模式中的原则
(1)针对接口或抽象类编程,而非针对其具体实现
(2)多使用类组合,少使用继承
5.类之间的关系
(1)交acquaintance:类A中包含类B的实例引用,UML图中由A指向B
(2)聚合aggregation:类A和类B息息相关,他们得生命周期是一致的。类A中使用了接口B,并且调用了接口B中的方法;此时,在Client使用类A时,必须提供一个实现了接口B的类C的实例,进而使用类A。(纯自我理解)。UML图中由A指向B,起始处有个菱形。
(3)继承:UML图中,使用三角形,并指向父类。
6.工厂模式
7.原型模式