Today I am going to talk about template method pattern and strategy pattern. In addition, we will discuss how to leverage them with functional interface in Java 8. These two design patterns are both belong to behavioral patterns in Design Patterns written by GOF.
I am working on some simple AWS lambda functions now. To get the better performance, I decide to get rid of Spring framework in most of them as they are simple and straightforward. But without having a mature framework, it should be more carefully as I need to implement several methodologies by myself instead of using things provided by the framework. So, I decide to refresh my knowledge of the basics, like common design patterns in object oriented programming. I am going to talk about a few design patterns by summarizing the resources I found, putting my own thoughts and including some Java code examples in the next few posts.
Today, I am going to start our journey with Factory Pattern.
As we know, the Hashmap is implemented by a bucket of linked lists or red-black trees. However, when we look into the source code of Hashmap in Java 8, there are interesting comments on attributes related to the size of bucket, e.g. DEFAULT_INITIAL_CAPACITY and MAXIMUM_CAPACITY. It is said these values MUST be a power of two.
So, what’s the reason that the bucket size of a Hashmap has to be a number, which is power of two?