Posts

Rest API Design Reference

REST stands for Representational State Transfer. It is an architectural style for developing software. Architectural Constraints REST defines 6 architectural constraints which make any web service – a true RESTful API. Uniform interface Client–server Stateless Cacheable Layered system Code on demand Uniform interface Uniform interface suggests that the resource in a system to have only one logical URI and  Then provide additional information to retrieve the related data. For instance we can provide Links for related resources like child objects, parent objects etc. User of one API end point should be able to identify and work similarly with other Resources. Also, the resource representations across system should follow certain guidelines such as naming conventions, link formats or data format, filter formats pagination formats etc. All resources should be accessible in similar way and should use similar status codes. Client–server Server should not depend on p...

Java Constructor vs method

Functionally difference between constructors and methods is that constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist. Constructors can't be called directly. They are invoked implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new. Constructors must be named with the same name as the class name. Constructors cannot  return anything, even void . Methods must be declared to return something ( or void). Constructor compiler provide default constructor where as method compiler does't provide. Constructor cannot be final since it cant be inherited. Constructor cannot be abstract since it is used to create and initialize object. Constructor cannot be static since it is used to create instance of object. Only public, protected & private modifiers are permitted.

immutable objects in java

In software programming, an immutable object is an object whose state cannot be modified after it is created. How to make Immutable Object in Java Make the class final. Make all members final, set them explicitly, in a static block, or in the constructor. Make all members private. Do not expose any setter methods. If the instance fields include references to mutable objects, then don't share references to the mutable objects.  For getter methods which needs to return mutable objects, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods. Do not store references to external mutable objects passed to the constructor. Create copies, and store references to the copies.  Advantage of Immutable Objects Immutable Objects are ThreadSafe. Value Objects are generally considered Immutable. In java String and all the Wrapper Classes like Integer, Boolean are immutable. It enhances security and less bug when the o...

Better ways to implement Singleton in Java

Singleton Design pattern restrict creation of only once instance of a Class. That means inside a single JVM only one instance of the Singleton Class will be created.  In java we have broadly two options to implement Singleton. 1) Early Creation of the Object Instance. 2) Lazy Creation of the Object Instance: Here the instance is created on first invocation. Following example demonstrate how to implement Singleton in Java. Method 1: Early Creation of Object Instance public class SingletonClass { private static final SingletonClass INSTANCE = new SingletonClass(); //Private Constructor private SingletonClass () { //Do nothing } public static SingletonClass getInstance () { return INSTANCE ; } } Here the instance is created ahead even before calling the Class. We need to provide a private constructor otherwise Java Compiler will add a default Constructor. Method 2: Lazy Creation of Object Inst...

Interpreter Design Pattern

Type: Behavioral Design Pattern Summary : Interpreter design pattern is a behavioral design pattern. It is used to interpret or evaluate an expression(like sentence, query, mathematical expression etc).  In this pattern Expression is considered to be composition or Terminal Expression and Compound Expressions.  Example:  SQL Squery,  Search engine query processing are example for Interpreter design pattern.

Prototype Design Pattern

Image
Type : Creational Design Pattern Summary Prototype design pattern is useful when creating an Object is expensive. Prototype is based on the idea of having a blueprint object for creating other object instances by cloning it. In java Prototype Design pattern is implemented by using cloning an existing object. Details In Prototype design pattern, we create a new Object instance by copying (clone) existing object instance and changing required attributes. It is a good use case when Creating an object is complex and expensive. In those cases rather than creating a brand new instance though constructor it is much more efficient if we can clone an existing object. Participants In Prototype Design Pattern Client : It calls the prototype to get object instance. Prototype : It Provides the clone interface. Clones: The clones object that are being created. Registry or Cache: It holds the master created objects. Example Let's take example of building a Car object...

Builder Design Pattern

Image
Type : Creational Design Pattern Summary Builder pattern is a Creational Design Pattern and solves the problem of creating complex objects or objects whose constructor needs many parameters. Details Considering a Class whose constructor has a number of parameters, we have to send all of them to instantiate an instance, which could be error prone and complex sometimes. Also if the constructor has some optional parameters or if we want to have some parameters to have fixed value, we might end up with a number or Constructors for the object. Builder pattern solves this problem by exposing a builder object which can have multiple methods based on different parameters. Example For our example we will consider a Class BlogObject which represents a Blog and has a number of fields. Some of the fields like, Tag, Summary etc are optional. Without Builder we might need to have a number of constructors for the Class. But with Builder we can create the instance of the Object easily a...