|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 5
CoffeeCup and the virtual caféThe discussion of object design in this and subsequent articles will make use of a class that models coffee cups. You can imagine using this class in a program that implements a "virtual café": a place in cyberspace where guests can sit at small tables, sipping virtual cups of coffee and chatting with one another. The primary function of the café is that of a chat room, where people separated by (potentially vast) physical distances yet connected to the same network come together to converse. To make your chat room more compelling, you want it to look like a café. You want each participant to see graphical representations ("avatars") of the other people in the café. And, to make the participants' experience more real, you want the people to be able to interact with certain items in the café, such as tables, chairs, and cups of coffee.
The CoffeeCup
The basic CoffeeCup class has one instance variable, innerCoffee, which keeps track of the number of milliliters of coffee contained in the cup. This variable maintains your virtual coffee
cup's state. The following methods allow you to change its state by:
add() method)
releaseOneSip() method)
spillEntireContents() method)
Here is a class that represents a simple coffee cup in a virtual café:
// In Source Packet in ex1/CoffeeCup.java
class CoffeeCup {
private int innerCoffee;
public void add(int amount) {
innerCoffee += amount;
}
public int releaseOneSip(int sipSize) {
int sip = sipSize;
if (innerCoffee < sipSize) {
sip = innerCoffee;
}
innerCoffee -= sip;
return sip;
}
public int spillEntireContents() {
int all = innerCoffee;
innerCoffee = 0;
return all;
}
}
Before getting started with a discussion of design guidelines for object initialization, I'd like to clarify a few terms.
Designer vs. client programmers
If you're like most Java programmers, you alternate between two hats, which you wear at different times. Sometimes you wear
your "designer" hat and build libraries of classes for others to use; other times you wear your "client" hat and make use
of a library of classes created by someone else. Some Java programmers -- completely oblivious to the rules of fashion --
are known to wear both hats at the same time.
One aspect of the flexibility of a body of code is the ease with which a client programmer can understand the code. Whether a client programmer is planning to change code or just use it as is, that programmer often has to figure out how to change or use the code by reading it.
The guidelines discussed in the remainder of this article, and in subsequent articles of this column, will talk about flexibility in terms of client programmers. Designs and implementations that are flexible are those that are easy for client programmers to understand, use, and change.
Java jargon related to initialization
This article uses several terms related to initialization that are defined in precise ways by the Java Language Specification
(JLS).