13-设计模式
策略模式
迭代器模式
void f(){
Collection c = new ArrayList();
g(c.iterator());
}
void g(Iterator it) {
while (it.hasNext()) {
Object o = it.next();
}
}单件模式
抽象工厂模式
最后更新于
void f(){
Collection c = new ArrayList();
g(c.iterator());
}
void g(Iterator it) {
while (it.hasNext()) {
Object o = it.next();
}
}最后更新于
public class Singleton {
private static Singleton instance;
private Singleton() {
// 私有构造函数,防止外部创建实例
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}if (type == 1) {
return new Type1();
} else if (type == 2) {
return new Type2();
}