Question

Well, I'm creating a library and that library needs to take all other libraries and make them work "alike".

For example: Imagine that I have 5 libraries, and all that libraries has the same idea, work to the same case, but they have their own way to work, their own API, and what I need is to make them work using a single API.

What is in my mind is to create a "factory" with a "trust list" inside of the factory that allows the user to choose different libraries to create, and the "factory" look on the "trust list" and if the library really exists, it creates and return the library.

But it can also be made using interfaces, where I can accept only classes that implements an specified interface, where I will have the security of the implementation of the methods that I want, so what this mean? All the libraries needs to implement that interface, implement the methods and make a kindle of wrapper to the library and that way they will work with the same API. The user can create a library using the factory and use the same API to all of them.

I don't know if you understand what I'm trying to explain, but I want to know, based on what I said, what is the best on my situation, "bridge" or "adapter" pattern?

And also, is my idea correct or am I crazy? (The interface and factory thing, and also the bridge and adapter, tell me what you have in mind).

Thank you all in advance.

Was it helpful?

Solution

The Bridge pattern is designed to separate a class's interface from its implementation so you can vary or replace the implementation without changing the client code.

I think you can specify public non-virtual interface, then using Template Method in each of these public functions invoke implementation method.

class Basic {
public:
      // Stable, nonvirtual interface.
      void A { doA();}
      void B { doB();}
      //...
private:
      // Customization is an implementation detail that may
      // or may not directly correspond to the interface.
      // Each of these functions might optionally be
      // pure virtual
      virtual void doA { impl_ -> doA();}
      virtual void doB { impl_ -> doB();}
};

These lectures might be useful:

Bridge pattern

Template method

OTHER TIPS

Sounds like adapter to me. You have multiple adapter implementations, which is basic polymorphism and each adapter knows how to adapt to the specific library.

I don't see how the bridge pattern would make any sense here. You would typically use that in places where you use these libraries, but you don't know the specific library implementation yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top