少年游

欲买桂花同载酒,终不似,少年游。

0%

Java设计模式

单例模式

在一个虚拟机内只存在一个实例,hashcode一致;
需要考虑两点

  1. 线程安全
  2. 延迟加载
    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
/**
* Created by Paul on 2016/4/26.
*/
//具体状态A
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
//具体状态B
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 setState(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 stateA = new ConcreteStateA();
State stateB = new ConcreteStateB();
Context context = new Context();
context.setState(stateA);
context.request("happy");
context.setState(stateB);
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("糗事百科");
}
}