Delegate Responsibility

The getter is an Anti-Pattern when it reveals the internals of a class to the caller. It is often indicative of an Anemic Domain Model. An Anemic Domain Models are thin on behaviour and is little more than a data model. Avoid this anti-pattern by maintaining the principle of Encapsulation. Use the principle of a single responsibility (the composite class should not need knowledge of its component class) to refactor the class and use delegation (the composite class tells the component class what to do). In short Tell Don't Ask.

Delegate responsibility for the required behaviour to the delegate class.

use :

	// delegate resp
	customer.printAddress( System.out ) ;

not :

	System.out.println( customer.getAddress() ) ;

“Get and set methods are evil.” (Allen Holub)

oo_delegate_responsibility.txt · Last modified: 2009/09/22 14:42 (external edit)