Tag Archives: java

Prefer try-with-resources over try-finally

The try-finally construction in Java is often used to close the resources. Finally block will be always executed, so no matter what has happened in the ‘try’ block, the file or connection will always be closed. However, since Java7, there is an easier and more succinct way to do it – try-with-resources. Not only does […]

Read more

Bridge pattern

Bridge design pattern is the essence of Object Oriented programming best practices. It enforces encapsulation, separation of responsibilities and coding to interfaces. It decouples the classes while maintaining a clear interface between them. Let’s see how it works. Let’s imagine that we have two types of classes, and we would like them to cooperate, in […]

Read more

Adapter pattern

Adapter programming pattern comes in handy, when we would like to use a certain API, but have an object with a different interface. When so, we can use the adapter to wrap the strange interface with convenient API of ours. Plus, we can adapt the output to our needs. What are the scenarios where we […]

Read more

Proxy pattern

The idea behind the proxy design pattern is to have a class, which in some way facilitates or optimises access to an underlying object. At the same time, the proxy class has the same interface as the underlying object, so it can be substituted for it. Proxy pattern can be used for accessing remote resources, […]

Read more

Avoid finalizers and cleaners

There is a mechanism in Java which allows running a certain code on object destruction. Something in a way like a C++ destructor method. However, due to java memory design and implementation of garbage collection in different JVMs, you should never rely on it. You are not guaranteed when the “destructor” method will be called, […]

Read more

Eliminate obsolete objects references

Java is a garbage-collected language. That means, that objects with no reference to them are cleared automatically. But it doesn’t make it a memory-leak free! Nulling the object to garbage-collect it should be an exception, not a norm. However, you still need to think about how the garbage collector works and how to design your […]

Read more

Avoid creating unnecessary objects

Just don’t do it. Don’t create new strings Don’t instantiate resource heavy classes in a loop Mind the unnecessary auto-boxing Make use of static fields or caching Make use of singleton pattern Opt for immutable objects and reuse them. This post is based on Joshua Bloch Effective Java Third Edition (get it on Amazon: https://www.amazon.com/Effective-Java-3rd-Joshua-Bloch/dp/0134685997)

Read more

Prefer dependency injection to hard wiring resources

When creating new classes it is preferable to design them easy to test (easy to mock) and as independent from other elements as possible (decoupling). Even though it might seem at the beginning that the classes used inside will never change, it is still advice to code to interface, instead of the implementation; and to […]

Read more