Define default values via explicit protocol pattern
Jenny Harlow (Talk | contribs) m |
m (Reverted edits by Ebybymic (Talk); changed back to last version by Jenny Harlow) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
This axiom is contained in [[PLoP 1995|Pattern Languages of Program Design]] and republished in [[Ken Auer 1995]]. | This axiom is contained in [[PLoP 1995|Pattern Languages of Program Design]] and republished in [[Ken Auer 1995]]. | ||
− | The | + | ====The Problem==== |
+ | |||
+ | You want to define default values for instance variables, but 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. Where should default values be defined for maximum flexibility? | ||
====The solution==== | ====The solution==== | ||
− | Create a method that sets the default value for the field in question with the name defaultFieldname and use this in all cases to initialise the field. | + | Default values should be identified in one place and that place should be clearly identified. Create a method that sets the default value for the field in question with the name defaultFieldname and use this in all cases to initialise the field. |
OR | OR |
Latest revision as of 03:08, 25 November 2010
This axiom is contained in Pattern Languages of Program Design and republished in Ken Auer 1995.
The Problem
You want to define default values for instance variables, but 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. Where should default values be defined for maximum flexibility?
The solution
Default values should be identified in one place and that place should be clearly identified. Create a method that sets the default value for the field in question with the name defaultFieldname and use 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.