Skip to main content

Posts

Useful Java Best Practices Short and Sweet

1. Avoid creating unnecessary objects. Object creation is the more expensive operation in java.So, Create an objects when its required, public class Student {     private List students;     public List getStudents() {         if(students == null) {             students = new ArrayList();         }         return students;     } } 

Factory Pattern

Factory pattern comes from creational design pattern category.main objective of this pattern is to instantiate an object and Factory pattern interface is responsible for creating objects but the sub-classes desides which class to instantiate. Im going to start with a simple example.Suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. <Name> and if the sex is Female (F), it displays message saying Hello Ms <Name>. First Im going to create a Person class.. public class Person {     public String name;     public String gender;     public String getName() {         return name;     }     public String getGender() {     ...