Pattern: Private Methods


Context

Some methods are intended to be used by clients of a class and these therefore make up the class's public protocol. Other methods are for internal use only and are concerned with implementation details. Such private methods should only be called from methods in the same class or from methods in closely related classes where it is permissible for them to have knowledge of such intimate implementation details.

In Smalltalk, unlike C++, there is no way of genuinely hiding particular methods from access by a client. However, developer's of client classes need some way of differentiating between methods they can use and those that they shouldn't.

Solution

We use an informal convention for marking a method as private. This is to begin the method comment with the string 'Private -' (that's Private<space><hyphen><space>).

In future we will also use private and public method categories to convey this information.

Methods marked in this way are not guaranteed to be compatible, or indeed present, in the future releases of the class. Hence they should not be used by clients of the class. They can, however, be used from within closely related classes.

Example

errorNotFound
	"Private - Raise a NotFound exception."
	...

Known Uses

The Color class contains a private method #defaultPalette.

defaultPalette
	"Private - Answer the default palette."

	^GDILibrary default getStockObject: DefaultPalette

Related Patterns