单例模式 在一个虚拟机内只存在一个实例,hashcode一致; 需要考虑两点
线程安全
延迟加载1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Example1: /** * Created by Paul on 2016/3/23. */ /** * 1. 静态私有实例 * 2. 私有构造 * 3. 公有静态方法 */ public class SingletonDemo { private static SingletonDemo instance = null; private SingletonDemo() { } public static SingletonDemo getInstance() { if (instance == null) { synchronized (SingletonDemo.class) { if (instance == null) { instance = new SingletonDemo(); } } } return instance; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SingletonDemo2 { private SingletonDemo2 () {} private static class SingleFactory { private static SingletonDemo2 instance = new SingletonDemo2(); } public static SingletonDemo2 getInstance () { return SingleFactory.instance; } public Object readResolve () { return getInstance(); } }
状态模式 State模式在实际使用中比较多,适合“状态”的切换。因为我们经常会使用If else if else 进行状态切换,如果针对状态的这样判断切换反复出现,我们就要联想到是否可以采取State模式了。
show me your code!1 2 3 4 public interface State { public void handle(String sampleParameter); }
1 2 3 4 5 6 7 8 9 10 public class ConcreteStateA implements State { @Override public void handle(String sampleParameter) { System.out.println("this is concreteStateA..." + sampleParameter); } }
1 2 3 4 5 6 7 public class ConcreteStateB implements State { @Override public void handle (String sampleParameter) { System.out.println("this is concreteStateB..." + sampleParameter); } }
1 2 3 4 5 6 7 8 9 10 11 //状态管理者 public class Context { private State state ; public void set State(State state ){ this.state = state ; } public void request(String sampleParameter){ state .handle(sampleParameter); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 //测试类 public class TestState { public static void main(String[] args) { State state A = new ConcreteStateA(); State state B = new ConcreteStateB(); Context context = new Context(); context.set State(state A); context.request("happy" ); context.set State(state B); context.request("sorrow" ); } }
观察者模式 来自网易的笔试,在订阅新闻中,运用了什么设计模式? Oberver-Subject 设计模式
1 2 3 4 public interface Observer { public void update(String str); }
1 2 3 4 5 6 7 8 9 10 public interface Subject { public void addObserver (Observer observer ) ; public void removeObserver (Observer observer ) ; public void notifyObservers (String str ) ; }
1 2 3 4 5 6 7 public class ConcreteObserver implements Observer { @Override public void update(String str) { System.out .println(str); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class ConcreteSubject implements Subject { private List<Observer> observers = new ArrayList<Observer>(); @Override public void addObserver (Observer observer) { observers.add(observer); } @Override public void removeObserver (Observer observer) { observers.remove(observer); } @Override public void notifyObservers (String str) { for (Observer observer:observers) observer.update(str); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class TestObserver{ public static void main(String[] args) { Observer observer1 = new ConcreteObserver() ; Observer observer2 = new ConcreteObserver() ; Observer observer3 = new ConcreteObserver() ; Subject subject = new ConcreteSubject() ; subject.addObserver(observer1 ) ; subject.addObserver(observer2 ) ; subject.addObserver(observer3 ) ; subject.notifyObservers("糗事百科" ) ; } }