Designing the View Class
In order to apply the view concept we need a view class. Before we think about the code it is worth stating the fundamental behavior that a view class should exhibit.
- The view contains links to the underlying data, not copies of it
- Only the data that qualifies for the view can be accessed via the view
- The view can reference multiple underlying data containers
The first behavior is crucial. It is this feature that provides view objects with their efficiency. The efficiency comes from the fact that a link to a piece of data is nearly always smaller and never larger than the data itself. So storing links uses no more and normally considerably less memory than storing copies of the data.
The second behavior means that the view does not grant any access to the data in the underlying container that is not contained within the view. This is very important and means that, for instance, a view showing the top five sales records by sale value will grant access only to those five records and not to any other part of the data.
The third behavior allows views to virtually merge data from multiple sources into one view. For instance, we should be able to create a view that represents data from two vector objects or from two map objects. This is a very powerful behavior indeed and can be used to construct sophisticated views.
Now we are ready to start coding. We'll write the view class by coding each one of the three behaviors one-by-one.
Note that the techniques applied and the code written in the article are both focused on using the STL.