Define default values via explicit protocol pattern
From CSSEMediaWiki
(Difference between revisions)
m |
|||
Line 8: | Line 8: | ||
Create a class to take the place of the default value and use this class wherever an object of this type needs a default value. A good example of this is [[Introduce Null Object]]. | Create a class to take the place of the default value and use this class wherever an object of this type needs a default value. A good example of this is [[Introduce Null Object]]. | ||
+ | |||
+ | == See also == | ||
+ | * [[Ken Auer 1995]] |
Revision as of 02:16, 3 August 2009
The problem is that if you define default values as initialisation in any constructor or class method then you have to be able to find all places where this default is used if you want to change it, Or you would have to override the methods that are doing the initialisation.
The solution
Create a method that sets the default value for the field in question with the name defaultFieldname and using this in all cases to initialise the field.
OR
Create a class to take the place of the default value and use this class wherever an object of this type needs a default value. A good example of this is Introduce Null Object.