Design – Wrapping a 3rd party API – Gateway Pattern connections

designdesign-patternsobject-oriented-design

Given a 3rd party API with a very cumbersome object with many different responsibilities– we decide to wrap using small, manageable, single-responsibility objects. This is the gateway pattern (I think).

So any time you do this there is going to be some kind of "connection" where you have to connect to the resource. Furthermore, calling applications may want to keep a single connection open and use it with multiple objects (those small, manageable, single-responsibility ones we created).

What form should this connection take? Should this connection be another object? Should it be passed it to the constructor of the small wrapper objects? Or, should the wrapper objects inherit from a connection object? Or aggregate it?

What about the special case where we can have only have only one connection per application?

Best Answer

Make a connection class with a public constructor. It should initialize an instance of the 3rd party object as a private field. The connection class should have factory methods that call private constructors of your other wrapper classes, passing its instance of the 3rd party object as a parameter.

Related Topic