假设我有几个Viewer组件用于显示文本,它们有几种模式供用户切换(不同的字体预设,用于查看文本/二进制/十六进制)。那么,在管理共享对象(例如字体,查找对话框等)方面,什么是最好的方法?我认为具有惰性初始化对象的静态类可能还可以,但这可能是错误的想法。
static class ViewerStatic
{
private static Font monospaceFont;
public static Font MonospaceFont
{
get
{
if (monospaceFont == null)
//TODO read font settings from configuration
monospaceFont = new Font(FontFamily.GenericMonospace, 9, FontStyle.Bold);
return monospaceFont;
}
}
private static Font sansFont;
public static Font SansFont
{
get
{
if (sansFont == null)
//TODO read font settings from configuration
sansFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Bold);
return sansFont;
}
}
}