Skip to content

Commit 96798e6

Browse files
author
chenchangqin
committed
develop#创建型(Creational) finish
1 parent d55e4bc commit 96798e6

File tree

9 files changed

+225
-0
lines changed

9 files changed

+225
-0
lines changed

Creational/Singleton/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# PHP 设计模式 —— 单例模式(Singleton)
2+
## 1、模式定义
3+
简单说来,单例模式的作用就是保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点。
4+
5+
常见使用实例:数据库连接器;日志记录器(如果有多种用途使用多例模式);锁定文件。
6+
7+
## 2、UML类图
8+
![Singleton-UML.png](/static/images/Singleton-UML.png)

Creational/Singleton/Singleton.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:23
7+
*/
8+
9+
namespace Creational\Singleton;
10+
11+
/**
12+
* Singleton类
13+
*/
14+
class Singleton
15+
{
16+
/**
17+
* @var Singleton reference to singleton instance
18+
*/
19+
private static $instance;
20+
21+
/**
22+
* 通过延迟加载(用到时才加载)获取实例
23+
*
24+
* @return self
25+
*/
26+
public static function getInstance()
27+
{
28+
if (null === static::$instance) {
29+
static::$instance = new static;
30+
}
31+
32+
return static::$instance;
33+
}
34+
35+
/**
36+
* 构造函数私有,不允许在外部实例化
37+
*
38+
*/
39+
private function __construct()
40+
{
41+
}
42+
43+
/**
44+
* 防止对象实例被克隆
45+
*
46+
* @return void
47+
*/
48+
private function __clone()
49+
{
50+
}
51+
52+
/**
53+
* 防止被反序列化
54+
*
55+
* @return void
56+
*/
57+
private function __wakeup()
58+
{
59+
}
60+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:40
7+
*/
8+
9+
namespace Creational\StaticFactory;
10+
11+
12+
class FormatInteger implements FormatterInterface
13+
{
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:36
7+
*/
8+
9+
namespace Creational\StaticFactory;
10+
11+
12+
class FormatString implements FormatterInterface
13+
{
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:36
7+
*/
8+
9+
namespace Creational\StaticFactory;
10+
11+
12+
interface FormatterInterface
13+
{
14+
15+
}

Creational/StaticFactory/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# PHP 设计模式 —— 静态工厂模式(Static Factory)
2+
## 1、模式定义
3+
与简单工厂类似,该模式用于创建一组相关或依赖的对象,不同之处在于静态工厂模式使用一个静态方法来创建所有类型的对象,该静态方法通常是 factory 或 build。
4+
5+
## 2、UML类图
6+
![Static-Factory-UML.png](/static/images/Static-Factory-UML.png)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:35
7+
*/
8+
9+
namespace Creational\StaticFactory;
10+
11+
12+
use phpDocumentor\Reflection\Types\Integer;
13+
14+
class StaticFactory
15+
{
16+
/**
17+
* 通过传入参数创建相应对象实例
18+
*
19+
* @param string $type
20+
*
21+
* @static
22+
*
23+
* @throws \InvalidArgumentException
24+
* @return FormatterInterface
25+
*/
26+
public static function factory($type)
27+
{
28+
$className = __NAMESPACE__ . '\Format' . ucfirst($type);
29+
30+
if (!class_exists($className)) {
31+
throw new \InvalidArgumentException('Missing format class.');
32+
}
33+
34+
return new $className();
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:26
7+
*/
8+
9+
namespace Tests\Creational\SingletonTest;
10+
11+
use Creational\Singleton\Singleton;
12+
13+
class SingletonTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function testUniqueness()
16+
{
17+
$firstCall = Singleton::getInstance();
18+
$this->assertInstanceOf(Singleton::class, $firstCall);
19+
$secondCall = Singleton::getInstance();
20+
$this->assertSame($firstCall, $secondCall);
21+
}
22+
23+
public function testNoConstructor()
24+
{
25+
$obj = Singleton::getInstance();
26+
27+
$refl = new \ReflectionObject($obj);
28+
$meth = $refl->getMethod('__construct');
29+
$this->assertTrue($meth->isPrivate());
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: chenchangqin
5+
* Date: 2018/12/27
6+
* Time: 14:37
7+
*/
8+
9+
namespace Tests\Creational\StaticFactoryTest;
10+
11+
use Creational\StaticFactory\StaticFactory;
12+
13+
class StaticFactoryTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function getTypeList()
16+
{
17+
return array(
18+
array('string'),
19+
array('integer')
20+
);
21+
}
22+
23+
/**
24+
* @dataProvider getTypeList
25+
*/
26+
public function testCreation($type)
27+
{
28+
$obj = StaticFactory::factory($type);
29+
$this->assertInstanceOf('Creational\StaticFactory\FormatterInterface', $obj);
30+
}
31+
32+
/**
33+
* @expectedException \InvalidArgumentException
34+
*/
35+
public function testException()
36+
{
37+
StaticFactory::factory("");
38+
}
39+
}

0 commit comments

Comments
 (0)