Full abstract factory pattern to use for Code Igniter models

1.7k Views Asked by At

I’m creating a website where I’m using MongoDB as database, buy I also want to be able to use any other storage system in the future, so I would like to use a full implementation of the abstract factory pattern. With a full implementation I refer to this:

  • “Database_factory” abstract class, which should be use by the client code (controller) to create a factory object.
  • “Mongodb_factory” class, which extends Database_factory class.

  • “Customers” abstract class that represents a set of queries.

  • “Mongodb_customers” class that implements “Customers” class. “Mongodb_customers” object should be returned by the “Mongodb_factory”.

I know exactly how to do this with pure OO PHP. But I don’t know how to merge it with the models arquitecture that CodeIgniter provides.

It would be easy to just have the “Customers” abstract class and then the “Mongodb_customers” to implement it, but I want to make use of the factories too.

When you need a model in CodeIgniter, you just use “$this->load->model(‘my_model’);” and use its functions, but how to create a factory object, and make it return the concrete model you need, which you use through an interface (abstract class)?

Is there any chance to achieve this?

Thanks to all of you people.

1

There are 1 best solutions below

0
On

Hm- so you want to be able to switch between different db implementations. Wouldn't the adapter pattern be better suited for this? You would just have to create a new model that wraps the new db and call that one. I thought abstract factory was just for creating a set of related/dependent objects without exposing their concrete classes. If that's what you want to do, then I think you can create a model that acts as a factory, and then other models that extend an abstract class model (I've never tried this but I figure you can try to load it within a class to extend it). That way when you load the factory, you can call one of the methods and its method will load the right implementation for you(with the loading happening in the method of your factory).