Pattern: View Factory

Also Known As

Abstract Constructor [Lan96]

Abstract

A View Factory provides a uniform interface to creating Hierarchical Views. You present a key to the factory and get a view in return.

Context

You are using an object-oriented language for the implementation of the Relational Database Access Layer Framework. You want to implement a single factory for all objects derived from the View abstract class. This ViewFactory should work together with the View Cache.

Problem

How do you implement a single factory for all View subclasses that determines the right subtype from the given database key?

Forces

Solution

Implement an Abstract Factory [GOF95] using an Abstract Constructor [Lan96] and shield it with an interface that distinguishes between new objects and objects that are activated from the database.

Structure

The ViewFactory is responsible for creating new Views and for activating existing ones. It works together with the ViewCache as explained in the View Cache pattern. The basic interface needed here is given by the following class declaration:

class ViewFactory {

public:

       static View * createView ( ViewKey aKey );

       static View * getView (ViewKey aKey);

};

Implementation

When implementing a View Factory you may use the following patterns. The Abstract Constructor pattern [Lan96] contains a detailed description of how to implement a ViewFactory. Usually Prototypes [GOF95] are used to trick statically typed languages. As stated above in the View Cache pattern, pointers returned by the getView() method can be made safer using Smart Pointers [Mey96] or the Counted Pointers Idiom [Cop92].

Related Patterns

The Abstract Interface [Col96] is a special form of an Abstract Constructor [Lan96] or Abstract Factory [GOF95] used for decoupling purposes. Much of the deeper discussion about this pattern may be found there.

Known Uses

sd&m’s HYPO project contains a View Factory [Kel+96a].

The equivalent of a View Factory for non-object-oriented-languages is some form of memory allocation for Views. This is especially tricky for transaction systems like CICS or IMS/TM. Refer e.g. to sd&m’s Thyssen project and various other host projects if you are looking for more details.



[1]    Runtime Type Information