Skip to content

Commit 4bad149

Browse files
committed
feat(DI): readme & code & test
1 parent d19b829 commit 4bad149

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
- [x] 外观模式
2323
- [x] 享元模式
2424
- [x] 代理模式
25-
- [x] 流式接口(Fluent Interface)
25+
- [x] 流式接口
2626
- [x] 数据映射
27-
- [ ] 依赖注入
27+
- [x] 依赖注入
2828

2929
## 行为型
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Structural\DependencyInjection;
4+
5+
class DatabaseConfiguration
6+
{
7+
private $host;
8+
private $port;
9+
private $username;
10+
private $password;
11+
12+
public function __construct(string $host, int $port, string $username, string $password)
13+
{
14+
$this->host = $host;
15+
$this->port = $port;
16+
$this->username = $username;
17+
$this->password = $password;
18+
}
19+
20+
public function getHost(): string
21+
{
22+
return $this->host;
23+
}
24+
25+
public function getPort(): int
26+
{
27+
return $this->port;
28+
}
29+
30+
public function getUsername(): string
31+
{
32+
return $this->username;
33+
}
34+
35+
public function getPassword(): string
36+
{
37+
return $this->password;
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Structural\DependencyInjection;
4+
5+
class DatabaseConnection
6+
{
7+
private $configuration;
8+
9+
public function __construct(DatabaseConfiguration $config)
10+
{
11+
$this->configuration = $config;
12+
}
13+
14+
public function getDsn(): string
15+
{
16+
return sprintf(
17+
'%s:%s@%s:%d',
18+
$this->configuration->getUsername(),
19+
$this->configuration->getPassword(),
20+
$this->configuration->getHost(),
21+
$this->configuration->getPort()
22+
);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Structural\DependencyInjection\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Structural\DependencyInjection\DatabaseConfiguration;
7+
use Structural\DependencyInjection\DatabaseConnection;
8+
9+
class DependencyInjectionTest extends TestCase
10+
{
11+
public function testDependencyInjection()
12+
{
13+
$config = new DatabaseConfiguration('localhost', 3306, 'test', '123456');
14+
$connection = new DatabaseConnection($config);
15+
16+
$this->assertSame('test:123456@localhost:3306', $connection->getDsn());
17+
}
18+
}

Structural/README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
165165
## 数据映射器
166166

167-
* 简介
167+
* 简介(非经典设计模式)
168168

169169
数据映射器是一个**数据访问层**
170170
标是保持**内存中的表示形式****持久性数据存储区**彼此之间以及**数据映射器**本身之间**相互独立**
@@ -179,3 +179,21 @@
179179
* 示例
180180

181181
> 场景:ORM/DAO
182+
183+
## 依赖注入
184+
185+
* 简介(非经典设计模式)
186+
187+
**IoC(控制反转)**的最常见的实现方式。
188+
由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,**依赖被注入到对象中**
189+
许多框架已经具有用于依赖注入的容器,这些容器可通过配置数组创建对象并将其注入到需要的位置。
190+
191+
* 优点:
192+
1. 实现更好的可测试,可维护和可扩展的代码,实现松散耦合的体系结构。
193+
194+
* 缺点:
195+
1. 依赖过多会有很多参数需要在构造函数或方法中注入;创建依赖对象,使代码维护不太易。
196+
197+
* 示例
198+
199+
> 场景:数据库连接依赖数据库配置信息。

0 commit comments

Comments
 (0)