Skip to content

Commit 13db8fc

Browse files
author
zhouqiang
committed
git init
0 parents  commit 13db8fc

20 files changed

+1465
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/example/

Adapter.class.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* 适配器模式
4+
* 解决问题:在转换一个对象的接口用于另一个对象时,实现Adapter对象不仅是最佳做法,而且也能减少很多麻烦
5+
*/
6+
class errorObject
7+
{
8+
private $__error;
9+
public function __construct($error)
10+
{
11+
$this->__error = $error;
12+
}
13+
public function getError()
14+
{
15+
return $this->__error;
16+
}
17+
}
18+
19+
//原需求
20+
class logToConsole
21+
{
22+
private $__errorObject;
23+
public function __construct($errorObject)
24+
{
25+
$this->__errorObject = $errorObject;
26+
}
27+
28+
public function write()
29+
{
30+
fwrite(STDERR, $this->__errorObject->getError());
31+
}
32+
}
33+
34+
//新需求改变了
35+
class logToCSV
36+
{
37+
const CSV_LOCATION = "log.csv";
38+
private $__errorObject;
39+
public function __construct($errorObject)
40+
{
41+
$this->__errorObject = $errorObject;
42+
}
43+
44+
public function write()
45+
{
46+
$line = $this->__errorObject->getErrorNumber();
47+
$line .= ",";
48+
$line .= $this->__errorObject->getErrorText();
49+
$Line .= "\n";
50+
file_put_contents(self::CSV_LOCATION, $line, FILE_APPEND);
51+
}
52+
}
53+
54+
//增加适配器,保持公共接口的标准性
55+
class logToCsvAdapter extends errorObject
56+
{
57+
private $__errorNumber, $__errorText;
58+
public function __construct($error)
59+
{
60+
parent::__construct($error);
61+
$parts = explode(":", $this->getError());
62+
$this->__errorNumber = $parts[0];
63+
$this->__errorText = $parts[1];
64+
}
65+
66+
public function getErrorNumber()
67+
{
68+
return $this->__errorNumber;
69+
}
70+
71+
public function getErrorText()
72+
{
73+
return $this->__errorText;
74+
}
75+
}
76+
77+
//$error = new errorObject("404:Not Found");
78+
//$log = new logToConsole($error);
79+
//使用适配器来更新接口
80+
$error = new logToCsvAdapter("404:Not Found");
81+
$log = logToCSV($error);
82+
$log->write();
83+
84+
?>

Build.class.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* 建造者模式
4+
* 解决问题:消除其他对象的复杂创建过程,这是最佳做法,而且在对象的构造和配置方法改变时,可以尽可能的减少重复更改代码。
5+
*/
6+
7+
class product
8+
{
9+
protected $_type = "";
10+
protected $_size = "";
11+
protected $_color = "";
12+
13+
//假设有三个复杂的创建过程
14+
public function setType($type)
15+
{
16+
$this->_type = $type;
17+
}
18+
public function setSize($size)
19+
{
20+
$this->_size = $size;
21+
}
22+
public function setColor($color)
23+
{
24+
$this->_color = $color;
25+
}
26+
}
27+
28+
$productConfigs = array('type'=>'shirt','size'=>'XL','color'=>'red');
29+
30+
$product = new product();
31+
//创建对象时分别调用每个方法不是最佳做法,扩展和可适应性低
32+
$product->setType($productConfigs['type']);
33+
$product->setSize($productConfigs['size']);
34+
$product->setColor($productConfigs['color']);
35+
//复杂的创建过程中使用构造函数来实现更不可取。
36+
37+
38+
//建造者模式
39+
class productBuilder
40+
{
41+
protected $_product = null;
42+
protected $_configs = array();
43+
44+
public function __construct($configs)
45+
{
46+
$this->_product = new product();
47+
$this->_configs = $configs;
48+
}
49+
50+
public function build()
51+
{
52+
$this->_product->setSize($this->_configs['size']);
53+
$this->_product->setType($this->_configs['type']);
54+
$this->_product->setColor($this->_configs['color']);
55+
}
56+
57+
public function getProduct()
58+
{
59+
return $this->_product;
60+
}
61+
}
62+
63+
$builder = new productBuilder($productConfigs);
64+
$builder->build();
65+
$product = $builder->getProduct();
66+

