简单工厂模式
简单工厂也称静态工厂,不属于 23 种经典设计模式。简单工厂是设计模式中最容易理解的一种设计模式了。
我们先展示简单工厂的具体实现代码:
php
<?php
// +----------------------------------------------------------------------
// | 手机接口
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 16:15:38
// +----------------------------------------------------------------------
declare(strict_types=1);
interface Phone
{
public function show(): void;
}
php
<?php
// +----------------------------------------------------------------------
// | 苹果手机 - 继承手机接口
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 16:16:31
// +----------------------------------------------------------------------
declare(strict_types=1);
include_once __DIR__ . '/Phone.php';
class Apple implements Phone
{
public function show(): void
{
echo "我是苹果手机\n";
}
}
php
<?php
// +----------------------------------------------------------------------
// | 华为手机 - 继承手机接口
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 16:16:31
// +----------------------------------------------------------------------
declare(strict_types=1);
include_once __DIR__ . '/Phone.php';
class Huawei implements Phone
{
public function show(): void
{
echo "我是华为手机\n";
}
}
php
<?php
// +----------------------------------------------------------------------
// | 简单工厂类
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 16:15:02
// +----------------------------------------------------------------------
declare(strict_types=1);
include __DIR__ . '/Apple.php';
include __DIR__ . '/Huawei.php';
class SimpleFactory
{
public static function createProduct(string $type): Phone
{
return match ($type) {
'apple' => new Apple(),
'huawei' => new Huawei(),
default => null,
};
}
}
php
<?php
// +----------------------------------------------------------------------
// | 设计模式 [简单工厂模式]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 14:07:01
// +----------------------------------------------------------------------
declare(strict_types=1);
include __DIR__ . '/SimpleFactory.php';
/**
* 创建1个产品对象
* - 需要注意的是,php实例化类的时候,需要先加载类
* - 如果把实例化放在类前面就报错
*/
$product = SimpleFactory::createProduct('huawei');
$product->show();
$product = SimpleFactory::createProduct('apple');
$product->show();
代码就实现了创建对象的工作:根据我们传入的自定义标识符(apple、huawei),来返回对应类型的 Product 对象。
上面引入了 Phone 接口类,用于规定不同产品类型必须申明的方法
短信模块
场景:公司同时使用了阿里云、腾讯云、百度云 3 家的短信服务商,需要在不同场景下使用不同的短信服务商
php
<?php
// +----------------------------------------------------------------------
// | 短信服务商接口
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 16:58:28
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace sms;
interface Message
{
public function send(string $msg): void;
}
php
<?php
// +----------------------------------------------------------------------
// | 阿里云短信服务
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 17:00:20
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace sms;
include_once __DIR__ . '/Message.php';
class Ali implements Message
{
public function send(string $msg): void
{
echo "阿里云短信验证码:$msg\n";
}
}
php
<?php
// +----------------------------------------------------------------------
// | 百度云短信服务
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 17:00:20
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace sms;
include_once __DIR__ . '/Message.php';
class Baidu implements Message
{
public function send(string $msg): void
{
echo "百度云短信验证码:$msg\n";
}
}
php
<?php
// +----------------------------------------------------------------------
// | 腾讯云短信服务
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 17:00:20
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace sms;
include_once __DIR__ . '/Message.php';
class Tx implements Message
{
public function send(string $msg): void
{
echo "腾讯云短信验证码:$msg\n";
}
}
php
<?php
// +----------------------------------------------------------------------
// | 短信服务器商工厂类
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 17:03:07
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace sms;
include __DIR__ . '/Ali.php';
include __DIR__ . '/Tx.php';
include __DIR__ . '/Baidu.php';
class MessageFactory
{
public static function createFactory($type): Baidu|Ali|Tx|null
{
return match ($type) {
'ali' => new Ali(),
'baidu' => new Baidu(),
'tx' => new Tx(),
default => null,
};
}
}
php
<?php
// +----------------------------------------------------------------------
// | 设计模式 [简单工厂模式]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-2024 linjialiang All rights reserved.
// +----------------------------------------------------------------------
// | Author: linjialiang <linjialiang@163.com>
// +----------------------------------------------------------------------
// | CreateTime: 2023-09-12 14:07:01
// +----------------------------------------------------------------------
declare(strict_types=1);
include __DIR__ . '/MessageFactory.php';
use sms\MessageFactory;
/**
* 创建1个产品对象
* - 需要注意的是,php实例化类的时候,需要先加载类
* - 如果把实例化放在类前面就报错
*/
$sms = MessageFactory::createFactory('ali');
$sms->send('[123456]');
$sms = MessageFactory::createFactory('baidu');
$sms->send('[123456]');
$sms = MessageFactory::createFactory('tx');
$sms->send('[123456]');
创建工厂方法一定要使用 static 吗?
答:需要常驻的全部 static,如果是按需实例化的就 new 对象
,完了再使用 ->
去调用方法