Naked Objects
By Richard Pawson and Robert Matthews

Appendix C: Cliché code

Developing software by cutting and pasting code from other programs incurs some risk, but when you are learning about a system it is a pragmatic, and often effective, approach. We provide here some examples of 'cliché code' often used in in Naked Objects programs.

Exploration class

To explore a set of classes that you have developed, create an extension of Exploration and add your classes to the ClassSet in the classSet method.

import org.nakedobjects.Exploration;
import org.nakedobjects.object.NakedClassList;

public class MyExploration extends Exploration {
    public void classSet(NakedClassList classes) {
        /* Add to the list all classes that are to made available to
         * to the user in the classes window */
        classes.addClass(NakedObjectClass1.class);
        classes.addClass(NakedObjectClass2.class);
        classes.addClass(NakedObjectClass3.class);
        :
    }

    public void initObjects() {
        NakedObjectClass newObject; 
        /* create instances via the class and initialize them */
        newObject = (NakedObjectClass) createInstance(NakedObjectClass.class);
        newObject.getValueObject().setValue(value);
        newObject.setAssociation(object);
     }

    public static void main(String[] args) {
        new MyExploration();
    }
Naked Object

The following template shows the declaration of a business object. Only the zero-parameter constructor and the title method is required.

import org.nakedobject.object.NakedObject;
import org.nakedobject.object.Title;
import org.nakedobject.object.value.ValueType;
:
:
public class ClassName extends AbstractNakedObject {

    /* Makes the class uninstantiable: no new objects can be created */
    public static About aboutClassName() {
        return ClassAbout.UNINSTANTIABLE;
    }
    
    /* Gives the class a name */
     public static String singularName() {
        return "Singular Name";
    }

    /* Gives the class a plural name */
    public static String pluralName() {
        return "Plural Name";
    }

    public Object() {
         /* Set a value's about so the value is uneditable */
        value.setAbout(FieldAbout.READ_ONLY);
    }

    /* This method is only called when the logical object is created.
     * The constructor will be called every time the object is
     * recreated (by the persistence mechanism). */
    public void created() {}

    /* Titles can be generated directly from both value and one-to-one
     * association objects, but not from one-to-many associations.  See
     * below for details of the Title class. */
    public Title title() {
        return value/object.title();
    }

    /* Makes the object read-only */
    public About about() {
        return ObjectAbout.READ_ONLY;
    }

    /* Value fields: TextString, Date, Time, DateTime, WholeNumber,
     * FloatingPointNumber, Percentage, Money, Option, URLString and     Label. 
     * Should be marked as final and not have a set... method. */
    private final ValueType value = new ValueType();

    public ValueType getVariable() {
        return value;
    }

     /* Association fields: any other class based on AbstractNakedObject.*/
    private ClassName object;

    public ClassName getObject() {
        resolve(object);
        return object;
    }

    public void setObject(ClassName object) {
        this.object = object;
        objectChanged();
    }

    /* The associate... and dissociate... method can override 
     * the set... method and, hence, used to set up associations. */
    public void associateObject(ClassName object) {
        setObject(object);
    }

    public void dissociateObject(ClassName object) {
        setObject(null);
    }

    /* The About object returned by    this method field determines
     * if the field can be changed. */
    public About aboutObject(ClassName object) {
     return FieldAbout.READ_ONLY; 
    }

   /* One-to-many association.  The collection object must be instantiated 
     * with the element type and a reference to its parent, the owner.  
     * This should be marked as final and only a set... method provided. */
    private final InternalCollection objects = new InternalCollection(ClassName.class, this);

    public InternalCollection getObjects() {
        return objects;
    }

    /* The associate... and dissociate... method
     * can override the get... method and, hence, are used
     * to set up associations.  The collection's add and
     * remove methods are used to change the collection. */
    public void associateObjects((Object object) {
        objects.add(object);
    }

    public void dissociateObjects((Object object) {
        objects.remove(null);
    }

    /* The About object returned by this method determines if
     * objects can be added and removed from this field. */
    public About aboutObjects() {
     return FieldAbout.READ_ONLY; 
    }

    /* action methods */

    /* Zero-parameter methods are show in the object's pop-up menu.  If the
     * method returns an object then this will be shown.*/
    public void/Object actionOptionName() {
        :
        return object;
    }

    /* The about method determines whether the option will disabled or not. */
    public About aboutActionOptionName() {
         return ActionAbout.enable();
    }

    /* One-parameter methods are made available through drag and drop.  
     * If the method returns an object then this will be shown. */
    public void/Object actionOptionName(Object object) {
        :
        return object;
    }

     /* The about method determines whether the option will disabled or not. */
    public About aboutActionOptionName(Object object)(Object object) {
         return ActionAbout.enable();
    }
}
Title

There are three basic ways to create Title objects.


value.title()
association.title()
new Title("text")

Titles can be appended to, which adds necessary spacing, or have details concatenated.


value.title().append("text");
association.title().append(value)
new Title("text").append(association)

value.title().concat("text");
association.title().concat(value)
new Title("text").concat(association)