php工厂模式设计问题

php工厂模式设计问题,php,Php,我正在学习使用php进行工厂模式设计。我的应用程序所做的是显示不同的形状名称,如矩形、正方形等及其x、y位置。第一个用户将提供x和y值,该值将显示在形状名称旁边,如下面的矩形(2,2) 我当前代码的问题是,它确实显示形状名称,即矩形,但它不显示用户提供的x和y值。为什么以及如何显示x和y值 下面是我的代码 index.php include_once("ConcreteCreator.php"); class Client { public function __construc

我正在学习使用php进行工厂模式设计。我的应用程序所做的是显示不同的形状名称,如
矩形、正方形等
及其
x、y
位置。第一个用户将提供x和y值,该值将显示在形状名称旁边,如下面的
矩形(2,2)

我当前代码的问题是,它确实显示形状名称,即
矩形
,但它不显示用户提供的x和y值。为什么以及如何显示x和y值

下面是我的代码 index.php

    include_once("ConcreteCreator.php");
class Client {
    public function __construct() {
        $shapeNow = new ConcreteCreator();
        $display  = $shapeNow->factoryMethod(new Rectangle(2, 2));
        //Above I am passing x and y values in Rectangle(2, 2);
        echo $display . '<br>';
    }    
}

$worker = new Client();
下面是Creator.php

    include_once('Creator.php');
include_once('Rectangle.php');
class ConcreteCreator extends Creator {

    public function factoryMethod(Product $productNow) {
        //echo $productNow;
        $this->createdProduct = new $productNow;
        return $this->createdProduct->provideShape();
    }

}
abstract class Creator {
    protected $createdProduct;
    abstract public function factoryMethod(Product $productNow);
}
下面是Product.php

interface Product {
    function provideShape();
}
这里是Rectangle.php

include_once('Product.php');

class Rectangle implements Product {
    private $x;
    private $y;
            //Here is I am setting users provided x and y values with private vars
    public function __construct($x, $y) {
        echo $x;
        $this->x = $x;
        $this->y = $y;
    }

    public function provideShape() {
        echo $this->x; //Here x does not show up
        return 'Rectangle (' . $this->x . ', ' . $this->y . ')';
                    //above x and y does not show up.
    }
}

使用factory的示例可以让您了解如何做

class Shape{
    int x,y
}

class Rectangle implements Shape{

}

class Circle implements Shape{

}
class ShapeCreator{
    public function create(string shapeName, int x, int y) {
        switch(shapeName)
            rectangle:
                return new Rectangle(x,y)
            circle:
                return new Circle(x,y)
    }    
}
main{
    sc = new ShapeCreator
    Shape circle = sc.create(1,2, "circle")
}

要查看其他概念,请参阅有时工厂类只是一个接口,它将由不同类型的具体工厂实现,但如果代码简单,您可以使用案例来实现对象的创建。

为什么要在ConcreteCreator.php中重新创建对象<代码>$this->createdProduct=new$productNow只需使用
$this->createdProduct=$productNow
这应该可以解决您的问题,很抱歉删除了注释。从一开始。工厂用于创建具有相同父对象的对象的更特定实例,因此假设您有一个figure父对象,它包含一些基本功能,如获取x和y、其x和y方法以及抽象绘制方法,然后使用字符串(比如“圆”)将对象传递给创建者它将用圆圈类绘图等填充所有的abstract函数。至少这是我获得工厂模式的方式。这与工厂模式完全不同。在您自己创建
矩形的示例中,通常您会传入类似“矩形”的描述,工厂将进行创建。@Cerkewny您能写一个示例代码吗?是的,基本上您想要的是myrectangle=factory\u create(x,y,Rectangle),工厂将读取字符串,然后创建矩形类型的对象,或者说这个类型还不受支持;如果(class_存在($class)返回新的$class;@sidux我认为这不是一个好主意,因为我们不确定“shapeName”是在您的例子中实现形状的actuall类。我还发现了一个更好的例子,说明factory在main中的
circle
是什么?以及
sc
是什么?我认为
sc
应该
cs
,但仍然不知道这里的圆是什么。您还没有创建任何circle实例来在main中使用它。@x4ph4r sc Is是为创建指定产品而创建的工厂,主圆圈是工厂创建的产品。很抱歉输入错误,现在所有内容都应该清楚了。@cerkiewny u仍然可以对所有形状使用抽象类,并使用instanceOf测试