On Github f3dd3rn / school_design_patterns
In general. Twice in detail. References.
behavioral
creational
structural
public class Singleton {
private static Singleton _instance = null;
private Singleton(){}
public static Singleton getInstance(){
if(_instance == null){
_instance = new Singleton();
}
return _instance;
}
}
public class Singleton {
private static Singleton _instance;
private Singleton(){}
public static Singleton getInstance(String classname) throws IllegalArgumentException {
if(singletonName.equals("logger")) {
_instance = Logger.getInstance();
} else if(singletonName.equals("debugger")) {
_instance = Debugger.getInstance();
} else if(singletonName.equals("singleton")) {
_instance = new Singleton();
} else {
throw new IllegalArgumentException("Illegal classname");
}
return _instance;
}
}
public class Singleton {
private static Map<String,Singleton> map = new HashMap<String,Singleton>();
private Singleton() {}
public static Singleton getInstance(String classname) throws IllegalArgumentException {
if(classname == null) {
throw new IllegalArgumentException("Illegal classname");
}
Singleton singleton = (Singleton)map.get(classname);
if(singleton != null) {
return singleton;
}
if(classname.equals("SingeltonSubclassOne")) {
singleton = new SingletonSubclassOne();
} else if(classname.equals("SingeltonSubclassTwo")) {
singleton = new SingletonSubclassTwo();
} else if(classname.equals("Singelton")) {
singleton = new Singleton();
} else {
throw new IllegalArgumentException("Illegal classname");
}
map.put(classname, singleton);
return singleton;
}
}
outsource instantiation
static methods
public class Color {
public static Color make_RGB_color(int red, int green, int blue) {
// do whatever you need to do
}
public static Color make_HSB_color(float hue, float saturation, float brightness) {
// do whatever you need to do
}
public static Color make_HEX_color(String hexCode) {
// do whatever you need to do
}
}
Caching