From e53470e3bafddf2d70043c12daa1cb0e951535d2 Mon Sep 17 00:00:00 2001 From: zyimm Date: Fri, 18 Aug 2023 09:52:22 +0800 Subject: [PATCH 1/4] :memo:update document --- _posts/编程/设计模式/行为模式/Strategy.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/_posts/编程/设计模式/行为模式/Strategy.md b/_posts/编程/设计模式/行为模式/Strategy.md index 0659ffe..7a1fdca 100644 --- a/_posts/编程/设计模式/行为模式/Strategy.md +++ b/_posts/编程/设计模式/行为模式/Strategy.md @@ -7,6 +7,7 @@ tags: 设计模式 策略模式是一种行为设计模式, 它能让你定义一组算法和策略, 并将每种算法分别放入独立的类中, 根据不同场景使用不同算法和策略。 # 需求的场景 + 假设以需要一个服务或模块来实现消息通知功能,一开始只需要邮件通知,你实现发送邮件通知功能😀。。。。 然后几天后需求方提出能不能增加短信通知,你加班加点实现短信通知功能 😵。。。 @@ -18,7 +19,9 @@ tags: 设计模式 如果一开始代码都写在一起,每种消息通知方式不同整体数据结构也大相径庭,那么后期维护可想而知 💔 。。。 # 如何解决 + 将每种通知方式设为一种通知策略,因此在整体模块或服务设计时候,应该做好如下几点: + 1. 定义好策略统一入口 2. 定义好策略调度规则 3. 约定好策略算法接口 From c73e4668a85daf16b03c9e255ea9a3982a8b83df Mon Sep 17 00:00:00 2001 From: zyimm Date: Fri, 18 Aug 2023 09:53:39 +0800 Subject: [PATCH 2/4] :memo:update document --- _posts/编程/设计模式/行为模式/Strategy.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_posts/编程/设计模式/行为模式/Strategy.md b/_posts/编程/设计模式/行为模式/Strategy.md index 7a1fdca..19365a2 100644 --- a/_posts/编程/设计模式/行为模式/Strategy.md +++ b/_posts/编程/设计模式/行为模式/Strategy.md @@ -7,7 +7,6 @@ tags: 设计模式 策略模式是一种行为设计模式, 它能让你定义一组算法和策略, 并将每种算法分别放入独立的类中, 根据不同场景使用不同算法和策略。 # 需求的场景 - 假设以需要一个服务或模块来实现消息通知功能,一开始只需要邮件通知,你实现发送邮件通知功能😀。。。。 然后几天后需求方提出能不能增加短信通知,你加班加点实现短信通知功能 😵。。。 From 0de77c83fe4b624737a9249652ff447a33ab555e Mon Sep 17 00:00:00 2001 From: zyimm Date: Tue, 5 Sep 2023 17:01:25 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=98=BA=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E5=B7=A5=E5=8E=82=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../编程/设计模式/行为模式/factory-method.md | 168 ++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 _posts/编程/设计模式/行为模式/factory-method.md diff --git a/_posts/编程/设计模式/行为模式/factory-method.md b/_posts/编程/设计模式/行为模式/factory-method.md new file mode 100644 index 0000000..4e48010 --- /dev/null +++ b/_posts/编程/设计模式/行为模式/factory-method.md @@ -0,0 +1,168 @@ +--- +title: 💫设计模式-工厂模式 +date: 2023-09-05 +tags: 设计模式 +--- + +**工厂模式**是一种创建型设计模式, 其在父类中提供一个创建对象的方法, 允许子类决定实例化对象的类型。 + +# 需求的场景 +这种模式在面试中经常会被问到,很多面试题的答案会表示该模式经常用于框架的db或cache组件设计。 + +假设你所在项目组在开发某个项目中,使用了多种缓存数据源,比如有内存,redis,本地文件。目前每次根据不同场景使用不同类型缓存,需要实例化不同缓存实例进行操作,比较繁琐。项目组开发人员希望统一调用缓存入口,简化缓存调用心智负担。 + + +# 如何解决 +1. 定义cache工厂类(父类)和依赖类 +2. 编写各个类型cache子类 +3. cache工厂类创建调用 + +# 实现 + +1. 😼定义cache工厂类(父类)和依赖类 + +```php +class Config +{ + private string $host; //连接host + + private string $dbName;// 数据库名称 + + private string $password;// 密码 + + private string $userName;// 用户名 + + public function getHost(): string + { + return $this->host; + } + + public function setHost(string $host): static + { + $this->host = $host; + return $this; + } + + public function getDbName(): string + { + return $this->dbName; + } + + public function setDbName(string $dbName): static + { + $this->dbName = $dbName; + return $this; + } + + public function getPassword(): string + { + return $this->password; + } + + public function setPassword(string $password): static + { + $this->password = $password; + return $this; + } + + public function getUserName(): string + { + return $this->userName; + } + + public function setUserName(string $userName): static + { + $this->userName = $userName; + return $this; + } +} + +``` + +2. 😸编写各个类型cache子类 +```php +class Cache +{ + protected Config $config; + + public function __construct(Config $config) + { + $this->config = $config; + } + + public static create(string $cache) + { + return new $cache($this->config); + } + + +} + + +class Redis extends Cache +{ + + public function __construct(Config $config) + { + parent::__construct(Config $config); + + $this->connect() + } + + protected function connect() + { + //根据Config进行缓存连接 + } +} + + +class Memory extends Cache +{ + + public function __construct(Config $config) + { + + parent::__construct(Config $config); + + } + + +} + +class File extends Cache +{ + + private string $cachePath = '/dev/logs/' + + public function __construct(Config $config) + { + parent::__construct(Config $config); + + + } + + +} + +``` + +3. 😺cache工厂类创建调用 + +```php +class Demo +{ + public function run() + { + $config = (new Config())->setHost('localhost') + ->setUserName('demo') + ->setPassword('*****'); + + $cache = new Cache($config); + //创建redis cache实例 + $redis = $cache->create(Redis::class); + //创建本地file cache实例 + $file = $cache->create(File::class); + } +} + +``` From 90b427205e95ba08edc9042aee0737dbf560e80b Mon Sep 17 00:00:00 2001 From: zyimm Date: Tue, 5 Sep 2023 17:02:05 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=98=BA=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E5=B7=A5=E5=8E=82=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _posts/编程/设计模式/行为模式/factory-method.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/编程/设计模式/行为模式/factory-method.md b/_posts/编程/设计模式/行为模式/factory-method.md index 4e48010..1a8e443 100644 --- a/_posts/编程/设计模式/行为模式/factory-method.md +++ b/_posts/编程/设计模式/行为模式/factory-method.md @@ -1,6 +1,6 @@ --- title: 💫设计模式-工厂模式 -date: 2023-09-05 +date: 2023-09-02 tags: 设计模式 ---