Vault

Definition: An utility stores a group of related algorithms. It doesn't store any data. This is merely a way of organizing algorithms. Note: Utility is not a standard design pattern.

Usage: Use an utility when you have a group of related methods that are not complicated enough to each merit their own class.

Example

interface CarnivalConstants {
	//colors
	static final Color buttonTextColor	= Color.black;
	static final Color textRolloverColor	= Color.blue;

	//game panels
	static final int defaultGameHeight	= 768;
	static final int defaultGameWidth	= 1024;
	static final int gameStatusPanelHeight	= defaultGameHeight;
	static final int gameStatusPanelWidth	= 160;

	//look and feel
	static final String lookAndFeelDatFilePath = "lookandfeel.dat";

	//main JFrame "stuff"
	static final String gameTitle		= "Lucky's Puzzle Carnival";

	static final GFDisplayMode defaultDisplayMode =
		new GFDisplayMode(defaultGameWidth, defaultGameHeight, 32);
	static final GFDisplayMode lowResolutionDisplayMode =
		new GFDisplayMode(640, 480, 16);

	static final GFDisplayMode emulatedFullscreenDisplayMode =
		new GFDisplayMode(640, 480, false, GFDisplayMode.ModeType.EMULATED_FULL_SCREEN);
	static final GFDisplayMode maximizedWindowDisplayMode =
		new GFDisplayMode(640, 480, true, GFDisplayMode.ModeType.MAXIMIZED_WINDOW);
} //end interface CarnivalConstants

That was a small excerpt from a constants file for a real game. This is essentially like using global constants. Since the constants are all final, they don't cause the sorts of problems that global variables are notorious for. Even so, some people consider this an "anti-pattern" rather than a "pattern". When used judiciously, this sort of thing can be useful.

The Vault pattern need not be implemented in this way. The data for the vault could be loaded from file. Examine the following example:

class CGLookAndFeel {
	static final int iButtonFontSize	= 0;
	static final int iSmallFontSize		= 1;
	static final int iMediumFontSize	= 2;
	static final int iLargeFontSize		= 3;
	static final int iGiganticFontSize	= 4;

	static Font getDefaultFont() {
		return new Font(defaultFontName, defaultStyle, getFontSize(iMediumFontSize));
	} //end getDefaultFont

	static String getDefaultFontName() {return defaultFontName;}

	static int getDefaultFontStyle() {return defaultStyle;}

	static int getFontSize(final int iFontType) {
		return aFontSize[iFontType];
	} //end getFontSize

	static void loadValuesFromDatFile(final String datFilePath) {
		read the data from a .dat file
	} //end loadValuesFromDatFile

	static void setDefaultFontName(final String newDefaultFontName) {
		defaultFontName = newDefaultFontName;
	} //end setDefaultFontName

	static void setDefaultFontStyle(final int newDefaultFontStyle) {
		defaultStyle = newDefaultFontStyle;
	} //end setDefaultFontStyle

	//PRIVATE CONSTANTS
	//font properties
	private static final String initialDefaultFontName	= "Serif";
	private static final int initialDefaultStyle		= Font.PLAIN;

	//font size properties
	private static final String fontSizeString = "Font_Size";
	private static final int[] aDefaultFontSize = {15, 12, 15, 20, 40};

	//PRIVATE VARIABLES
	//values - set to "sensible" defaults
	private static int[] aFontSize = aDefaultFontSize;

	//font defaults
	private static volatile String defaultFontName = initialDefaultFontName;
	private static volatile int defaultStyle = initialDefaultStyle;
} //end class CGLookAndFeel

Notice the method "loadValuesFromDatFile". This is the method that loads the data from a file. An expanded version of this class is used in real world code.

Index

Copyright (C) 2009 Steven Fletcher. All rights reserved.