Jump to content

Recommended Posts

There are many times in programing where there is a need to have an object that creates other object

or maybe there needs to be a set of instruction or data manipulation that needs to occur before an object

gets created. This is called an object factory. Factory is a design pattern that controls the creation of

other objects from one entry point. (not the formal definition, but mainly what it is used for.)

 

Example:

 

function do_some_work() {
    // Possibly manipulate some data, or connect to a database and retrieve information...
    ....
}

class ObjectFactory {
     public function create($classname="") {
          if(!empty($classname)) {
               // Do some work
               $work = do_some_work();
               return new $classname($work);
          }
     }
.......
}

class Test {
    public function __construct($args="") {
        ....
    }
......
}

$factory = new ObjectFactory();
$test = $factory->create("Test");

 

The issue with this object factory is that it is not generalized.

It also will pass $work to ever object that it creates which might not be what is needed.

By the nature of this object you can't create both static (Singleton), and instantiatable (using the reserved word new) objects.

 

There are a few ways this can be accomplished.

 

Example:

 

function do_some_work() {
    // Possibly manipulate some data, or connect to a database and retrieve information...
    ....
}

class ObjectFactory {
     public static function create($classname="") {
          // Do some work
          $work = do_some_work();
          if(!empty($classname)) {
                switch($classname) {
                      case "Singleton": return Singleton::getInstance($work); break;
                      case "Instantiable": return new Instantiable($work); break;
                      .......
                }
          }
     }
.............
}

class Singleton {
    public static function getInstance($args="") {
        ....
    }
....
}

class Instantiable {
    public function __construct($args="") {
        ....
    }
}

$factory = new ObjectFactory();
$test = $factory::create("Instantiable");

 

The issue with this class is first the ObjectFactory is an instantiable class itself,

therefore taking up more memory and coding. Second, every time a new object needs to be created

it must be placed into the switch() statement. Third, again it will pass $work to all of the

objects it creates (or returns).

 

On the other hand her is an example of a general Factory object. Example:

 

abstract class ObjectFactory {
     public function create($classname="") {
         if(!empty($classname)) {
             $reflection = new ReflectionClass($classname);
             if($reflection->isInstantiable()) {
                 return new $class_name($args);
             } else {
                 return call_user_func(array($classname,"getInstance"));
             }
         }
     }
.......
}

class Test {
    public function __construct() {
        return;
    }
   
    public function sayHi($string="") {
        echo("Hi: ".$string);
        return;
    }
}

class TestSingleton {
    public static function getInstance() {
        .....
    }
   
    public function sayHi($string="") {
        echo("Hi: ".$string);
        return;
    }
}

// Will print Hi: Have a nice day.
// on the screen
ObjectFactory::create("Test")->sayHi("Have a nice day."); // Uses the instantiable Object

ObjectFactory::create("TestSingleton")->sayHi("again from a singleton."); // Uses the Singleton Object.

 

This example uses PHP5 reflection class.

A very handy class to reverse engineering object.

It is also an abstract class which means it can not be instantiated.

It allows for object chaining and leaves it to the returning class to "do work".

by chaining ->sayHi("again from a singleton.");

Lastly, it solves both cases of returning a new instance of an object that is instantiatable and the instance of a singleton object, and gives a standardized way of creating other objects.

 

This is a brief overview of an abstract factory object.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.