Patterns » History » Version 1

J. Moringen, 02/05/2011 11:01 PM
initial version

1 1 J. Moringen
h1. Patterns
2 1 J. Moringen
3 1 J. Moringen
The @rsc::patterns@ namespace currently includes the following pattern implementations:
4 1 J. Moringen
5 1 J. Moringen
* @Singleton@ in file source:trunk/rsc/src/rsc/patterns/Singleton.h
6 1 J. Moringen
* @Factory@ in file source:trunk/rsc/src/rsc/patterns/Factory.h
7 1 J. Moringen
** @SingletonFactory@
8 1 J. Moringen
** @ObservableFactory@ in file source:trunk/rsc/src/rsc/patterns/ObservableFactory.h
9 1 J. Moringen
*** @ObservableSingletonFactory@
10 1 J. Moringen
* @ContainerProxy@ in file source:trunk/rsc/src/rsc/patterns/ContainerProxy.h
11 1 J. Moringen
** @AssociativeProxy@ in file source:trunk/rsc/src/rsc/patterns/AssociativeProxy.h
12 1 J. Moringen
13 1 J. Moringen
h2. Singleton
14 1 J. Moringen
15 1 J. Moringen
The @Singleton@ class can be used to ensure that at most one object of a given class exists at any time. It also encapsulates the creation and destruction of that object. Example:
16 1 J. Moringen
<pre>
17 1 J. Moringen
template<typename Key, typename Interface>
18 1 J. Moringen
class SingletonFactory: public Singleton<SingletonFactory<Key, Interface> > ,
19 1 J. Moringen
                        public Factory<Key, Interface> {
20 1 J. Moringen
  friend class Singleton<SingletonFactory<Key, Interface> > ;
21 1 J. Moringen
private:
22 1 J. Moringen
  SingletonFactory();
23 1 J. Moringen
};
24 1 J. Moringen
</pre>
25 1 J. Moringen
and
26 1 J. Moringen
<pre>
27 1 J. Moringen
SingletonFactory& factory = SingletonFactory::getInstance();
28 1 J. Moringen
</pre>
29 1 J. Moringen
30 1 J. Moringen
h2. Factory
31 1 J. Moringen
32 1 J. Moringen
The @*Factory@ family of classes implements different variations of the "factory pattern":http://en.wikipedia.org/wiki/Factory_pattern. In all cases, keys of some type are used to identify implementations of a common interface that is specified as a template argument of the factory class. Example:
33 1 J. Moringen
<pre>
34 1 J. Moringen
typedef rsc::patterns::SingletonFactory<std::string, my_interface> my_factory;
35 1 J. Moringen
</pre>
36 1 J. Moringen
37 1 J. Moringen
h2. ContainerProxy
38 1 J. Moringen
39 1 J. Moringen
The @ContainerProxy@ classes can be considered variants of the proxy pattern. The can be used to expose a modified interface to a container. For example, elements can be transformed on-the-fly when they are accessed.