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();
1 Answer 1
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...
Explore related questions
See similar questions with these tags.
WoodenFactory
where you have to callmakeWooden...
methods, is not entirely how you would like your factory. AmakeTable
andmakeChair
will make more sence. Then everyfurnitureFactory
can have amakeTable
andmakeChair
, changing the factory from Wooden to Plastic just produces different objects. (instead of also changing the method calls) \$\endgroup\$