设计模式之工厂模式

2019年06月16日 15:10:49阅读数:1402博客 / 齐天大圣的博客 / php设计模式

工厂模式与简单工厂模式相比,多了一个抽象工厂类。

<?php

interface IMath
{
    function count($num1, $num2);
}

class Add implements IMath
{
    public function count($num1, $num2)
    {
        return $num1 + $num2;
    }
}

class Sub implements IMath
{
    public function count($num1, $num2)
    {
        return $num1 - $num2;
    }
}

interface IFactory
{
    static function getObj ();
}

class FacAdd implements IFactory
{
    public static function getObj()
    {
        return new Add();
    }
}

class FacSub implements IFactory
{
    public static function getObj()
    {
        return new Sub();
    }
}

$math = FacSub::getObj();
echo $math->count(10, 20);


版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • 博主信息
    齐天大圣的博客
    博文
    6
    粉丝
    3
    评论
    1
    访问量
    7983
    积分:0
    P豆:230