简体   繁体   中英

How do I override a small portion of a large 3rd party API?

We're using a commercial distributed caching framework. We need to provide other shops within our enterprise the ability to use this framework, but we need to maintain control of the cache names. The framework will happily create/get cache instances with whatever name is requested, but we want to avoid two different application teams inadvertently using the same cache name and ending up with references to the same cache.

Ideally we'd have an admin web site where users could reserve cache names and then the cache creation process would check the database behind that site to make sure the cache name being requested is one that they have reserved.

Without wrapping the entire 3rd party API with our own wrapper, is there a way to do this?

We could subclass the CacheManager, override the creation methods, and instruct them to use our subclass instead of the original, but what would keep them from just using the original?

One way is to change the API source code to make the class and methods invisible and provide your own interface. If it is open source, it should not be a problem. If purchased, the vendor should be able to help you out.

2nd way could be to fail at runtime, if any API class is directly being tried to loaded, instead via overriden class. this could be done by creating a custom bootstrap class loader, which will become single entry point for all the class loading request.

Create a library of your own with a factory method that creates a properly named cache and returns it. From then on the user of the library is just accessing the commercial distributed caching framework as normal without going through any subclasses or such other mumbo jumbo.

Your Library: Accesses Server to Retrieve Cache Name -> Creates Cache -> Hands Cache Back to User

User then interacts directly with the third party library via the cache they were handed back.

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