Managing shared resources between classes?

Imagine that I have several Viewer components that are used to display text, and they have several modes that the user can switch (different font presets for viewing text / binary / hexadecimal). What would be the best approach for managing shared objects - such as fonts, search dialogs, etc.? I realized that a static class with lazily initialized objects would be fine, but that might not be the right idea.

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;
        }
    }
}
+5
source share
2 answers

, , , : Singleton Cache. , Singleton . , , . , , , , . , .

Singleton, , , , Lazy init. . , (, - - ), . , , . , !

, "ViewerStatic" . -, "". "". , . , . ViewerStatic . "FontFlyWeights", , "ConstantStrings" "SystemDialogFactory"... ..

+1

, ? , , , , .

, , , ?

+1

All Articles