Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
complexvalues [2019/01/04 13:21] christian [Anatomy of a Value] |
complexvalues [2019/01/04 16:38] christian [Using Values] |
||
---|---|---|---|
Line 10: | Line 10: | ||
Now, I use Values for more than 10 years and I cannot do without it anymore. Most of the classes I define are Values. Objects are used for the " | Now, I use Values for more than 10 years and I cannot do without it anymore. Most of the classes I define are Values. Objects are used for the " | ||
+ | |||
+ | ==== Using Values ==== | ||
+ | |||
+ | To define a new Value class: | ||
+ | - create a new subclass of Value without instance variables and " | ||
+ | - edit he class method # | ||
+ | - add a pragma for each instance variable describing the variable | ||
+ | - open the popup menu on the new class and select "add Value methods..." | ||
+ | - edit the class method #example to provide a useful value | ||
+ | |||
+ | Done. | ||
+ | |||
+ | Now you have: | ||
+ | * a class with the specified instance variables | ||
+ | * an accessor for each variable with the same name | ||
+ | * an initializer with all parameters which sets up a fresh value | ||
+ | * a constructor taking all parameters and the sole caller of the initializer | ||
+ | * (2^^< | ||
+ | * an example | ||
+ | |||
+ | Lets have an example for example: | ||
+ | <code smalltalk> | ||
+ | Person | ||
+ | name: ' | ||
+ | sex: #male | ||
+ | birthday: (Date d: 25 m: 6 y: 1960) | ||
+ | </ | ||
+ | |||
+ | Withit you can: | ||
+ | * create a value with <code smalltalk> | ||
+ | * ask for its parts <code smalltalk> | ||
+ | * print it as code <code smalltalk> | ||
+ | ' | ||
+ | name: '' | ||
+ | sex: #male | ||
+ | birthday: (Date d: 25 m: 6 y: 1960)' "</ | ||
+ | * get the value from its code <code smalltalk> | ||
+ | * add fancy access methods like <code smalltalk> | ||
+ | weekdayAtBirth | ||
+ | ^self birthday weekday | ||
+ | </ | ||
+ | |||
==== Anatomy of a Value ==== | ==== Anatomy of a Value ==== |