😺设计模式-工厂模式更新
This commit is contained in:
parent
79f3969dee
commit
934c7bff78
@ -24,6 +24,7 @@ tags: 设计模式
|
|||||||
**😼1.定义cache工厂类(父类)和依赖类**
|
**😼1.定义cache工厂类(父类)和依赖类**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
|
// config 配置类 用于缓存实例化依赖配置数据
|
||||||
class Config
|
class Config
|
||||||
{
|
{
|
||||||
private string $host; //连接host
|
private string $host; //连接host
|
||||||
@ -79,29 +80,34 @@ class Config
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
// 工厂类
|
||||||
|
|
||||||
**😸2.编写各个类型cache子类**
|
|
||||||
|
|
||||||
```php
|
|
||||||
class Cache
|
class Cache
|
||||||
{
|
{
|
||||||
protected Config $config;
|
protected Config $config;
|
||||||
|
|
||||||
public function __construct(Config $config)
|
public function __construct(Config $config)
|
||||||
{
|
{
|
||||||
|
//通过构造注入Config依赖
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static create(string $cache)
|
public static create(string $cache)
|
||||||
{
|
{
|
||||||
return new $cache($this->config);
|
//创建实例
|
||||||
|
return new $cache($this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
**😸2.编写各个类型cache子类**
|
||||||
|
|
||||||
|
```php
|
||||||
|
|
||||||
class Redis extends Cache
|
class Redis extends Cache
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user