| Applying science to business management |
  
Using Objects
The previous chapter shows how lists can be used to aggregate
separate data items into a single package. For example, you can
combine 12 monthly sales values into a single data item named Sales.
This allows you to work with the entire sales data set as though
it were a single item.
Sales=[126,178,154,192,116,137,189,122,136,133,181,182];
Total Sales=sum(Sales);
Next Year Sales=Forecast(Sales);
Although you can mix data types in a single list, you
typically use lists to aggregate data of a single type. If you
want to combine data of different types that represent different
attributes of a single item, you should use an object.
Suppose you want to combine all information you have about a
single customer into a package. The package might contain
information like the customer's name, address, phone number, etc.
You can combine this information into an object named customer
and access the individual data items using the following
notation:
customer.name
customer.address
customer.phonenumber
An object is like a list except that individual elements are
accessed by name rather than by an index number. The individual
data elements in an object are called properties.
Once you have combined separate bits of data into an object,
you can treat the object as a single data item. For example,
suppose you have a database of customer objects and you want to
send a form letter to each customer. You can create a function
that constructs the letter using each customer object as
input.
Write Letter(customer):={
var c= customer;
append(dialogtemplate,"letters.txt");
}
// Write Letter form is defined as follows
<%date(1)%>
<%c.name%>
<%c.address%>
<%c.city%> <%c.state%> <%c.zipcode%>
Dear <%c.name%>:
I am writing to you because ...
Inside the Write Letter function you can access
particular properties of the customer object in order to
fill form fields.
Objects go beyond simply allowing you to combine dissimilar
data items. Objects also allow you to combine data with functions
that act on the data. These functions are stored in the object as
a special type of property called a method.
For example, you can create a function named mailingaddress
that simply combines properties of the customer object to
produce a text block suitable for addressing a letter. Now, if
this function is part of the customer object, you can call
the method using an expression like this:
customer.mailingaddress()
Packaging functions that process data with the data itself
makes your code modular. This can greatly simplify the
development and maintenance of some types of applications.
Assume your database of customers includes two kinds of
objects (domestic customers and international customers). Because
international mailing addresses contain different information
from domestic ones (e.g., no state and zip code) the two object
types will have different properties. This difference will cause
the Write Letter function to fail. However, if both object
types contain a mailingaddress method that is appropriate
to the particular object type, you can create a more generic form
of Write Letter.
Write Letter(customer):={
var c= customer;
append(dialogtemplate,"letters.txt");
}
// Write Letter form is defined as follows
<%date(1)%>
<%c.mailingaddress()%>
Dear <%c.name%>:
I am writing to you because ...
Now, no matter what kind of customer object is passed to Write
Letter, the appropriate mailing address will be constructed.
This works because each object knows how to construct its own
mailing address. Furthermore, if you want to add a new type of
customer object, you don't need to be concerned about how new
fields in the object will affect Write Letter. You only
need to know that the new object must expose a name
property and a mailingaddress method.
|