Welcome Guest, Not a member yet? Register   Sign In
factory pattern?
#1

[eluser]pocketmax[/eluser]
Does ci have a factory pattern? For those of you who don't know a factory pattern is a design pattern where one object/class takes in the configuration of an object and instantiates objects and returns them based on the config that was given. So for example...

$mynewobj = ci::Factory(array('type'=>'contact','fname'=>'bob'));

The factory class takes in a config array and determines the obj it should make is a contact and simple returns it. I've used them in the past but what I mainly need is a factory that keeps track of the objects it makes and there ids so it is to not duplicate an object unnecessarily. So for example...

$mynewobj = ci::Factory(array('type'=>'contact','id'=>'123'));
//since the factory already made a contact object with the same id, it won't make another and will simply return the other object as a reference.
$myotherobj = ci::Factory(array('type'=>'contact','id'=>'123'));

This has helped SSSOOOO much in the past and I think it is vital in every framework to keep track of this stuff. Logically, it makes sense. If I had two objects pointing to the same data and I made a change to one objects attribute, the other object would never know.
#2

[eluser]sl3dg3hamm3r[/eluser]
As far as I know: No, CI doesn't implement such a thing by default.
Basically what you want is a Factory combined with a Registry, right? Why not writing a small library with a static Factory-method yourself? I guess it is not a big deal...
#3

[eluser]Cro_Crx[/eluser]
Have you considered using an Object Relational Mapper ? The features you've described are available with an ORM as well as many other benefits. They generally tend to minimize the amount of code you'll write as well.

I use DMZ ==> http://www.overzealous.com/dmz/pages/toc.html It's really great.

There is a bit of a learning curve as you'll need to write your models differently to use DMZ.

Those examples of the Factory class above can be done with DMZ. Here's the top example done in DMZ

Code:
//$mynewobj = ci::Factory(array(‘type’=>‘contact’,‘fname’=>‘bob’));
    $contact = new Contact();
    $contact->where('fname', 'bob');
    $contact->get();
#4

[eluser]pocketmax[/eluser]
ok, thanks




Theme © iAndrew 2016 - Forum software by © MyBB