- A Singleton class is a class whose one object is created at a time.
- Following are the different ways of creating a singleton class
- Eager Initialisation
- Lazy initialisation
- Synchronise block
- Double check lock(There is a memory issue, resolve through volatile instance variable).
- Double check locking creates a memory issue as all the threats create a new object in the cache memory.
- Another issue is of reordering that is one thread has updated the value of singleton class But it is unavailable to other thread Since it is in the cache Of first thread only.
- Both above issues are resolved using volatile keyword
- Any read or write Operation to the volatile object by thread happens from the shared memory only and not from cache.
- This process is slow as volatile, uses memory and not cache.
- Synchronised locks the entire block while a thread is processing.
- To overcome above issues, we will use Bill Pugh Solution.
- https://github.com/gauravmatta/javacodes/blob/2be6738842c0a29995d184439a567b9b1b7f8da7/designpatterns/src/com/springimplant/patterns/singleton/DoubleLockingInitialization.java
- Bill Pugh Solution
- This is similar to eager initialisation with a difference that object is initialised in a nested static class.
- Whenever it is used, then only it will be loaded and not loaded by default.
- This solution is very fast, comparatively.
- https://github.com/gauravmatta/javacodes/blob/2be6738842c0a29995d184439a567b9b1b7f8da7/designpatterns/src/com/springimplant/patterns/singleton/BillPughSolution.java
- Enum
- In Enum By default All constructors are private.
- Only one object is created as only one snapshot of Enum per JVM is present.
- By default, enums are singleton only.
- So Enum specify All the conditions automatically, singleton.
- CQRS Design Pattern
- FAANG/MAANG Engineer
- Low Code Platforms
- Software Testing
- Secure Coding
- IOT
- Microservices
- Web Security
- UML Diagrams
- Fork–join model
- Dependency Injection
- Adapter Pattern
- Join Pattern
- DevOps
- Agile
- Security Awareness
- API gateway pattern
- Serverless Architecture
- Event Driven API paradigms
- Rational Application Development
- COTS
- Rational Software Architecture
- N layer/tier Architecture
- OAuth 2.0
- Scaling Microservices
- SAGA Pattern
- Circuit Breaker Pattern
- Singleton
- Immutable
Singleton
Subscribe to:
Posts (Atom)
Recursion
Q What do you understand by a Recursive Programme? Recursion Is the process of repeating items in a self similar way. In programming langua...
-
Inheritanceis tightly coupled. Inheritance is basically used for polymorphism which can be done in other ways to. Generally over a time we d...
-
How can we secure our micro-services? Application security User security Spring access controls Framework security Interaction with downstre...
-
Q What is the difference between wireless sensor networks and IOT networks? Ans : IoT exists at a higher level then WSN. In other words,...
No comments:
Post a Comment