【发布时间】:2015-07-11 17:48:00
【问题描述】:
在我的应用程序中,我需要处理多个字体文件。因此,我没有每次都创建新实例,而是实现了 Singleton 来获得这样的字体:
public class FontSingleton {
private static FontSingleton instance;
private static Typeface typeface;
private FontSingleton(){
//private constructor to avoid direct creation of objects
}
public static FontSingleton getInstance(Context context,String fontFileName){
if(instance==null){
instance = new FontSingleton();
typeface = Typeface.createFromAsset(context.getResources().getAssets(), fontFileName);
}
return instance;
}
public Typeface getTypeFace(){
return typeface;
}
}
现在,我可以像这样得到typeface:
FontSingleton.getInstance(mContext,"font1.otf").getTypeFace();
是处理内存泄漏和实现单例的正确方法吗?我是设计模式和Android的新手。谁能指导我正确的方法?
【问题讨论】:
标签: java android android-layout design-patterns singleton