Sat Dec 29 08:01:37 PST 2018



The Object-Oriented Design Principles are the core of OOP programming, but I have seen most of the Java programmers chasing design patterns like Singleton pattern, Decorator pattern or Observer pattern, and not putting enough attention on learning Object-oriented analysis and design. It's important to learn basics of Object-oriented programming like Abstraction, Encapsulation, Polymorphism, and Inheritance. But, at the same time, it's equally important to know object-oriented design principles, to create the clean and modular design. I have regularly seen Java programmers and developers of various experience level, who either don't hear about these OOP and SOLID design principle, or simply doesn't know what benefits a particular design principle offers, or how to apply these design principle in coding. 








Hello guys, today, we'll talk about design patterns and some of the best online courses to learn design patterns in Java from scratch. If you are wondering what is a design pattern and why Java developers should learn them? then let me give you a brief overview. Design patterns are nothing but a tried and tested solutions of common programming problems, for example, the creational design patterns deal with the problems of object creation. They exist from a long time but made popular by famous Gang of four of Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm in their classic 1994 book
Design Patterns: Elements of Reusable Object-Oriented Software, also known as GOF design patterns. This book documented
24 design patterns which are reusable to solve common programming problems. 








Thread safe Singleton means a Singleton class which returns exactly same instance even if exposed to multiple threads. Singleton in Java has been a classical design pattern like Factory method pattern or Decorator design pattern and has been used a lot even inside JDK like
java.lang.Runtime
is an example of Singleton class. Singleton pattern ensures that exactly one instance of class will remain in Java program at any time. In our last post 10 Interview questions on Singleton in Java we have discussed many different questions asked on Singleton pattern, One of them was writing Thread safe singleton in Java. Prior to Java 5 double checked locking mechanism is used to create thread-safe singleton in Java which breaks if one Thread doesn't see instance created by other thread at same time and eventually you will end up with more than one instance of Singleton class. From Java 5 onwards volatile variable guarantee can be used to write thread safe singleton by using double checked locking pattern. I personally  don't prefer that way as there are many other simpler alternatives to write thread-safe singleton is available available like using static field to initialize Singleton instance or using Enum as Singleton in Java. Let?s see example of both ways to create thread-safe Singleton in Java.






Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html