Skip to content

Instantly share code, notes, and snippets.

View viveknaskar's full-sized avatar
🎯
Focusing

Vivek Naskar viveknaskar

🎯
Focusing
View GitHub Profile
@viveknaskar
viveknaskar / SingletonDeserializationExample.java
Created May 17, 2021 17:52
Illustration of breaking Singleton pattern using Serialization & Deserialization
import java.io.*;
/**
* Using Serialization & Deserialization, we can create new instance
* thereby breaking SingletonPattern
*/
public class SingletonDeserializationExample implements Serializable {
private static SingletonDeserializationExample single_instance = null;
@viveknaskar
viveknaskar / SingletonCloningPreventExample.java
Created May 17, 2021 16:31
Illustration of breaking Singleton pattern using Cloneable interface
/**
* By explicitly throwing CloneNotSupported exception from the clone()
* would prevent object creation the second time, thereby preventing
* the breaking of Singleton Pattern
*/
public class SingletonCloningPreventExample extends SuperCloneableClass {
private static SingletonCloningPreventExample single_instance = null;
/**
@viveknaskar
viveknaskar / SingletonCloningExample.java
Created May 17, 2021 16:02
Illustration of breaking Singleton pattern using Cloneable interface
/**
* Using Cloneable Interface, we can create more than one instance in a Singleton class by
* simply using the clone() on the first object
*/
public class SingletonCloningExample implements Cloneable {
private static SingletonCloningExample single_instance = null;
@Override
protected Object clone() throws CloneNotSupportedException
@viveknaskar
viveknaskar / SingletonReflectionPreventExample.java
Last active May 17, 2021 15:25
Illustration of preventing breakage of Singleton pattern using Reflection API
import java.lang.reflect.Constructor;
/**
* Illustration of preventing breakage of Singleton pattern using Reflection API
*/
public class SingletonReflectionPreventExample {
private static SingletonReflectionPreventExample single_instance = null;
/**
* Making the constructor as private
@viveknaskar
viveknaskar / SingletonReflectionExample.java
Created May 17, 2021 14:39
Illustration of breaking Singleton class using Reflection API
import java.lang.reflect.Constructor;
/**
* Using ReflectionAPI, we can create more than one instance in a Singleton class
*/
public class SingletonReflectionExample {
private static SingletonReflectionExample single_instance = null;
/**
* Making the constructor as private
@viveknaskar
viveknaskar / StateExample.java
Created March 11, 2021 18:10
An illustration of State Design Pattern
/**
* An illustration of state design pattern
*/
public class StateExample {
public static void main(String[] args) {
Parcel parcel = new Parcel();
parcel.printStatus();
@viveknaskar
viveknaskar / AdapterExample.java
Created March 11, 2021 17:36
An illustration of Adapter Design Pattern
/**
* An illustration of adapter design pattern
*/
public class AdapterExample {
public static void main(String[] args) {
/**
* Using AudioPlayer for playing different audio formats
*/
@viveknaskar
viveknaskar / DecoratorExample.java
Last active March 11, 2021 15:45
An illustration of Decorator Design Pattern
/**
* An illustration of decorator design pattern
*/
public class DecoratorExample {
public static void main(String args[]) {
/**
* Creating new Margherita pizza
*/
@viveknaskar
viveknaskar / FactoryMethodExample
Created March 11, 2021 13:27
An illustration of Factory Method Design Pattern
import java.util.Scanner;
public class FactoryMethodExample {
public static void main(String[] args) {
System.out.println("Enter the type of car:\nAudi \nTesla");
Scanner in = new Scanner(System.in);
String carType = in.nextLine();
@viveknaskar
viveknaskar / SingletonExample.java
Created March 11, 2021 10:17
An illustration of Singleton Design Pattern
/**
* Singleton is a design pattern by which you create a singleton class that has only one instance
* at any time
*
*/
public class SingletonExample {
private static SingletonExample single_instance = null;
public String msg;