Quote:
"Developing reliable software in C is akin to shaving your beard
with a chainsaw. It will (roughly) work and will require
continued patching."
The goal is to translate an important use case into working
code. First, we need to understand "important" then we need to
translate the concepts into specifications.
Important
----------------------------------------
Software development is a four-variable game:
- cost: what does it cost?
- time: how much time does it take?
- quality: how good is the code
- scope: how many features
Bottom line is "how much money do I get for delivering the
software with that feature and that quality standard". In our
course, the bottom line is similar: "how many points do I get for
presenting and turning in software with that feature and that
quality standard." You need to learn to choose and that's why the
projects are structured the way they are.
From concepts to specifications
----------------------------------------
Concepts describe what we have. Specifications describe what we
want from the code.
Usually we want
- classes and interfaces
- relationships among classes
- for each class and interface
- fields
- methods
- signature
- purpose statement
Example
----------------------------------------
Let's look at the "known customer places an order for several
products" story.
Example: "known customer places an order for several products"
customer calls ordering
ordering looks up customer
ordering opens order and links it with customer
ordering adds lines to the order for product/quantity pairs
orderline looks up cost of product and price policy for
customer and quantity
ordering finalizes order
Let's first add navigability arrows to the relevant portion of the
class diagram:
%
% +-------------+
% 1 | Customer | 1
+---------------------> | description |<----+
| % +-------------+ |
| % *^ |
| % | |
1| % 1| |
+----------+ * % 1 +----------+ 1 |
| customer | <- - - - - - -> | Ordering | ---+ |
+----------+ 1 % +----------+ | |
| % | 1 | |
===========|==== | *v v*
| | +--------+
+-----+ +------+ | Order |
| | +--------+
1v 1v |
+----------+ |
| Catalog | |1
+----------+ |
| |
| v*
| +-----------+
| | OrderLine |
| +-----------+
V |1
+---------+ |
| Product |<---------------+
+---------+ 1
capital names mean classes, lower-case is a concept
customer calls ordering: that's a physical action
Assume that ordering personnel then types in the name of the
customer into an open order worksheet and that an instance of the
Ordering class handles such a stimulus:
Ordering: constructor
Ordering()
Ordering looks up customer description:
the multiplicity constraint suggests that ordering has access to
a collection (enumeration, list, array, database) of customer
descriptions:
Ordering:
Customer lookupCustomer(String name)
Next Ordering creates an order for the customer
Order: constructor
Order(Customer c)
and it links the customer to the order
Customer:
void addOrder(Order o)
Now Ordering is ready to accept requests for specific products for
this order. For each request asks the order to add a line:
Ordering:
void addLine(Order o, Product p, Quantity q)
// note: non-physical classes show up
// the product link comes from an interaction with the catalog
Order:
void addLine(Product p, Quantity q)
// create an OrderLine and add it to the order
OrderLine: constructor
OrderLine(Product p, Quantity q)
// compute value of order line
Eventually Ordering accepts a request to close off an order. It
should at that point deliver the total value of the order.
Ordering:
int close(Order o)
// add order to ordering database
// inform shipping and billing of the order
// compute value of the order in cents
Order:
int value()
// compute the value of the order
OrderLine:
int value()
// lookup the value of the orderline
Based on this analysis of the use case, we enrich the diagram with
fields and method specs.
+---------------------------+ +-----------------------+
| Ordering | | Order(Customer c) |
+---------------------------+ +-----------------------+
| | * 1 | OrderLine lines[] | *
| Order open[] | ----------> | Customer c | -+
| Customer customer_db[] | | | | |
+---------------------------+ | +-----------------------+ |
| Customer lookupCustomer() | | | int value() | |
| Order openOrder(Customer) | | | void addLine | |
| void addLine | | | (Order o, | |
| (Order o, | | | Product p, | |
| Product p, | | | Quantity q) | |
| Quantity q) | | +-----------------------+ |
| int closeOrder(Order o) | | |
+---------------------------+ | +-----------------------+
| |
+-------------------------+ v
| +-------------------------+
| | OrderLine |
| | (Product p, Quantity q)|
| +-------------------------+
| | Product p |
| | Quantity q |
| | int cost |
| +-------------------------+
| | private int value() |
| +-------------------------+
v
+---------------------------+
| Customer() |
+---------------------------+
| String name |
| String address |
| Order orders[] |
+---------------------------+
| void addOrder(Order o) |
+---------------------------+