Dao.class.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* 数据访问对象模式
4+
* 解决问题:如何创建透明访问任何数据源的对象(重复和数据源抽象化)
5+
*/
6+
7+
abstract class baseDAO
8+
{
9+
private $__connection;
10+
public function __construct()
11+
{
12+
$this->__connectToDB(DB_USER, DB_PASS, DB_HOST, DB_DATABASE);
13+
}
14+
15+
private function __connectToDB($user, $pass, $host, $database)
16+
{
17+
$this->__connection = mysql_connect($host, $user, $pass);
18+
mysql_select_db($database, $this->__connection);
19+
}
20+
21+
public function fetch($value, $key = NULL)
22+
{
23+
if(is_null($key))
24+
{
25+
$key = $this->_primaryKey;
26+
}
27+
28+
$sql = "select * from {$this->_tableName} where {$key}='{$value}'";
29+
$results = mysql_query($sql, $this->__connection);
30+
31+
$rows = array();
32+
while ($result = mysql_fetch_array($results))
33+
{
34+
$rows[] = $result;
35+
}
36+
return $rows;
37+
}
38+
39+
public function update($keyedArray)
40+
{
41+
$sql = "update {$this->_tableName} set ";
42+
foreach ($keyedArray as $column=>$value)
43+
{
44+
$updates[] = "{$column} = '{$value}' ";
45+
}
46+
$sql .= implode(",",$updates);
47+
$sql .= "where {$this->_primaryKey}='{$keyedArray[$this->_primaryKey]}'";
48+
mysql_query($sql, $this->__connection);
49+
50+
}
51+
}
52+
53+
class userDAO extends baseDAO
54+
{
55+
protected $_tableName = "userTable";
56+
protected $_primaryKey = "id";
57+
58+
public function getUserByFirstName($name)
59+
{
60+
$result = $this->fetch($name, 'firstName');
61+
return $result;
62+
}
63+
}
64+
65+
66+
$user = new userDAO();
67+
$id = 1;
68+
$userInfo = $user->fetch($id);
69+
70+
$updates = array('id'=>1, 'firstName'=>'arlon');
71+
$user->update($updates);
72+
73+
$all = $user->getUserByFirstName('arlon');

Decorator.class.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
*
4+
* 装饰器设计模式适用于下列工作场合: 需求变化是快速和细小的,而且几乎不影响应用程序的其他部分
5+
* 使用装饰器设计模式设计类的目标是: 不必重写任何已有的功能性代码,而是对某个基于对象应用增量变化。
6+
* 装饰器设计模式采用这样的构建方式: 在主代码流中应该能够直接插入一个或多个更改或“装饰”目标对象的装饰器,同时不影响其他代码流。
7+
*
8+
*/
9+
class CD {
10+
public $trackList;
11+
12+
public function __construct() {
13+
$this->trackList = array();
14+
}
15+
16+
public function addTrack($track) {
17+
$this->trackList[] = $track;
18+
}
19+
20+
public function getTrackList() {
21+
$output = '';
22+
23+
foreach ($this->trackList as $num => $track) {
24+
$output .= ($num + 1) . ") {$track}.";
25+
}
26+
27+
return $output;
28+
}
29+
}
30+
31+
$tracksFroExternalSource = array("What It Means", "Brr", "Goodbye");
32+
33+
$myCD = new CD();
34+
foreach ($tracksFroExternalSource as $track) {
35+
$myCD->addTrack($track);
36+
}
37+
38+
print "The CD contains:{$myCD->getTrackList()}\n";
39+
40+
/**
41+
* 需求发生小变化: 要求每个输出的参数都采用大写形式. 对于这么小的变化而言, 最佳的做法并非修改基类或创建父 - 子关系,
42+
* 而是创建一个基于装饰器设计模式的对象。
43+
*
44+
*/
45+
class CDTrackListDecoratorCaps {
46+
private $_cd;
47+
48+
public function __construct(CD $cd) {
49+
$this->_cd = $cd;
50+
}
51+
52+
public function makeCaps() {
53+
foreach ($this->_cd->trackList as & $track) {
54+
$track = strtoupper($track);
55+
}
56+
}
57+
}
58+
59+
$myCD = new CD();
60+
foreach ($tracksFroExternalSource as $track) {
61+
$myCD->addTrack($track);
62+
}
63+
64+
//新增以下代码实现输出参数采用大写形式
65+
$myCDCaps = new CDTrackListDecoratorCaps($myCD);
66+
$myCDCaps->makeCaps();
67+
68+
print "The CD contains:{$myCD->getTrackList()}\n";

0 commit comments

Comments
 (0)