Item 14: In public classes, use accessor methods, not public fields


So, do you see what’s wrong with this code? Well, not much actually. Item 14 in effective java is pretty simple to understand. In fact its just a by product of using common sense while writing code. Problem with the above given class is that – The class is just a bunch of properties. The class simply fails to take advantage of the encapsulation features. How can the problem be solved? Adding accessor methods in the given class, after changing ...

String Intern


  String#intern Probably you already know what it is, in case you don’t, please don’t stop unless you read and understand the whole thing.   String Interning String interning is a method where each distinct and immutable value of string is stored in a data structure. The data structure is called a string pool. The string interning storage method makes certain string operations time or space efficient. String is actually the original copy of the immutable string stored in the ...

YAGNI


Introduction YAGNI is abbreviation for “You Ain’t Gonna Need It”. It is a concept of extreme programming. And I am finding it very interesting as I never came across this term before. The concept is simple, that there is no need to implement something, unless you actually need it. i.e. do not try and forecast changes in the software and just code whats necessary. Caution The principle is not universally accepted. And it is supposed to be carried out along ...
Factory Design Pattern

Factory Design Pattern


People have been appreciating the effort I am putting in each post these days. Feels good. Moving on with another design pattern, we have the ‘Factory Design Pattern’. It is also known as the Simple Factory Design Pattern. There exists a very similar pattern named ‘Abstract Factory Design Pattern’. The difference between the design patterns will be discussed once I post about Abstract Factory Design Pattern in my next post. Lets use the Factory Design Pattern to solve a simple ...

Item 13: Minimize the accessibility of classes and members


Data hiding and encapsulation is one of the building blocks of a great design. Well minimizing the accessibility seems to be a pretty obvious thing. Well, although it is, its benefits are worth mentioning. Decoupled Modules – Since the exposed APIs of a module will be limited. The code becomes maintainable. Well you’ve got the hint from the point 1. It takes the understanding and the correct application of the Access Control Mechanism of Java. Rules of Thumb Make each ...