简体   繁体   中英

What's the best design pattern for this?

I have a requirement to add a layer between an external library and client code, such that the client has a consistent interface to the underlying library and we are able to switch out the library with the minimum of code changes.

For example:

public interface IConsistentInterface
{
    void Foo(string bar);
    void Bar(string foo);
}

Internally the concrete implementation of this interface will be calling into the library, using whatever functionality that offers. This way if we switch out the library, we just need to change the internal calls of concrete types of this interface.

This looks to me like the Adapter pattern, but is this the best approach to the problem?

Thanks.

It seems like the Gateway pattern might fit nicely here. However, as you say, the Adapter pattern can also work well for you. In fact, thinking about it there seems little difference between how the two might look in code.

You can't go wrong normalising the external API into another API your code can consume consistently. If changes occur in the external API, the breaking code will be limited to your normalised API, where you can either do a fix or damage limitation. The net effect, and the one both patterns aim for, is a much reduced surface area of impact on the rest of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM