____  ___    _  _     _   _ _____     _______
 / ___|/ _ \  | || |   | | | |_ _\ \   / / ____|
| |  _| | | | | || |_  | |_| || | \ \ / /|  _|
| |_| | |_| | |__   _| |  _  || |  \ V / | |___
 \____|\___/     |_|   |_| |_|___|  \_/  |_____|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Singleton Design Pattern

BY: @rakeshbecse | CREATED: April 1, 2018, 4:59 a.m. | VOTES: 0 | PAYOUT: $0.00 | [ VOTE ]

Singleton pattern:

Singleton pattern belongs to the creational pattern.

It allows one instantiation or object for JVM

Rules for singleton pattern:

The constructor should be private.

​​private Singleton()

{

}​

We can not create an object outside of the class.

Singleton singleton=new Singleton(); //Not possible to create an object using new keyword because the constructor is private

Advantages of singleton pattern:

It controls multiple object creations by allowing only one-time creation.

Use cases of singleton pattern:

 Logging is a popular use of Singletons.  It provides one single access point to an applications log file.

Issues with singleton pattern:

If two threads call the getInstance() method at the same time, you can end up with two singletons. 

If we make getInstance() method synchronized then it solves the issue, but we need to sacrifice performance because synchronized getInstance()  is a costly operation.

TAGS: [ #softwaredesignpattern ] [ #designpatterns ] [ #singletondesignpattern ]

Replies

@dailyxkcd | April 1, 2018, 5 a.m. | Votes: 1 | [ VOTE ]

The secret of getting ahead is getting started.

@resteemsupport | April 1, 2018, 5 a.m. | Votes: 1 | [ VOTE ]

Hello rakeshbecse!

Congratulations! This post has been randomly Resteemed! For a chance to get more of your content resteemed join the Steem Engine Team

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>