Give example of APIs in Java which uses Singleton and Factory method patterns.

Give example of APIs in Java which uses Singleton and Factory method patterns.

Ninja Asked on 18th September 2018 in Design Pattern.
Add Comment
1 Answer(s)
Best answer

Singleton Pattern Example:

java.lang.Runtime.getRuntime()

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The Runtime.getRuntime() method in java.lang package returns the same instance every time it is called.

Factory Method Pattern Example:

java.util.Calendar.getInstance()

java.util.Calendar is an abstract class. The getInstance() method of Calendar class is a static method that returns an implementation of the Calendar class depending on the input. The default method without parameter returns an implementation of Calendar class based on the current time in the default time zone with the default locale.

java.util.Calendar.getInstance(Locale aLocale)

This is an overloaded method which returns a calendar using the default time zone and specified locale. The Calendar returned is based on the current time in the default time zone with the given locale.

Ninja Answered on 18th September 2018.
Add Comment