여러 글꼴이있는 보고서에 대한 설정 개체가 있습니다.
기본값은 다음과 같습니다.
public Font TitleFont { get; set; } = new Font("Arial", 8, FontStyle.Bold);
그러나 보고서의 gdi + 렌더링에 사용되기 전에 여러 위치에서 변경 될 수 있습니다. 이것은 winForms 컨트롤에 없습니다.
글꼴은 IDisposable을 구현하므로 속성의 setter에서 이전 글꼴을 삭제해야합니까? 아니면 글꼴 이름, 크기 및 스타일을 3 개의 속성으로 저장하고 필요할 때만 글꼴을 만들어야합니까?
여기서 주요 질문은 클래스가 글꼴에 대한 책임 이 있는지 여부 입니다. 그렇다면 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;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다