This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An illustration of state design pattern | |
*/ | |
public class StateExample { | |
public static void main(String[] args) { | |
Parcel parcel = new Parcel(); | |
parcel.printStatus(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An illustration of adapter design pattern | |
*/ | |
public class AdapterExample { | |
public static void main(String[] args) { | |
/** | |
* Using AudioPlayer for playing different audio formats | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An illustration of decorator design pattern | |
*/ | |
public class DecoratorExample { | |
public static void main(String args[]) { | |
/** | |
* Creating new Margherita pizza | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |