这里的主要问题是这个类是否负责字体。
是的话,应该Dispose字体:
public class MyFontStorage: IDisposable {
private Font m_MyFont;
...
public Font MyFont {
get {
return m_MyFont;
}
set {
if (m_MyFont == value)
return;
if (m_MyFont != null)
m_MyFont.Dispose();
m_MyFont = value;
}
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
MyFont = null;
}
}
public void Dispose() {
Dispose(this);
GC.SuppressFinalize(this);
}
}
....
using (MyFontStorage storage = new MyFontStorage()) {
...
// Storage is responsible for the font
storage.MyFont = new Font(...);
...
// Control has responsibility for the font as well, that's why
// we have to create a copy in order to each font instance has one owner
MyControl.Font = new Font(MyFontStorage.MyFont, MyFontStorage.Font.Style);
...
}
创建一个副本而不是仅仅分配是一个不好的做法(容易出错),这就是为什么你可能想要实现一个工厂 -
“我应该存储字体名称、大小和样式吗”
public class MyFontFactory {
private string m_FamilyName;
private float m_Size;
private FontStyle m_Style;
...
public MyFontFactory(Font propotype) {
...
m_FamilyName = propotype.FontFamily.Name;
m_Size = propotype.Size;
m_Style = propotype.Style;
}
public Font CreateFont() {
return new Font(m_FamilyName, m_Size, m_Style);
}
}
....
MyFontFactory factory = new factory(SomeControl.Font);
...
// Much more natural:
// MyFontFactory creates a new font and MyControl takes responsibility for it
MyControl.Font = factory.CreateFont();
最后,在辅助类/实用程序/例程等的情况下。
public class MyFontMisc {
private Font m_MyFont;
public Font MyFont {
get {
return m_MyFont;
}
set {
m_MyFont = value;
}
}
// Just some computations
public float EmSizeInMillimeters {
get {
if (null == m_MyFont)
return 0.0;
...
}
}
}
......
// MyFontMisc doesn't hold any responsibility for the font
MyFontMisc misc = new MyFontMisc(SomeControl.Font);
// MyFontMisc just uses font for some computation and doesn't take
// any responsibility for the font (which owns to SomeControl)
Length = misc.EmSizeInMillimeters * 3.0f + 15.0f;