singleton pattern and visitor pattern in java & js implemented
public class SingleObject {
private static SingleObject instance = new SingleObject();
// PRIVATE: can't create "new" object
private SingleObject() {
}
// Get the only object available
public static SingleObject getInstance() {
return instance;
}
public void showMessage() {
System.out.println("Hello World!");
}
}
✔️ SingleObject object = SingleObject.getInstance();
❌ SingleObject object = new SingleObject();
Not safe in Multi-Thread Environment:
-
loop can be excuted multiple times at the same moment (조건문이 동시에 두번 돌 수 있음)
-
shared value can't be maintained well-> use
synchronized
key wordpublic synchronized static void print(String input) { count++; System.out.println(input + "count : "+ count); }
Interface can't use static methods
Without chaing the classes of the elements on which it operates, it lets you define a new operations
https://www.tutorialspoint.com/design_pattern/design_pattern_quick_guide.htm