May 30 Morning Devotional

Poetic interpretation of Song of Solomon 2:15 - Catch for us the foxes, the little foxes that ruin the vineyards.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




5 popular decorators from Java

5 popular decorators from Java you probably didn’t even know they were decorators

Java has some nice decorators that can make your life easier, or tougher, depending on the case. Below, you will see a list of five decorators present in Java that you didn’t even realize they were decorations.

Java 8 provides many synchronizedX() methods, for lists, sets, maps, etc. They are analog.

The synchronizedList() will turn the access to the list mutually exclusive. That is, two threads will not have simultaneous access to any method of the object as a lock is held by the first thread. If thread1 enters the get() method, thread2 will be blocked to enter the method size() until thread1 releases the lock.

For the concurrency concern, though, there is a better solution. For Maps, for example, ConcurrentHashMap is more performant. As described for lists, the analog method synchronizedMap() will turn all methods mutually exclusive, even the methods that only read data. In other hands, ConcurrentHashMap avoids blocking the object for reads and provides a more fine-grained lock for writes. This makes ConcurrentHashMap more performant then synchronizedMap() and should be preferred.

As well as for synchronizedList() method, Java 8 provides analog unmodifiableX() methods for lists, sets, maps, etc.

The decoration here is that the list cannot be changed. Therefore, the methods that would change the list throw an UnsupportedOperationException.

There are some discussions if that would be the best design to provide immutable collections, but this post does not cover this discussion.

Stepping back a little, a better code, not runtime-dependent, could be achieved with the use of Generics. That’s right!

But this decorator can be a second option if eventually there are legacy code involved, in which changing the construction of the object is not an option.

Yeah, every Java developer had once to use input or output streams to read or write data from/to streams, files, etc. This is one of the many decorators in the io package. As per the javadocs, BufferedReader:

The BufferedReader provides the Reader functionality and adds on the top of that the methods to retrieve complete lines instead of single characters. Thus, it simplifies a lot the work to read text files.

Java 8 introduced the concept of Streams in the language, and also the method lines() in the BufferedReader. This is easy to use and fully compatible with streams.

The GZIPInputStream is one of the many decorators of InputStream, that adds the functionality of reading compressed data in GZIP file format.

Add a comment

Related posts:

4 Simple Tips to Improve Your Thinking

That also pay off in your relationships

How I Used My Diabetes to Become a Better Person

The doctor had the test report in front of him showing my sugar readings. They were way off the charts. An uncomfortable silence hung over the entire room. The doctor pronounced his verdict. He told…

Best water leak detectors for smart homes

Fire might be a homeowner’s greatest fear, but any insurance company will tell you that water is the far more common cause of property damage, even if you don’t live in an area subject to flooding…