多例模式【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)); } }
一天一个设计模式,嘿嘿!
时间: 2024-10-09 09:54:59