I would like to write a data structure implementation in Java that uses caches as a core part of its functionality, and I would like the user to be able to provide their own cache implementations that implement a particular cache interface so they can test performance using various strategies (like LRU, LFU, MRU, etc.).

What is the best way to allow a user to swap in their own cache in an instance of one of the data structures without giving them access to my codebase? Is there a way I can pass in a class that implements the cache interface as a parameter?

This structure will contain perhaps several caches arranged in different ways, so I would need more than one instance of a cache, and I would like to be able to create and destroy caches at runtime. Would passing a constructor to the cache as a Lambda function be a good solution?

  • Possible duplicate of A software design pattern to model runtime-dependent behavior
    – gnat
    Commented Feb 15, 2018 at 6:09
  • 1
    I'm not sure you can pass a class as a parameter, but I'm pretty confident you can pass an instance of a class that implements your desired interface.
    – Mael
    Commented Feb 15, 2018 at 6:20
  • You could try an event, that returns a (cached) value. Or a virtual method. Commented Feb 15, 2018 at 6:21

2 Answers 2


Along the lines of your lambda idea, my suggestion would be to create a CacheFactory interface.

public class Whatever
  public Whatever(CacheFactory factory)
    cache = factory.build(/* configuration options */);

And you can do plenty of crazy things with that, if you like, such having your factory return singletons for certain caching types or strategies.

Alternatively, you could also use generics*

public class Whatever<C extends ICache> {
  private C cache;

  public Whatever() {
    cache = new CacheType();

(*) You can do things similar to this in C#

  • Yes. Java supports generics (afortunately).
    – Laiv
    Commented Feb 15, 2018 at 19:53
  • Thanks, I think I'll opt for generics. However, just to know, why would the factory and generic strategies be preferred over a lambda?
    – user129137
    Commented Feb 15, 2018 at 21:17
  • @user129137 Any number of reasons: better testability, extensibility, state, more explicit contract / more semantic value, etc...
    – svidgen
    Commented Feb 16, 2018 at 2:57
  • ... Lambdas, on the other hand, seem better to me for functions used only in one place, with an obvious purpose and/or when they more directly fulfill the requirements of the immediate context -- if that makes sense. For example, Rockets.GetBrokenOnes() might return All.Where(r => r.isBroken) or something -- the lambda is "elemental" in that context. It's not a separate, nameable responsibility... Silly example. Soft rule. Do whatever is sensible to you and your team.
    – svidgen
    Commented Feb 16, 2018 at 3:05

You could define a Cache interface which establishes a contract that you expect their implementation to abide by. Then allow them to then pass in their own implementation in the constructor (or setter method). i.e.

public class YourDataStructure {
    private final Cache cache;

    public YourDataStructure(Cache instanceOfTheirCacheImplementation) {
        cache = instanceOfTheirCacheImplementation;
  • I updated my question to explain that I might want several instances of the cache in my structure, so passing in a single instance wouldn't suffice
    – user129137
    Commented Feb 15, 2018 at 18:54
  • @user129137 it could. You could make an aggregate cache.
    – Zymus
    Commented Feb 15, 2018 at 21:05
  • @Zymus has a good solution. Effectively just include in the interface a method which adds a new cache. Alternatively, have the user provide a CacheFactory instead of a Cache, then you can make all the caches you desire. Commented Feb 16, 2018 at 3:00
  • @Maybe_Factor Hehe ... A CacheFactory you say. You mean like my answer? ... :)
    – svidgen
    Commented Feb 16, 2018 at 3:08
  • @svidgen Yes exactly! When I wrote my answer, the question didn't include details about needing to create caches willy nilly. Commented Feb 16, 2018 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.