16.2. Memento Pattern

[ fromfile: memento.xml id: memento ]

Abstract

This section combines QMetaObjects with the SAX2 parser to show how to write a general-purpose XML encoding/decoding tool that works on QObjects with well-defined Q_PROPERTYs and children. This gives us an example that combines the MetaObject pattern with the Memento pattern. And because XML and QObjects can both represent hierarchical structures, these ideas are combined with the Composite and Abstract Factory patterns, to save and load entire trees of polymorphic objects.

Figure 16.2.  CustomerList UML

CustomerList UML

Example 16.10. src/xml/propchildren/customerlist.xml

<object class="CustomerList" name="Customers" >

   <object class="Customer" name="Simon" >
     <property name="Name" type="QString" value="Simon" />
     <property name="Date" type="QDate" value="1963-11-22" />
     <property name="LuckyNumber" type="int" value="834" />
     <property name="State" type="QString" value="WA" />
     <property name="Zip" type="QString" value="12345" />
     <property name="FavoriteFood" type="QString" value="Donuts" />
     <property name="FavoriteDrink" type="QString" value="YooHoo"/>
   </object>


   <object class="Customer" name="Raja" >
     <property name="Name" type="QString" value="Raja" />
     <property name="Date" type="QDate" value="1969-06-15" />
     <property name="LuckyNumber" type="int" value="62" />
     <property name="State" type="QString" value="AZ" />
     <property name="Zip" type="QString" value="54321" />
     <property name="FavoriteFood" type="QString" value="Mushrooms" />
     <property name="FavoriteDrink" type="QString" value="Jolt" />
   </object>

</object>