当前位置: 移动技术网 > IT编程>软件设计>设计模式 > PHP设计模式—工厂模式之抽象工厂模式

PHP设计模式—工厂模式之抽象工厂模式

2020年03月30日  | 移动技术网IT编程  | 我要评论

 

定义:

抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们的具体类。抽象工厂模式主要解决涉及到多个产品系列的问题。

 

代码实例:

先回顾上一篇中工厂方法模式的例子,该示例以 bloggscal 和 megacal 两种格式管理编码。如果增加更多的编码格式,这种类结构会横向增长,但如何为不同类型的 pim 对象加入编码器,使类结构“纵向”增长呢?
这里将会用到三个相似的类层次结构,分别是预约(appt)、待办事项(ttd)以及联系人(contact)。

1、创建appt 抽象类

/**
 * appt 抽象类
 * class apptencoder
 */
abstract class apptencoder
{
    abstract public function encode();
}

2、创建ttd 抽象类

/**
 * ttd 抽象类
 * class ttdencoder
 */
abstract class ttdencoder
{
    abstract public function encode();
}

3、创建contact 抽象类

/**
 * contact 抽象类
 * class contactencoder
 */
abstract class contactencoder
{
    abstract public function encode();
}

4、创建bloggscal appt 格式处理类

/**
 * bloggscal appt 格式处理类
 * class bloggsapptencoder
 */
class bloggsapptencoder extends apptencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "appointment data encoded in bloggscal format\n";
    }
}

5、创建bloggscal ttd 格式处理类

/**
 * bloggscal ttd 格式处理类
 * class bloggsttdencoder
 */
class bloggsttdencoder extends ttdencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "ttd data encoded in bloggscal format\n";
    }
}

6、创建bloggscal contact 格式处理类

/**
 * bloggscal contact 格式处理类
 * class bloggscontactencoder
 */
class bloggscontactencoder extends contactencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "contact data encoded in bloggscal format\n";
    }
}

7、创建megacal appt 格式处理类

/**
 * megacal appt 格式处理类
 * class megaapptencoder
 */
class megaapptencoder extends apptencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "appointment data encoded in megacal format\n";
    }
}

8、创建megacal ttd 格式处理类

/**
 * megacal ttd 格式处理类
 * class megattdencoder
 */
class megattdencoder extends ttdencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "ttd data encoded in megacal format\n";
    }
}

9、创建megacal contact 格式处理类

/**
 * megacal contact 格式处理类
 * class megacontactencoder
 */
class megacontactencoder extends contactencoder
{
    public function encode()
    {
        // todo: implement encode() method.
        return "contact data encoded in megacal format\n";
    }
}

10、创建bloggscal 工厂类

/**
 * bloggscal 工厂类
 * class bloggscommsmanager
 */
class bloggscommsmanager extends commsmanager
{
    public function getheadertext()
    {
        // todo: implement getheadertext() method.
        return "bloggscal header\n";
    }

    public function getapptencoder()
    {
        // todo: implement getapptencoder() method.
        return new bloggsapptencoder();
    }

    public function getttdencoder()
    {
        // todo: implement getttdencoder() method.
        return new bloggsttdencoder();
    }

    public function getcontractencoder()
    {
        // todo: implement getcontractencoder() method.
        return new bloggscontactencoder();
    }

    public function getfootertext()
    {
        // todo: implement getfootertext() method.
        return "bloggscal footer\n";
    }
}

11、创建megacal 工厂类

/**
 * megacal 工厂类
 * class megacommsmanager
 */
class megacommsmanager extends commsmanager
{
    public function getheadertext()
    {
        // todo: implement getheadertext() method.
        return "megacal header\n";
    }

    public function getapptencoder()
    {
        // todo: implement getapptencoder() method.
        return new megaapptencoder();
    }

    public function getttdencoder()
    {
        // todo: implement getttdencoder() method.
        return new megattdencoder();
    }

    public function getcontractencoder()
    {
        // todo: implement getcontractencoder() method.
        return new megacontactencoder();
    }

    public function getfootertext()
    {
        // todo: implement getfootertext() method.
        return "megacal footer\n";
    }
}

12、调用

$mgr = new bloggscommsmanager();
print $mgr->getheadertext();
print $mgr->getapptencoder()->encode();
print $mgr->getttdencoder()->encode();
print $mgr->getcontractencoder()->encode();
print $mgr->getfootertext();

$mega = new megacommsmanager();
print $mega->getheadertext();
print $mega->getapptencoder()->encode();
print $mega->getttdencoder()->encode();
print $mega->getcontractencoder()->encode();
print $mega->getfootertext();

13、结果

bloggscal header
appointment data encoded in bloggscal format
ttd data encoded in bloggscal format
contact data encoded in bloggscal format
bloggscal footer
megacal header
appointment data encoded in megacal format
ttd data encoded in megacal format
contact data encoded in megacal format
megacal footer

 

总结:

1、解除了系统与实现细节间的耦合。我们可以在示例程序中添加或移除任何数量的编码类型,而不会对系统造成任何影响。
2、我们组合了系统中功能相关的元素。因此,bloggscommsmanager 可以确保只使用与 bloggscal 格式相关的类。
3、添加新产品会非常痛苦,这是因为我们不仅需要创建新产品的实现类,还需要修改抽象创建者及其所有的实现类来支持这个新产品。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网