mardi 8 décembre 2020

Java AbstractQueuedSynchronizer and the template method pattern

While reading the source code of ReentrantLock, I found that internally it use a synchronizer which extends AbstractQueuedSynchronizer to control the lock. Doug Lea mentioned in this paper that AbstractQueuedSynchronizer serves as a "template method pattern", which helps simplify the coding of sub-classes.

However, Joshua Bloch advised in Effective Java that we should "favor composition over inheritance", because "unlike method invocation, inheritance violates encapsulation". And in my understanding, the "templates" in Spring (e.g. RedisTemplate, TransactionTemplate, etc.) follow this rule.

So, back to the AbstractQueuedSynchronizer and the synchronizer defined in ReentrantLock, I would like to know if its design (based on template method pattern) has any disadvantages. Many thanks!

Aucun commentaire:

Enregistrer un commentaire