A bit on confusion on my end working through the Bridge pattern. Hopefully someone can point me to where I'm going wrong.
The initial problem comes from wanting to formalize data access without implementing the API in every adapter. So
class SalesRepository{
// constructor and other stuff
getCart(key){
return this.db.cart.get(key);
};
}
means we have to give every adapter a cart
with get
functionality, which could be difficult to maintain. So far so good.
The CartRetrieval
bridge comes to the rescue, allowing us to house the get
logic in one place and add the bridge to our adapters, like so:
const CartRetrieval = (db) => {
return {
get: (id) => console.log(`Getting from ${db.name}`),
}
}
class PgAdapter{
constructor(){
this.name = "Postgres Adapter";
this.cart = CartRetrieval(this);
};
// Other stuff
}
So here is my confusion: The body of our bridge's get
method is just a dummy console.log
that is meant to stand in the place of the logic needed to get a particular cart
.
But each database may require a different logic to perform that get, right? How does the get
method in the CartRetrieval
know which to use? Would we switch on the db
type passed in, supplying a different strategy depending on the adapter? Should the bridge delegate back to the adapter, in which case we're back to each adapter needing to implement the API?
Thanks in advance for any help!