10.1.3 Object Terminology

An object is an entity that packages state and procedures.

The state variables that are part of an object are called instance variables. The instance variables are stored in places that are part of the application environment for the object. This means they are encapsulated with the object and can only be accessed through the object. An object produced by (make-counter) defines a single instance variable, count.

The procedures that are part of an object are called methods. Methods may provide information about the state of an object (we call these observers) or modify the state of an object (we call these mutators). An object produced by (make-counter) provides three methods: reset! (a mutator), next! (a mutator), and get-count (an observer).

An object is manipulated using the object's methods. We invoke a method on an object by sending the object a message. This is analogous to applying a procedure.

A class is a kind of object. Classes are similar to data types. They define a set of possible values and operations (methods in the object terminology) for manipulating those values.
We also need procedures for creating new objects, such as the make-counter procedure above. We call these constructors. By convention, we call the constructor for a class make- where `` is the name of the class. Hence, an instance of the counter class is the result produced when the make-counter procedure is applied.

Excercise 10.1. Modify the make-counter definition to add a previous! method that decrements the counter value by one.

Excercise 10.2. $\left[\star \right]$ Define a variable-counter object that provides these methods: make-variable-counter: Number $\rightarrow$ VariableCounter: Creates a variable-counter object with an initial counter value of 0 and an initial increment value given by the parameter.
set-increment!: Number $\rightarrow$ Void: Sets the increment amount for this counter to the input value.
next!: Void $\rightarrow$ Void: Adds the increment amount to the value of the counter.
get-count: Void $\rightarrow$ Number: Outputs the current value of the counter.

Here are some sample interactions using a variable-counter object:

> (define vcounter (make-variable-counter 1))
> (ask vcounter 'next!)
> (ask vcounter 'set-increment! 2)
> (ask vcounter 'next!)
> (ask vcounter 'get-count)
3