I took clues from DataMapper
pattern to create something like this:
class BusinessObjectCreator
{
public function create()
{
//Acquire Data
$number = filter_input(INPUT_POST, "number", FILTER_SANITIZE_INT);
//create and populate object
$object = new Object();
$object->setNumber($number);
return $object;
}
}
//usage:
$objectInstance = (new BusinessObjectCreator())->create();
//examples of usage once created
$objectInstance->someBusinessFunction();
echo $objectInstance->getNumber();
But to me it also looks like a Factory
pattern or a Builder
pattern.
Which is it? and did I code it up correctly?
Purpose is to create an object instance populated with data. Then I can use the created object to do operations on the data.
This looks like a Factory Method. The naming convention leads you to think it's the builder pattern which has a separate object for the building of the concrete object. You have a separate object but are not setting the data like a builder would.
I'm not too familiar with PHP but to make it a builder pattern you'd create methods for the data, then call create()/build() at the end of the method chain.
It's creation would look like this
$objInstance = (new BusinessObject())->number($number)->create();
Since you only have a class with a method that creates an object, this pattern falls into the factory method category. Usually you use a builder when there are a lot of potential different configurations of an object upon creation, so you may just stick the factory method, if you only are setting one piece of data.