Api / plugins for open source libraries?
whenever i use an open source library eg. Doctrine I always end up coding a class (called Facade) to use the Doctrine library.
so the next time I want to create a user, just type:
$fields = array('name' => 'peter', 'email' => 'peter@gmail.com');
Doctrine_Facade::create_entity($entity, $fields);
then it creates an object with the provided information.
so I think all coders will create their own "Facade".
Wondering how open source Facades usually load and interact with open source libraries? this is a rare reason, I have not seen any of them. in some frameworks I've seen them called plugins, eg. plugins for twitter api or facebook api.
so whenever you load a library you have to search for plugins / facades on the net, or is it better to just try to code your own? I just thought it would be great if everyone didn't reinvent the wheel.
thanks.
a source to share
Let's say it's not just about factories, let's say you really often write Facades for the libraries you use. What's the point? Why are you doing this? The point is that you are using the library in a certain way. If the facade you are writing is versatile and everyone tends to write something similar, the Facade will definitely be part of the library. So the reason it isn't, and why you want to write it, is because you are using it in a very specific way, which is specific to your library application. This way you go from library abstraction to your application abstraction. This can remove much of the complexity of the library from your application, but it also limits the way you use the library. So, if you understand my point, you can make surethat it doesn't make sense to release every Facade for a specific way to use the library. However, sometimes when we talk about a large influential library that is combined with some other libraries and together contains abstraction that can be widely used, it may happen that a new library is created.
a source to share
The purpose of the facade is ( citation )
- Provide a unified interface for a set of interfaces in a subsystem. The facade defines a higher-level interface that makes it easier to use the subsystems.
- Wrap a complex subsystem with a simpler interface.
While the above can be said to apply to your example, it is much more like AbstractFactory . You might want to rename it EntityFactory without the Doctrine part, because the fact that it uses Doctrine internally is an implementation detail. It doesn't matter for the public Factory API. You might want to switch from Doctrine to Propel later and then you just need to change the code inside the class, but not the API.
You may also be interested in the Gateway Template .
But to answer your question, is this a common approach: yes, I think so. Abstraction makes the code easier to understand and easier to work with. But since the facade / gateway API - whatever is used - is usually determined by what the application is doing, it is rarely reusable, so I doubt you will find ready-made facades / gateways on the internet.
a source to share