Design Pattern, Java, OO Design Principles

Avoid NullPointerException in Java

I would like to share with you some tips to deal/prevent NPE in your code: The BIG IMPORTANT rule: do NOT assign / pass / return null value and keep your code as cohesive as possible. Programming tips / techniques: 1. Return an empty collections instead of null. For example: List: Set: Map: 2. Return… Continue reading Avoid NullPointerException in Java

Design Pattern, Java, OO Design Principles

Why extends is evil

Improve your code by replacing concrete base classes with interfaces The extends keyword is evil; maybe not at the Charles Manson level, but bad enough that it should be shunned whenever possible. The Gang of Four Design Patterns book discusses at length replacing implementation inheritance (extends) with interface inheritance (implements). Good designers write most of their code in terms… Continue reading Why extends is evil

Java

Authorize user permission with bit field technique in Java

Assume that we have 4 functions: View, Add, Edit, Delete And 4 user roles: 1, 2, 3, 4 with: 1: User can View 2: User can View and Add 3: User can View, Add and Edit 4: User has full permission Here is small snippet to detect permission of user. And here is result: With… Continue reading Authorize user permission with bit field technique in Java

Chưa phân loại

What is the difference between a monitor and semaphore?

The reason that semaphores and monitors are needed is because multi-threaded applications (like Microsoft Word, Excel, etc) must control how threads access shared resources. This is known as thread synchronization – which is absolutely necessary in a multi-threaded application to ensure that threads work well with each other. If applications do not control the threads then it… Continue reading What is the difference between a monitor and semaphore?

Java

10 Most Useful Java Best Practice Quotes for Java Developers

Quote 1: Avoid creating unnecessary objects and always prefer to do Lazy Initialization Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code. Quote 2: Never make an… Continue reading 10 Most Useful Java Best Practice Quotes for Java Developers

Java

Encapsulation in java and its relation with abstraction

Most of you will agree that encapsulation and abstraction together brings a lot of confusion. Most blogs add only confusion further. Lets solve this puzzle. I started working on this post after my previous post “Understanding abstraction in Java“. My goal was to understand encapsulation in java and how it relates to abstraction. As soon… Continue reading Encapsulation in java and its relation with abstraction