Rice University - Comp 212 - Intermediate Programming
Fall 2001
Lecture #20 - Restricted Access Containers (RACs) and the Strategy
Pattern
Restricted Access Containers
- Restrict the users from seeing inside or working on the inside of the container.
- 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.
- However, a "policy" must exist that governs how data is stored/retrieved.
Examples:
- First in/last out (FIFO)
- Last in/first out (LIFO)
- Retrieve by ranking
- (my favorite) Random retrieval
- RACs are sometimes called "priority queues".
- The policy is variant behavior --> abstract it.
- The behavior of the RAC is independent of exactly what the policy does.
- The RAC delegates the actual storing and retrieving work to the policy.
- The RAC is only dependent on the existence of the policy, not
what it does.
- The policy is a "strategy" for storing and retrieving data
from the RAC. See
the Strategy design pattern.
- Strategy pattern vs. State
pattern -- so alike, yet so different!
- RAC double dispatches through the policy for the store and retrieve methods.
Those operations depend on the policy.
- Internal data container: (Note, the outside behavior does not depend on
this -- implementation only! --- Click
here for docs / Click here to download the code
)
- 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().
- Data nodes have data (amazing!).
- 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.
- Outside RAC context holds onto the stop node, not the abstract node.
- Store and retrieve are implemented using visitors.
- Need an init() method of the policy to condition the internal data structure
for use when the policy is installed.
- 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.