- Creational Design Patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
- Singleton design pattern is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
- Builder design pattern is a creational pattern that separates the construction of a complex object from its representation, allowing the same construction process to create different representations. It is particularly useful when an object has a large number of optional parameters or when its construction involves a complex, step-by-step process.
- Prototype design pattern is a creational pattern that creates new objects by copying an existing object, known as a prototype. This is beneficial for complex or costly object creation, as it's more efficient to clone a pre-configured prototype than to build a new object from scratch. To implement it, you create a Prototype interface with a clone() method that concrete classes implement to copy themselves.
- Factory design pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify their exact classes. Rather than by calling a constructor, this is accomplished by invoking a factory method to create an object. Factory methods can be specified in an interface and implemented by subclasses or implemented in a base class and optionally overridden by subclasses.
Thursday, 30 October 2025
Explain some of the Creational Design Patterns ?
Tuesday, 28 October 2025
How to change the last commit message in Git ?
git commit --amend -m "Added application properties"
Sometimes, the above git commit --amend command execution opens up a editing tool like notepad++ with the previous commit message, so that we can alter the commit message. Once after saving this message and closing this editing tool, this changes the log previous message.
Use git log --oneline to check all the commits and commit messages.
Tuesday, 14 October 2025
How to use BiFunction functional interface in java 8 ?
BiFunction is a Functional interface with only one abstract method apply(T t, U u), which takes two arguments and return a result of type R. It is a generic interface defined as BiFunction<T, U, R>
BiFunction interfaces are typically implemented as lambda expressions.
import java.util.function.BiFunction;
import java.util.function.Function;
public class BiFunctionExample {
public static void main(String[] args) {
BiFunction<String, String, String> biFunction = (s1, s2) -> s1 + s2;
System.out.println(checkStr(biFunction));
Function<String, String> function = r -> "Result : " + r;
System.out.println(biFunction.andThen(function).apply("b", "c"));
}
private static Boolean checkStr(BiFunction<String, String, String> biFunction) {
if ("ab".equals(biFunction.apply("a", "b"))) {
return true;
}
return false;
}
}
Result:
true
Result : bc
Monday, 13 October 2025
How to use Duration class in java.time ?
It represents a time-based amount of time such as `34.5seconds`, `2 hours`. It deals with quantity of time with seconds and nanoseconds precision as well. In addition, DAYS unit can be used and it is treated like 24 hours amount of time, without considering any daylight saving effects. For date based amounts of time one can use java.time.Period class.
Duration class is immutable class and thread-safe. Duration class has static factory methods ofDays(), ofHours(), ofMinutes(), ofSeconds(), ofMillis(), and ofNanos() like for Duration instance creation.
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationExample {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime then = LocalDateTime.of(2025, 10, 13, 22, 52, 0);
System.out.println("Current date time: " + now);
//30 minutes amount of time
Duration duration = Duration.between(now, then);
System.out.println("Duration between now and then " + duration);
//add 10 hours and 2 minutes for the duration
Duration changedDuration = duration.plusHours(10).plusMinutes(2);
System.out.println("changed duration : " + changedDuration);
Duration lessDuration = Duration.ofSeconds(30);
//convert duration to nanoseconds and millis
System.out.println("less duration in nanos : " + lessDuration.toNanos());
System.out.println("less duration in millis : " + lessDuration.toMillis());
}
}
Result:
Current date time: 2025-10-13T21:55:26.190489700
Duration between now and then PT56M33.8095103S
changed duration : PT10H58M33.8095103S
Sunday, 12 October 2025
How to use Clock class in java.time package ?
Clock class is an abstract class defined in java.time package. Use of Clock class is optional compared to date time classes defined in java.time package.
Clock class has some static method to get UTC time, to print Time Zone information.
import java.time.Clock;
import java.time.ZoneId;
public class ClockExample {
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
//utc time
System.out.println(clock.instant());
System.out.println("------------------------------");
Clock clock2 = Clock.systemDefaultZone();
System.out.println(clock2);
// always instant() shows the UTC time
System.out.println(clock2.instant());
// current time zone.
System.out.println(clock2.getZone());
//results when executed in "Asia/Calcutta" timezone.
Clock clock3 = Clock.system(ZoneId.of("America/Los_Angeles"));
System.out.println(clock3);
//still the UTC time
System.out.println(clock3.instant());
System.out.println(clock3.getZone());
}
}
Result:2025-10-12T07:15:55.975588400Z
------------------------------
SystemClock[Asia/Calcutta]
2025-10-12T07:15:56.023460400Z
Asia/Calcutta
SystemClock[America/Los_Angeles]
2025-10-12T07:15:56.023460400Z
America/Los_Angeles
References: Clock class usage - optional
How to use UUID class of java.util package ?
We can generate a UUID (Unique ID) in java using UUID class of java.util package.
import java.util.UUID;public class UniqueIdGenerator {
public static void main(String[] args) {
String value = UUID.randomUUID().toString().replace("-", "");
System.out.println("uuid = " + value);
}
}
uuid = 5029262172c74f6cab932f30cb23bb07
Tuesday, 7 October 2025
What is AtomicBoolean? How to use it?
- Use primitive boolean or Boolean wrapper class when dealing with single-threaded contexts or when thread-safety is handled by synchronization mechanisms.
- Use AtomicBoolean class specifically when you need to perform thread-safe, atomic operations on a boolean value in a concurrent environment, especially flags that needs to be updated reliably by multiple threads.
Saturday, 20 September 2025
What is Keycloak ?
Keycloak is used to manage user authentication and access control for application and services, provides centralized security through features like Single Sign-On (SSO), identity brokering (connecting to external identity providers like Google or corporate directories), and user management. It acts as an Identity and Access Management (IAM) tool to secure modern applications, mobile apps, and REST APIs by simplifying the process of adding authentication and protecting services.
Features & Use Cases:
- Single Sign-On (SSO): Users log in once to Keycloak and can then access multiple connected applications without re-entering their credentials.
- Identity Brokering: Allows users to log in using existing external identity providers, such as social media accounts (like Google or Facebook) or enterprise user directories like LDAP and Active Directory.
- Authentication & Authorization: Keycloack handles user identity verification and then grants or denies access to resources based on defined policies.
- User Management: Provides an Admin Console for configuring users, roles, and groups, and an Account Management Console for users to manage their own profiles.
- Security Protocols: Supports standard authentication protocols such as Open ID Connect and SAML 2.0, using token like JWTs for secure communication.
- Client Adapters: Help secure different types of applications (web, mobile, REST APIs) with minimal effort.
- Customization: Offers theming for UI customization and extensibility through code, as well as ability to define password policies.
What is OKTA ?
Okta is a cloud-based identity and access management (IAM) platform that secures and streamlines user access to applications and resources across any device. It provides Single Sign-On(SSO) to reduce multiple logins, enables multi-factor authentication (MFA) for added security, and offers administrative tools to manage user identities and access policies for both employees and customers.
Key Functions:
- Single Sign-On (SSO): Users can log in once to access all their connected applications and services without re-entering credentials for each one.
- Multi-Factor Authentication (MFA): Adds layers of security beyond a simple password by requiring users to provide multiple forms of verification, such as a code from a mobile app or a fingerprint.
- Universal Directory: A centralized user directory that manages identities across different applications, simplifying user provisioning and deprovisioning.
- Adaptative Security Policies: Allows administrators to set dynamic security rules based on factors like a user's location, device or behavior, providing flexible and responsive security.
- Integration and Connectivity: Okta integrates with thousands of applications through its Okta Integration Network (OIN), connecting any user to any application.
- Developer Tools: Provides APIs and SDKs for developers to build identity controls directly into their own applications.
Difference between OAuth 2.0 and OpenID Connect
- Focus: Grants access to resources (e.g. an API) on behalf of a user.
- Purpose: Allows a third-party application to perform actions or access data on another service without needing the user's credentials.
- Tokens: Issues an Access Token, which is used to access the user's resources.
- User Information: Does not standardize or provide user identity information; its focus is on the granted permissions (scopes).
- Focus: Authenticated a user and provides identity information to the client application.
- Purpose: Enables Single Sign-On(SSO) and allows applications to get basic user profile data, such as name and email.
- Tokens: Issues both an Access Token and an ID Token. The ID Token is a JWT containing user identity and authentication details.
- User Information: Defines scopes like openid, profile, and email, to request specific user profile information that is returned in the ID Token.
- Mechanism: It's an extension of OAuth 2.0; the "openid" scope is required to make a request an OIDC request.
Thursday, 30 November 2023
Python Modules
In any language, if the program is getting bigger, you may have to split it into more files for easier maintenance. You may also want to use a function defined in a file without copying the entire logic in each file. To support this, Python has a way to put the definitions in a file and use them in a script wherever required to use those functions you have defined in that file. Such file is called as a module in Python. It always ends with .py extension.
You can call a function / definition defined in a module file using import statement in a script. For instance, I am defining a calc module with some definitions.
File: calc.py
Sunday, 14 November 2021
What is LocalDate in Java 8 ?
LocalDate class is an immutable & thread safe class. Its based on ISO-8601 calendar system.
It represents a date without any time components. Its date format often viewed in the form year-month-day {2021-11-14}.
Using this class, you can also access other data fields like :
> day-of-year
> day-of-week
> week-of-year
Example:
public class LocalDateEx{
public static void main(String[] args) {
LocalDate date = LocalDate.of(2021, 4, 14);
System.out.println("Year of the given date "+ date.getYear());
System.out.println("Month of the given date "+ date.getMonth());
System.out.println("day of week of the given date "+ date.getDayOfWeek());
System.out.println("day of month of the given date "+ date.getDayOfMonth());
System.out.println("day of year of the given date "+ date.getDayOfYear());
System.out.println("day of year of the given date "+ date.getDayOfYear());
System.out.println("month of year(1 to 12) of the given date "+
date.getMonthValue());
System.out.println("is given date a leap year? "+ date.isLeapYear());
System.out.println(date);
}
}
Factorial of any number in Java 8 - using reduction
Reduction - will reduce a stream of values to a result by using an identity (may be an initial value) and a function that will be applied internally on that stream. This processing may not be sequentially, its parallel stream.
import java.util.stream.LongStream;
public class Factorial {
public static void main(String[] args) {System.out.println(facto(5));}
}
public static int facto(int n){
return IntStream.rangeClosed(1, n)
.reduce(1, (int a, int b) -> a * b);
}
we can either use LongStream or IntStream. LongStream.rangeClosed(1, n) gives stream of values from 1 to n,n is included here.if you want to exclude nth value,you can useLongStream.range(1, n) gives stream of values from 1 to n-1.reduce function will apply a function a*b with 1 as initial value(also known as identity)Its the similar operation as the following code.long result = identity;
* for (long element : this stream)
* result = accumulator.applyAsLong(result, element)
* return result;But the above code performs the same operation in sequential manner,but the reduce will apply the function in parallel form.