Rice University - Comp 212 - Intermediate Programming

Fall 2001

Lecture #20 - Restricted Access Containers (RACs) and the Strategy Pattern


Restricted Access Containers

  1. Restrict the users from seeing inside or working on the inside of the container.
  2. Have a simple store(data) and retrieve() methods. Note the lack of specification of how the data goes in or comes out of the container.
  3. However, a "policy" must exist that governs how data is stored/retrieved. Examples:
    1. First in/last out (FIFO)
    2. Last in/first out (LIFO)
    3. Retrieve by ranking
    4. (my favorite) Random retrieval
  4. RACs are sometimes called "priority queues".
  5. The policy is variant behavior --> abstract it.
    1. The behavior of the RAC is independent of exactly what the policy does.
    2. The RAC delegates the actual storing and retrieving work to the policy.
    3. The RAC is only dependent on the existence of the policy, not what it does.
    4. The policy is a "strategy" for storing and retrieving data from the RAC. See the Strategy design pattern.
    5. Strategy pattern vs. State pattern -- so alike, yet so different!
  6. RAC double dispatches through the policy for the store and retrieve methods. Those operations depend on the policy.
  7. Internal data container: (Note, the outside behavior does not depend on this -- implementation only! --- Click here for docs / Click here to download the code )
    1. Doubly linked Lisp-list-like circular list: All abstract nodes a have both next and previous links and have all the link management (get/insert/etc) methods plus abstract methods, notably the visitor pattern execute().
    2. Data nodes have data (amazing!).
    3. Instead of a regular null node, use a "stop node" that holds both the head and the tail of the list --> O(1) access for both head and tail with no accounting problems that come with trying to track a tail that moves with each insert/remove.
    4. Outside RAC context holds onto the stop node, not the abstract node.
    5. Store and retrieve are implemented using visitors.
    6. Need an init() method of the policy to condition the internal data structure for use when the policy is installed.
  8. Stacks and Queues are types of RAC with quasi-standard interfaces. Use adapters to translate to/from the standard interfaces to our implementation of a RAC.