PriDE Design Patterns: Proxies


Another functionality from the feature list, not being covered directly by PriDE are so-called proxies or thin objects. Proxies are reduced representations of records which only allow a human and programmatic identification. A typical application is e.g. the selection of an element from a combobox, containing a quasi-unique string representation for the user and an actually unique key information to reconstruct the complete record from the database.
A simple way to implement this concept is the encapsulation of proxy attributes in a separate base class, similar to the proposed implementation for  object IDs. Suppose a little bit more realistic entity class for the representation of customer records:
 
class Customer extends MappedObject {
    protected static RecordDescriptor red = new RecordDescriptor
        (Customer.class, "customer", null, new String[][] {
            { "id",        "getId",        "setId" },
            { "name",      "getName",      "setName" },
            { "surname",   "getSurname",   "setSurname" },
            { "phone",     "getPhone",     "setPhone" },
            { "fax",       "setFax",       "getFax" },
            { "lastOrder", "getLastOrder", "setLastOrder" },
            { "solvency",  "getSolvency",  "setSolvency" },
            { "credit",    "getCredit",    "setCredit" }
        });

    private int id;
    private String name;
    private String surname;
    private String phone;
    private String fax;
    private Date lastOrder;
    private int solvency;
    private int credit;

    // to be continued
}

Without discussing the particular details of the attributes above, it can be assumed that the ID makes up a unique customer number while the combination of name and surname are a reasonable identification criterion for humans. These three attributes might therefore be extracted as a proxy class, leading to a definition like this:
 
class CustomerProxy extends MappedObject {
    protected static RecordDescriptor red = new RecordDescriptor
        (CustomerProxy.class, "customer", null, new String[][] {
            { "id",        "getId",        "setId" },
            { "name",      "getName",      "setName" },
            { "surname",   "getSurname",   "setSurname" },

    private int id;
    private String name;
    private String surname;

    /** Returns a string representation of the form
      * "Lessner, Jan (22735004)"
      */
    public String toString() {
        return name + ", " + surname + " (" + id + ")";
    }

    // to be continued
}

class Customer extends CustomerProxy {
    protected static RecordDescriptor red = new RecordDescriptor
        (Customer.class, "customer", CustomerProxy.red, new String[][] {
            { "phone",     "getPhone",     "setPhone" },
            { "fax",       "setFax",       "getFax" },
            { "lastOrder", "getLastOrder", "setLastOrder" },
            { "solvency",  "getSolvency",  "setSolvency" },
            { "credit",    "getCredit",    "setCredit" }
        });

    private String phone;
    private String fax;
    private Date lastOrder;
    private int solvency;
    private int credit;

    // to be continued
}

The proxy class above can now be used as a reduced representation of Customer objects. CustomerProxy ist not an abstract class and its RecordDescriptor contains all information to access the database table customer. This allows to get CustomerProxy objects directly from a database query as demonstrated in the following example:
 
CustomerProxy proxy = new CustomerProxy("L%");
ResultIterator iter = proxy.wildcard(new String[] { "name" });
CustomerProxy[] results = (CustomerProxy[])iter.toArray();

Of course database access through a proxy class is limited to read, partial update and delete operations. Record creation will usually not work, because all fields in the customer table which are not known to the CustomerProxy class will be left NULL.

The approach above just gives a rough impression how easy proxy objects may be defined. As customers are in fact not a specialization of customer proxies, it is conceptually douptful to derive one from the other. However, also in a proper design, a proxy definition should be as simple as the one above. The complete source code for the examples can be found in examples/proxy.


Home Introduction Javadoc