[Design Patterns] Template Method Pattern

Template Method Pattern – when you define and implement one or more common methods in the base abstract class, but leave some abstract methods to be implemented in the child classes depending on the context. You may invoke the concrete methods from the abstract one. The abstract method contains functionality common to all types of…

[Design Patterns] The Strategy Pattern

Strategy Pattern – is when you define some interfaces which represent the strategies for various context dependent tasks. Then you implement these interfaces in classes with alternative purposes. Sometimes during runtime, the implementations can be switched so that a new strategy is adopted. In the actual client code (that is employing the strategies), you define…

[Design Patterns] The Builder Pattern

Builder Pattern – The constructor is private. You create a nested static class called Builder. In the builder you define methods corresponding to the constructor parameters of the nesting class. Each method returns a Builder instance (return this at the end). Drawback is that you duplicate fields in the Builder. Finally you create a method called…

[Java 8] Map.computeIfAbsent()

The computeIfAbsent method is similar to compute(), however it only has side effects on the map if the key wasn’t there before applying the method. One important thing is that the signature takes a Function as an argument and not a BiFunction as in the case of compute().

Here’s an example:


[Java 8] Map.compute example

Here’s a Java 8 Map.compute() example. Basically this construct replaces an idiom such as:

It does an in-place transformation by using a BiFunction, therefore you need to return if you have more than one statement. Another important thing about compute: If the BiFunction returns null and the key was in the map, it is…

[JAX-RS] Access-Control-Allow-Origin as a Filter

So if you’re getting the Access-Control-Allow-Origin error in chrome. Then you need to be sending those headers with the response from the server. One solution would be to put it in the Response in each method. A better (and more scalable) solution is to use a filter. Then don’t forget to disable it in production.

[Java Concurrency] An example of AtomicInteger

Here’s a simple example that shows how AtomicInteger can be used instead of the not thread-safe int Counter implementation.  


[JAVA 8] Custom DTO mapping

Very nice idea: http://www.idanfridman.com/mapping-your-entities-to-dtos-using-java-8-lambda-expressions/ Mapping your Entities to DTO’s Using Java 8 Lambda expressions We all facing the cluttered overhead code when we need to convert our DTO’S to Entities(Hibernate Entities, etc..) and backwards. In my example ill demonstrate how the code getting much shorter with Java 8. Let’s create the Target DTO: