Naked Objects
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 classTo explore a set of classes that you have developed, create an extension
of 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 ObjectThe following template shows the declaration of a business object.
Only the zero-parameter constructor and the 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 TitleThere are three basic ways to create 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)
|
|
Copyright (c) 2002 nakedobjects.org You may print this document for your own personal use, or you may copy it in electronic form for access within your organisation, provided this notice is preserved. |