Home Segments Index Top Previous Next

345: Mainline

Data abstraction, explained in Chapter 14, provides another reason for using setter and getter methods for class variables. You might determine, for example, that the value of ProteinRatio must be equal to the value of CarbohydrateRatio. If you have used setters and getters for the class variables, you can enforce the equality constraint by storing just one number accessed by both setter–getter pairs:

Food class definition 
Object subclass: #Food 
  instanceVariableNames: 'fCalories cCalories pCalories' 
  classVariableNames: 'FatRatio CommonRatio' 
  poolDictionaries: '' 
Food method definition • class 
carbohydrateRatio 
  ^ CommonRatio 
Food method definition • class 
carbohydrateRatio: aNumber 
  CommonRatio := aNumber 
Food method definition • class 
proteinRatio 
  ^ CommonRatio 
Food method definition • class 
proteinRatio: aNumber 
  CommonRatio := aNumber