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.ArrayList; | |
import java.util.List; | |
public class ProxyInternet implements Internet { | |
private List<String> restrictedUrls; | |
private ActualInternet actualInternet; | |
public ProxyInternet() { | |
createRestrictedUrlList(); |
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
public class ActualInternet implements Internet { | |
@Override | |
public void connectToUrl(String url) { | |
System.out.println("Making connection to: " + url); | |
} | |
} |
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
public interface Internet { | |
void connectToUrl(String url) throws Exception; | |
} |
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
public class ProxyPatternMain { | |
public static void main(String[] args) { | |
Internet internet = new ProxyInternet(); | |
try { | |
internet.connectToUrl("www.google.com"); | |
internet.connectToUrl("www.facebook.com"); | |
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
public class BillPughSingleton { | |
private BillPughSingleton() { | |
} | |
private static class SingletonHelper { | |
private static final BillPughSingleton INSTANCE = new BillPughSingleton(); | |
} | |
public static BillPughSingleton getInstance() { |
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
public class ThreadSafeCheckSingleton { | |
private static ThreadSafeCheckSingleton instance; | |
private ThreadSafeCheckSingleton() { | |
} | |
public static ThreadSafeCheckSingleton getInstanceUsingDoubleLocking(){ | |
if(instance == null){ | |
synchronized (ThreadSafeCheckSingleton.class) { |
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
public class ThreadSafeSingleton { | |
private static ThreadSafeSingleton instance; | |
private ThreadSafeSingleton() { | |
} | |
public static synchronized ThreadSafeSingleton getInstance() { | |
if (instance == null) { | |
instance = new ThreadSafeSingleton(); |
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
public class LazyInitialisationSingleton { | |
private static LazyInitialisationSingleton instance; | |
private LazyInitialisationSingleton() { | |
} | |
//This is initialised only when the object is demanded for the first time | |
public static LazyInitialisationSingleton getInstance() { | |
if (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
public class StaticBlockSingleton { | |
private static StaticBlockSingleton instance; | |
private StaticBlockSingleton() { | |
} | |
//Static block initialization for exception handling | |
static { | |
try { |
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
public class Main { | |
public static void main(String[] args) { | |
EagerSingleton eagerSingleton = EagerSingleton.getInstance(); | |
} | |
} |
NewerOlder