加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 21:23:59 所属栏目:PHP教程 来源:网络整理
导读:? 定义: 抽象工厂模式: 提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们的具体类。抽象工厂模式主要解决涉及到多个产品系列的问题。 ? 代码实例: 先回顾上一篇中工厂方法模式的例子,该示例以 BloggsCal 和 MegaCal 两种格式管理编码。如果

?

定义:

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

?

代码实例:

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

1、创建Appt 抽象类

/**
 * Appt 抽象类
 * Class ApptEncoder
 */
abstract class ApptEncoder
{
    public function encode();
}

2、创建Ttd 抽象类

*
 * Ttd 抽象类
 * Class TtdEncoder
  TtdEncoder
{
     encode();
}

3、创建Contact 抽象类

*
 * Contact 抽象类
 * Class ContactEncoder
  ContactEncoder
{
     encode();
}

4、创建BloggsCal Appt 格式处理类

*
 * BloggsCal Appt 格式处理类
 * Class BloggsApptEncoder
 class BloggsApptEncoder extends encode()
    {
        // TODO: Implement encode() method.
        return "Appointment data encoded in BloggsCal formatn";
    }
}

5、创建BloggsCal Ttd 格式处理类

*
 * BloggsCal Ttd 格式处理类
 * Class BloggsTtdEncoder
 class BloggsTtdEncoder return "Ttd data encoded in BloggsCal formatn";
    }
}

6、创建BloggsCal Contact 格式处理类

*
 * BloggsCal Contact 格式处理类
 * Class BloggsContactEncoder
 class BloggsContactEncoder return "Contact data encoded in BloggsCal formatn";
    }
}

7、创建MegaCal Appt 格式处理类

*
 * MegaCal Appt 格式处理类
 * Class MegaApptEncoder
 class MegaApptEncoder return "Appointment data encoded in MegaCal formatn";
    }
}

8、创建MegaCal Ttd 格式处理类

*
 * MegaCal Ttd 格式处理类
 * Class MegaTtdEncoder
 class MegaTtdEncoder return "Ttd data encoded in MegaCal formatn";
    }
}

9、创建MegaCal Contact 格式处理类

*
 * MegaCal Contact 格式处理类
 * Class MegaContactEncoder
 class MegaContactEncoder return "Contact data encoded in MegaCal formatn";
    }
}

10、创建BloggsCal 工厂类

*
 * BloggsCal 工厂类
 * Class BloggsCommsManager
 class BloggsCommsManager  CommsManager
{
     getHeaderText()
    {
         TODO: Implement getHeaderText() method.
        return "BloggsCal headern";
    }

     getApptEncoder()
    {
         TODO: Implement getApptEncoder() method.
        return new BloggsApptEncoder();
    }

     getTtdEncoder()
    {
         TODO: Implement getTtdEncoder() method.
         BloggsTtdEncoder();
    }

     getContractEncoder()
    {
         TODO: Implement getContractEncoder() method.
         BloggsContactEncoder();
    }

     getFooterText()
    {
         TODO: Implement getFooterText() method.
        return "BloggsCal footern";
    }
}

11、创建MegaCal 工厂类

*
 * MegaCal 工厂类
 * Class MegaCommsManager
 class MegaCommsManager return "MegaCal headern" MegaApptEncoder();
    }

     MegaTtdEncoder();
    }

     MegaContactEncoder();
    }

    return "MegaCal footern";
    }
}

12、调用

$mgr =  BloggsCommsManager();
print $mgr->getHeaderText();
$mgr->getApptEncoder()->encode();
$mgr->getTtdEncoder()->$mgr->getContractEncoder()->getFooterText();

$mega =  MegaCommsManager();
$mega->$mega->getApptEncoder()->$mega->getTtdEncoder()->$mega->getContractEncoder()->$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 
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、添加新产品会非常痛苦,这是因为我们不仅需要创建新产品的实现类,还需要修改抽象创建者及其所有的实现类来支持这个新产品。

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读