Blogger Information
Blog 6
fans 3
comment 1
visits 8860
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
设计模式之工厂模式
齐天大圣的博客
Original
1529 people have browsed it

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

<?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);


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post