0

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.

Dennis
  • 7,907
  • 11
  • 65
  • 115
  • Just a casual comment/question: Why don't you use dependency injection? In other words, why don't you pass an Object to the create method? – Guillermo Mansilla Aug 11 '15 at 20:25
  • Do you mean why don't I create the object elsewhere and then pass it into `create()` function, (then populate the object, and return it)? – Dennis Aug 11 '15 at 20:28
  • ^ if I understood it correctly ... I can. It will just make the pattern different. I can create object elsewhere and change the name to `BusinessObjectPopulator` to reflect the purpose better. – Dennis Aug 11 '15 at 20:41
  • Yes, I understand that this (pass the object as a parameter or create the object inside the function) is not what you're asking. I just wanted to point out something that can be improved. As for the question per se, this patters seems like a factory to me and it can still be improved by having your class to implement an interface. – Guillermo Mansilla Aug 11 '15 at 21:01
  • what kind of an interface do you mean? I am not sure I understood. Also, I can further improve on this by injecting input source rather than having input source hardcoded into the class like I have right now. – Dennis Aug 11 '15 at 21:14
  • https://en.wikipedia.org/wiki/Factory_method_pattern – Guillermo Mansilla Aug 11 '15 at 23:54
  • http://stackoverflow.com/questions/757743/what-is-the-difference-between-builder-design-pattern-and-factory-design-pattern – Henrique Barcelos Aug 12 '15 at 20:49

1 Answers1

0

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.

Builder Pattern

Dennis
  • 7,907
  • 11
  • 65
  • 115
Pumphouse
  • 2,013
  • 17
  • 26
  • well in my application I am setting up several pieces -- model name, various input parameters, and other data. It is done at the same time (i.e. all data is known at the same point in time) – Dennis Aug 11 '15 at 21:49