Note - Package Structure Matters!

A package is a small piece of code that obeys the Single Responsibility Principle (SRP): it is focused on a single activity. In order to test and extend packages, designers need to understand their operations, and so spend a lot of time reading their text. To support this study, we will structure our packages in the following way:
  1. C++ packages consist of two source code files

    A source code header file with extension ".h" and implementation file with extension ".cpp". Both files have the same name, exclusive of their extensions.
    The header file contains the declaration of one or more classes (usually one) and may have global function declarations. The implementation file defines all classes and global functions
    There are a couple of exception to this structure. Template classes must have their definitions in the header file, and the header may also contain non-template functions qualified with the key work "inline". Also, a class is allowed to implement one or more of its methods inline in the class declaration, as in C# and Java. We usually don't do that.
  2. Manual Information:

    Each package file has a block of comments at the top we refer to as manual information. It contains a prologue briefly summarizing package operation, operating environment, and identifying the author. After the prologue comes manual information details that describe, in a paragraph or two, package operations. This may be followed by a description of the package's public interface.
  3. Maintenance Information

    Manual information is followed by a block of comments called maintenance information. This contains package revision history and instructions for building the package, if needed.
  4. List of public services the package uses

    A set of using statements for all of the libraries on which the package depends.
  5. One or more namespace and class declarations

    These implement each of the package's functions inline. Each function is preceeded by a single line comment that describes the function's responsibility, usually in four or five words.
  6. Test stub:

    Finally, we append a main function that serves as a construction test for the package. This function is bracketed with compilation constant statements that allow us to choose whether to compile the test or not. We compile during construction testing, but disable that when the package is used as a part of a larger subsystem.
Package_Name
File: Package_Name.h
#ifndef PACKAGE_NAME
#define PACKAGE_NAME
Manual Page goes here
Maintenance Page goes here
Declarations with inline implementations go here
#endif
File: Package_Name.cpp
#include "package_name.h"
Class method and global function definitions go here
#ifdef TEST_PACKAGE_NAME
int main(int argc, char* argv[])
{
  // tests go here
}
#endif
C++ Package Structure

Example:

The BlockingQueue is an example of a well structured package. For now, we are only interested in its package structure. Later, when we discuss threading, we will use this package in many examples and demonstration codes.

Program Structure:

We often represent program structure using package diagrams, like the one shown below. Many simple programs have this set of packages, perhaps with different names.
Simple Program Packages

When we discuss a program's packages, we are interested in the dependency relationships that result from calls from one package into another. And we are interested in the responsibilities of each package.

In this simple program an executive directs program activities by making calls into lower level packages. A simple program may have an "InputCollector" package devoted to collecting the set of things the program will analyze. It has an "Analyzer" package that processes its inputs to generate data to display. It is the responsiblity of the "Display" package to transform the computed data into information for users of the program.

As program activities get progressively more complex, we partition the processing of some, or all, of the packages below the executive into a director package and lower level packages that carry out specific parts of the director's responsibilities.

In succeeding lectures we will be discussing some of these more complex structures, which may look quite different from that shown here.

Conclusions for: Package Structure is Important!

A published package contains:
  1. Prologue, manual information and maintenance information, which are comments within the code body.
  2. Code for service definitions and test stub. The test stub is a main function enclosed in a compiler directive used for switching the test stub main in or out of the compiled code block, depending on whether it will be used with other packages.
  3. Each of the member functions is preceeded by a single comment line that describes its responsibility in a few words.
All of the reusable modules provided for your use in our website follow this format, as your submitted code must, as well. Demonstration code, intended to illustrate some idea, but not intended for reuse may follow a simpler format.