Motivation:
The pattern Unified Access describes a type safe access (reading and writing) to values of a map object (object that maps keys to values) for a such case where values have got different data types.Structure (class model):
The model contains two classes:- MyMap - a simple map that maps keys to values
- Property - a representative of a map key
Implementation in Java language
The implementation requires a generic type language support that's why the Java SE 5.0 or more must be used.A MyMap class implements a simple map behavior and it contains two keys NAME and AGE.
import java.util.*; public class MyMap { public static final Property<String> NAME = new Property<String>(); public static final Property<Integer> AGE = new Property<Integer>(); private Map<Property, Object> data = new HashMap<Property, Object>();All keys are an instance of a Property class, however each of them will provide a value of a different type.
protected Object readValue(Property key) { return data.get(key); } protected void writeValue(Property key, Object value) { data.put(key, value); } }
If both classes (MyMap and Property) will be located in the same package, then it is possible to retain their visibility to protected so that no strange object can to use them.
public class Property<VALUE> {This pattern allows a value access by a methods of key only. It is possible to verify the method setValue accepts declared value type to write into map object and then method getValue returns the same data type only.
public void setValue(MyMap map, VALUE value) { map.writeValue(this, value); } public VALUE getValue(MyMap map) { return (VALUE) map.readValue(this); } }
MyMap map = new MyMap();
MyMap.NAME.setValue(map, "Peter Prokop"); MyMap.AGE .setValue(map, 22); String name = MyMap.NAME.getValue(map); int age = MyMap.AGE.getValue(map); System.out.println(name + " is " + age);
Usage:
The pattern design can be used in place of conventional POJO object. It's meaningful in case there is to need to refer a method (set/get), no its value. Here are three samples of usage in a Java language- The sort method takes a list of items and its properties (e.g. NAME, AGE) for the ordering. A GenericSorter implementation builds an appropriate comparator oneself .
interface GenericSorter { public void sort(List<MyMap> items, Property ... properties); }
- a child of a TableModel interface, which can to take a list of columns by an argument of a method
interface GenericTableModel<MyMap> extends TableModel { public void setColumns(Property ... columns); }
- There is obvious, that all values are saved by a single method writeProperty() only.
In case the MyMap class becomes a parrent of more classes (they are written in the same pattern desing),
so the method writeProperty() will become a central writing point for all hierarchy of classes.
The point is a true place for inserting common validations, conversions and listeners.
Next method readProperty() replaces all NULL values by a value for all Integer types. In the example a class Property returns a value type by a new method getType().protected Object readValue(Property key) { Object result = super.readValue(key);
if (result==null && key.getType()==Integer.class ){ return 0; } else { return result; } }
Both projects are available include a source code on SourceForge.net under an open license since 2007-10-24.
The pattern design was slightly extended; a final solution is described by two interfaces
About Author:
- You can write questions or other messages to me by a blog http://ujoframework.blogspot.com/.
- There exists some more free applications on an authors home page http://pponec.net/ .
- A contact e-mail: ujoframework@gmail.com
PPone(c) 2007