3
\$\begingroup\$

Products Class that are like

Table, Chair, etc.

Please review it and give your thoughts and let me know if there are any rooms for improvement.

Products.php

<?php

abstract class Products
{

}

class Table extends products
{
    public function __construct()
    {
        echo "New Table Created";
    }
}

class Chair extends products
{
    public function __construct()
    {
        echo "New Chair Created";
    }
}

Factory Class That can produce Plastic Furniture, Wooden Furniture

Abstract Factory Class - FurnitureClass

<?php
include "products.php";

abstract class FurnitureFactory
{
    abstract function building();
}

class WoodenFactory extends FurnitureFactory
{
    const TABLE = 1;
    const CHAIRS = 2;

    public function building()
    {
        echo "Building Wooden Furniture";
    }

    public function makeWoodenTable()
    {
        echo "Wooden ";
        return new Table();
    }

    public function makeWoodenChair()
    {
        echo "Wooden ";
        return new Chair();
    }
}

class PlasticFactory extends FurnitureFactory
{
    public function building()
    {
        echo "Building Plastic Furniture";
    }
}

$wfactory = new WoodenFactory();
$wtable = $wfactory->makeWoodenTable();
$wchair - $wfactory->makeWoodenChair();
\$\endgroup\$
2
  • \$\begingroup\$ seems to be redundant in case if some factory should build a furniture set which is comprised of 1 table and 2 chairs \$\endgroup\$ Mar 12, 2016 at 20:56
  • \$\begingroup\$ imho, having a WoodenFactory where you have to call makeWooden... methods, is not entirely how you would like your factory. A makeTable and makeChair will make more sence. Then every furnitureFactory can have a makeTable and makeChair, changing the factory from Wooden to Plastic just produces different objects. (instead of also changing the method calls) \$\endgroup\$ Mar 15, 2016 at 7:59

1 Answer 1

1
\$\begingroup\$

The class Products seems better named as Product (singular) since a table is a type of product, as is a chair.


The methods in WoodenFactory could simply be static methods since they don't appear to depend on any state, and thus the word wooden could be removed from them - e.g. WoodenFactory::makeTable() and WoodenFactory::makeChair().


The constants TABLE and CHAIRS don't appear to be used for anything...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.