Encapsulation and Abstraction

Encapsulation and abstraction are two program design techniques often used to manage the complexity of large software systems. Encapulation is the technique of consolidating related code and data fragments into atomic units of program construction. The GenVoca model has demonstrated that the scales of encapsulation currently provided by programming languages, namely functions and classes, are inappropriate for building large systems. Instead, GenVoca advocates the use of components, which correspond to subsystems or modules. In P++, a component consists of a suite of interrelated data members, functions, and classes.

Abstraction is a design technique which separates the interface of a component from its implementation. In P++, the realm construct specifies the interface of a component: it declares the functions and classes which comprise a component's interface, without revealing the implementation of those functions and classes.

Figure: Sample realm and component declarations.
realm collection<class T>
{
  class container
  {
    container ();               // container
  };
  class cursor
  {
    cursor (container *);       // constructor
    void first ();              // container traversal
    void next ();               // container traversal
    int eoc ();	                // beyond end of container?
    void insert (T);            // add item
    void remove ();             // delete item
    T& get_value ();            // get item
  };
};

component linked_list : collection<class T>
{
  class element
  {
    element (T);                // constructor
    T data;                     // the value of this node
    element *next, *prev;       // adjacent nodes on list
  };
  class container
  {
    element *head;              // first node of list
  };
  class cursor
  {
    element *current_pos;       // current position
    container *cont;            // container iterated upon
  };
};

Figure [*] lists an example from the domain of data structures. First shown is the declaration of the collection realm: this is a standardized abstract interface for interacting with data structures that store collections of objects. collection declares two classes, container (which actually stores the objects) and cursor (which provides methods for manipulating objects inside a container). Notice that the realm reveals no implementation details about its classes, thus maintaining the proper separation between a component's abstract interface and concrete implementation.

The next declaration in Figure [*] is that of linked_list, a component from the collection realm. Because linked_list belongs to collection, it is obligated to implement (export) the methods listed in collection's declaration. The body of linked_list's declaration introduces a new class called element, along with new data members for the container and cursor classes. This class and these data members are part of linked_list's private implementation; they are not visible to other components or programs.

A fundamental feature of component encapsulation is evident in this example: a component consists of several data members, functions, and/or classes that are intimately intertwined. Because of their interrelated algorithms, the element, cursor, and container classes cannot be designed, implemented, or reused in isolation. P++ provides the programming language support to maintain their cohesion as an atomic unit of system construction.