Skip to main content

Posts

Showing posts with the label Design pattern

[Brainshare] Singleton Design pattern

What is Singleton Design Pattern: A class has only SINGLE instance and provide a global access to it. Usage of Singleton Design Pattern: https://stackoverflow.com/a/3192124/4955676 Type of Singleton Design Pattern: [code] Singleton Class - READ MORE [code] Lazy Initialization - READ MORE [code] Thread Safe - READ MORE Singleton Design Pattern Video Book

Builder Design Pattern - Java

Builder Design Pattern - Java When the construction of the class contains a large number of argument then this Design Pattern is preferred. Builder Pattern, as the name suggest, it helps to reduce the complexity in Building Java Object. public class Person { private String firstname; private String lastname; private String address; private int age; private String city; private String state; public Person(String firstname, String lastname, String address, int age, String city, String state) { super(); this.firstname = firstname; this.lastname = lastname; this.address = address; this.age = age; this.city = city; this.state = state; } } In the above example, you can see, that the construction of the Person class has 6 arguments and few of them can be optional. So we can create a new PersonBuilder class which will help to create the Object of the Person class. public class PersonBuilder { private String firstname; ...

Factory Design Pattern - Java

Factory Design Pattern Factory Design Pattern works as per it's name. As one Factory can generate various types of items but of a specific category. Like, Car Factory ( Nissan Car Factory ) can create cars like, Micra, Sunny, Evalia but it can't create Duster. Because,Duster can be created by Renault Car Factory. So, with the same concept, A company can create a various types of Employees. Like, Developer, Admin , Developer Manager. But, only Developer can be Dev Manager. Here is the Complete example of creating the Company with all the Employee details. Class Company.java /** * Parent Class of all Class. Employee is Base class of all types of Employee. * * @author Hari * */ abstract class Employee { public abstract void getMyDetails(); } /** * Admin is child class of the Employee class. * * @author Hari */ class Admin extends Employee { public void getMyDetails() { System.out.println("I'm admin"); } } /** * Develo...

Singleton Design Pattern in Java

Singleton Design Pattern Singleton Design Pattern means able to create only one object of the class. When you want to restrict some class from being created multiple objects, this design pattern is being used. First step of creating the class singleton is to make it's constructor private so other class can't call it anytime and create and object of it. Then create a static method "getInstance" which will return the object of the same class . Here its SingletonExampleClass . This method (getInstance) is created static , because this class has private constructor so we won't have it's object with us to access this class first time. Synchronized block is used stop multiple threads to access the instantiation code block at the same time, else it will be able to create multiple objects. now assume that two thread are there T1 and T2. Not Thread T1 access this method first and acquire the lock. While Thread T1 was accessing the code inside Synchronized bl...