Search code examples
javaoopapi-design

API Design - Use object or collection as parameter?


Which one of the following designs is considered better for an API and why?

apiMethod(Map<A, B> aToB)

or

apiMethod(MapWrapper<A, B> mapWrapper) where MapWrapper is simply a class containing a reference to the Map.

What are the pros and cons of both the approaches?


Solution

  • You always strive for "minimalistic" APIs.

    In this case: when your API works when passing a Map - then of course you choose that path. Why put the burden on your client to first wrap that map?!

    The pro of option 1 is - this is the straight forward path.

    Whereas option 2 only has the downside of making it harder to use the API.

    In other words: if the central "property" of that parameter is "to be a map" - then it should be passed as map. But when the "central theme" is something different - then you pass something that goes along that other theme.