php 抽象工厂模式,抽象工厂模式(Abstract Factory)

1.1.1. 目的

在不指定具体类的情况下创建一系列相关或依赖对象。 通常创建的类都实现相同的接口。 抽象工厂的客户并不关心这些对象是如何创建的,它只是知道它们是如何一起运行的。

1.1.2. UML 图

53679b50a9fe3f318357ff36612be5e8.png

1.1.3. 代码

你可以在 GitHub 上找到这个代码。

Product.php

namespace DesignPatterns\Creational\AbstractFactory;

interface Product

{

public function calculatePrice(): int;

}

ShippableProduct.php

namespace DesignPatterns\Creational\AbstractFactory;

class ShippableProduct implements Product

{

/**

* @var float

*/

private $productPrice;

/**

* @var float

*/

private $shippingCosts;

public function __construct(int $productPrice, int $shippingCosts)

{

$this->productPrice = $productPrice;

$this->shippingCosts = $shippingCosts;

}

public function calculatePrice(): int

{

return $this->productPrice + $this->shippingCosts;

}

}

DigitalProduct.php

namespace DesignPatterns\Creational\AbstractFactory;

class DigitalProduct implements Product

{

/**

* @var int

*/

private $price;

public function __construct(int $price)

{

$this->price = $price;

}

public function calculatePrice(): int

{

return $this->price;

}

}

ProductFactory.php

namespace DesignPatterns\Creational\AbstractFactory;

class ProductFactory

{

const SHIPPING_COSTS = 50;

public function createShippableProduct(int $price): Product

{

return new ShippableProduct($price, self::SHIPPING_COSTS);

}

public function createDigitalProduct(int $price): Product

{

return new DigitalProduct($price);

}

}

1.1.4. Test

Tests/AbstractFactoryTest.php

namespace DesignPatterns\Creational\AbstractFactory\Tests;

use DesignPatterns\Creational\AbstractFactory\DigitalProduct;

use DesignPatterns\Creational\AbstractFactory\ProductFactory;

use DesignPatterns\Creational\AbstractFactory\ShippableProduct;

use PHPUnit\Framework\TestCase;

class AbstractFactoryTest extends TestCase

{

public function testCanCreateDigitalProduct()

{

$factory = new ProductFactory();

$product = $factory->createDigitalProduct(150);

$this->assertInstanceOf(DigitalProduct::class, $product);

}

public function testCanCreateShippableProduct()

{

$factory = new ProductFactory();

$product = $factory->createShippableProduct(150);

$this->assertInstanceOf(ShippableProduct::class, $product);

}

public function testCanCalculatePriceForDigitalProduct()

{

$factory = new ProductFactory();

$product = $factory->createDigitalProduct(150);

$this->assertEquals(150, $product->calculatePrice());

}

public function testCanCalculatePriceForShippableProduct()

{

$factory = new ProductFactory();

$product = $factory->createShippableProduct(150);

$this->assertEquals(200, $product->calculatePrice());

}

}

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接

我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值