I am new to the design patterns. I have used it on the frameworks I’ve used but now I need to choose the right one for what I want to do. My problem is the next:
I have two databases where I need to extract statistics. I’m will be calling it lorem and ipsum. In some cases I need just data from one, from the other or the two combined. Actually I’ve functions like:
- getAll
- getAllFromLorem
- getAllFromLipsum
I think that I can use a factory pattern for do something like this:
<?php
$lorem = Stats::factory('lorem');
$lorem->getAll();
$lipsum = Stats::factory('lipsum');
$lipsum->getAll();
My idea of the structure of this will be have something like:
/stats/lorem.php
/stats/lipsum.php
/stats.php
And when I factory one or other driver it will use the getAll of one of the other file.
I think that this is right. But I also have a problem. I’ll like to have functions that will internally combine it, something like:
<?php
$all = Stats::factory();
$all->getAll(); // this is lorem and ipsum combined (by me, not auto of course...)
But this last thing I don’t know how can I achieve it. Where this getAll function will go?
Can anyone suggest me a good way of doing this?
Thank you in advance!
The idea would be for your
Stats::factory()method to return an object of a specific interface. In this case the interface would be something like:You would have one implementation the “single database” version and another for the combined source version that combined the results. Here’s an naive implementation that simply merges the results together from both sources.