Blogs : Latest entries
|
|
|
We've all been there, you start with a class and a simple constructor that makes total sense. As time goes by, you keep adding features and the list of constructor arguments grows … and grows … until … it becomes unusable. This is exactly what happened with the The version 1.0 constructor looked like this:
public Cache(String name, int maxElementsInMemory,
boolean overflowToDisk, boolean eternal,
long timeToLiveSeconds, long timeToIdleSeconds)However, with version 1.7 this turned into:
public Cache(String name, int maxElementsInMemory,
MemoryStoreEvictionPolicy memoryStoreEvictionPolicy,
boolean overflowToDisk, String diskStorePath,
boolean eternal, long timeToLiveSeconds,
long timeToIdleSeconds, boolean diskPersistent,
long diskExpiryThreadIntervalSeconds,
RegisteredEventListeners registeredEventListeners,
BootstrapCacheLoader bootstrapCacheLoader,
int maxElementsOnDisk, int diskSpoolBufferSizeMB,
boolean clearOnFlush,
boolean isTerracottaClustered,
String terracottaValueMode,
boolean terracottaCoherentReads)There are a lot of downsides to relying on constructor parameters like this:
new Cache("myCache", 10000, MemoryStoreEvictionPolicy.LRU, false,
null, true, 60, 30, false, 0, null, null, 0, 0, false,
true, "identity", true)However there some advantages:
So, we decided to change the The version 2.0 constructor looks like this:
new Cache(CacheConfiguration config)We then created a constructor in
public CacheConfiguration(String name, int maxElementsInMemory)All the other parameters are implemented as fluent interface methods as well as regular setters and getters so that instances of the class can be used as beans:
public final CacheConfiguration clearOnFlush(boolean clearOnFlush) {
setClearOnFlush(clearOnFlush);
return this;
}This allows the example above to be rewritten like this:
new Cache(new CacheConfiguration("myCache", 10000)
.eternal(true)
.timeToLiveSeconds(60)
.timeToIdleSeconds(30)
.terracotta(new TerracottaConfiguration()
.clustered(true)
.valueMode(ValueMode.IDENTITY)));The end results are that:
|


