首页 > 代码库 > 多例模式

多例模式

多例模式【Multition Pattern 】

public class Emperor {
    public String name = "";
    private static int numOfEmperor = 2;
    private static ArrayList<Emperor> e = new ArrayList<Emperor>();
    static {
        for (int i = 0; i < numOfEmperor; i++) {
            e.add(new Emperor("emperor"+i));
        }
    }
    private Emperor(String name){
        this.name = name;
    }
    public static Emperor getInstance(){
        Random r = new Random();
        return e.get(r.nextInt(numOfEmperor));
    }
}

一天一个设计模式,嘿嘿!

多例模式