Monday 7 February 2022

Singleton - Creation Design Pattern

 Singleton - Creational Design Pattern 

Like, every other post, lets start with our 3 prime questions. What, Why and How?

What is Singleton Design Pattern:
It is one of the creational design pattern. 

Why it is used?
It is used to serve a purpose of avoiding a class creating multiple objects. It can only be used when a class should have a single instance available to all clients. If object creation frequency is high,  Creating multiple objects sometimes proves costly. Singleton class is used for logging, database connection, driver objects, caching and thread pool.

How to use it?
Please refer to the below screenshot on how we can implement singleton class

1. We need to create a Class which will work as singleton. In my case, the class name is SingletonClass.java
 I'll explain you line by line to get a better understanding of implementation. 
LINE NO 5: we have created a singletonClass object as private and static. 
  • private because we don't want anyone to access it from outside.
  • static because we want to access it as a class level. If it is not declared as static then to access it, we may need to create an object of class. This explanation is  partly dependent on LINE NO 12.
LINE NO 7: We have created constructor as private because we don't want anyone to create an object of this class from outside.(as we are creating this class based on a singleton principle)

LINE NO 12: As object can not be created from outside the class, hence while accessing it from outside 
we have created a method getSingletonClassAPI as static. This method will give us an object of class singleton. This static method is justification of LINE NO 5, why we created the variable as static too.
Working:
  • If the received argument of Class reference(i.e., SingletonClassObj) is not null, it will treat it as the object of class is already created and we don't need to create a new instance. Hence, it will return the same instance.
  • On other hand, argument of class reference is null, then it will check if the object of the class i.e(singletonClass) is not null. If it is not null, then it will return the class object.
  • If the Object of the class is also null then it will create a new instance of the class and return the same.
This way we can implement an singleton class


Below is the snapshot of the class which calls the API to create a singletonClass object.


Here is the output:
As you can see, It print the same reference that means the same instance is always return.





Saturday 5 February 2022

Design Pattern

 Design Patterns

To understand anything new in technology, these are 3 main question one should always ask, i.e., What, Why and How?
WHAT -> What it is?
WHY -> Why should we use it?
HOW -> How should we use it?

So, lets understand the design pattern based on same 3 questions?

1. What is Design Pattern? What does it mean?
Ans: Design patterns are simply as word says it is a pattern in which certain design is made/approached.
In technical world, they are the reusable solutions to the common problems that occur in software design.

2. Why we should use Design Pattern?
Ans: The idea to use design pattern is to speed up the development process by providing well-tested, proven software paradigms. Design pattern helps you to make your code more flexible, reusable and maintainable. Every design pattern has its goal and you should know which one to pick and implement based on the problem in project.
To just give an example, we all mostly come across or worked on database connection, we can't create database connection object every time we need it. As it can be costly. So, to tackle or avoid a situation, by not letting user create multiple database connection we can use singleton design pattern

To understand How? Firstly, we need to understand which are the types of design pattern. We need to go one by one to understand the how part.

Below is the hierarchy level representation of Design Patterns:

Design Pattern Hierarchy






Tuesday 24 November 2020

JDK 18 - Understanding Functional Interface

 What is Functional Interface?

Functional Interfaces: An interface is called a functional interface if it has a single abstract method irrespective of the number of default or static methods.

  • Annotation @FunctionInterface used(optional).
  •  It should have only 1 abstract method.
  • Two abstract methods give compilation error(given annotation is written @FunctionalInterface).
  • It can have any number of default or static methods.

Why it is used and what are the benefits?
  • Earlier we used to use Anonymous class to serve the purpose of inline method implementation. This approach with an anonymous class is tedious, lengthy, and not easy to read.
  • Functional Interface helps us achieve the same benefit of anonymous class but in a concise form.

Below is the example of Anonymous class and functional interface serving the same purpose.

FunctionalInterfaceImpl


AnonymouseClassDemo


FunctionalInterfaceDemo


As we can see from the above examples, to implement different behavior of doOperation method, Lamda expression approach looks more concise and easy to read than Anonymous Class.








Tuesday 5 September 2017

Static, instance blocks in Java

Static and Instance Block

Static and non-static blocks are more of the same in syntax. A block can be static if we write a static keyword before the block.



Static Block:

  • java's static block is a group of statements that gets executed when the class gets loaded by ClassLoader
  • It is generally used to create static resources when the class is loaded.
  • static block code gets only once at the time of class loading
  • static blocks always gets executed first, We can have multiple static blocks, but it doesn't make much sense
  • static block can't access non-static variables and methods.

Non-Static/ Instance Block:
  • Instance block will get executed on each object creation but,  before the constructor. 
  • It can access static as well as non-static variables and methods