views:

97

answers:

4
+1  Q: 

PHP Design Pattern

Hi, I have a class that performs database operations and returns results (array, true, false). And I have an another class that creates JSON string by using this class in its constructor. Can we say this class is an Adapter? Or simply wrapper or ...

Class Db
{
    public function getRows($params)
    {
        //...
    }
}


Class DbAdapter
{
    private $_dbh;
    public function __construct($dbh)
    {
        $this->_dbh = $dbh;
    }

    public function getJson()
    {
        return '{"key": "foo", "key2": ' . json_encode($this->_dbh->getRows($params)) . '}';
    }
}

Thanks

A: 

In Ruby on Rails they have the MCV model. The class performing the database operations is called a Model. The code handling the output is a View. And to convert/add information from the Model to a View a Controller is needed.

I do not know whether PHP has such names or not, but the Rails ones seems reasonable. Your Adapter class would be called a Controller in Rails.

Veger
No it wouldne be a controller. It would either be a view helper utilized by passing in a model instance and then converting it to json, or it would be a method on a model directly. However Rails and some other frameworks can be misleading because essentially they consider the Model layer the ORM - when in the typical definition any Model may or may not be tied to a DB or persistence layer.
prodigitalson
A Controller would get the information using the Model, then convert it to something usable (by rendering a View) and return it. Quite similar what DbAdpter class is doing. A Helper and Model are/should not issuing commands to get data from the Model, that the job of the Controller. But I guess this is off-topic for this question...
Veger
prodigitalson
A: 

From: http://www.fluffycat.com/PHP-Design-Patterns/Adapter/

Adapters are helpful if you want to use a class that doesn't have quite the exact methods you need, and you can't change the orignal class. The adapter can take the methods you can access in the original class, and adapt them into the methods you need.

I would say your code qualifies as an adapter if you plan to add methods to supplement another class.

Kevin Goff
This is the question I don't plan using DbAdapter in another class. Can we still say it is an adapter.
jsonx
+1  A: 

Id say its more of Decorator... http://en.wikipedia.org/wiki/Decorator_pattern

prodigitalson
Actually first I thought it as a decorator. But as far as I understand decorator should add new methods/skills to existing object. Thus I couldn't decide.
jsonx
Well the thin is IMO this shouldnt be applied to your DB class directly regardless of if its an adapter or decorator unless its just a return format (aka DB::ASSOC, DB::JSON, DB::OBJ, etc.). Otherwise the proper place for it is really directly on the class that Models the data in the array you return from `getRow` or on a decorator that wraps such a model object.
prodigitalson
A: 

I don't think you can pick one pattern and say that's the one being used here. Here's my little list of patterns I see in your example. Feel free to comment.

Delegation because the DBAdapter class delegates the task of getting the actual rows to the DB class.

Decorator because the DBAdapter class decorates the DB class with additional functionality - that of spitting the output in JSON.

Adapter/Wrapper if you think that it allows other client to access your database rows that only understood JSON or XML or some other format.

But if we had to pick one, I'd say Adapter. It takes the data in the form of PHP data structures and converts it into a JSON representation.

Anurag