Naked Objects
By Richard Pawson and Robert Matthews

The anatomy of a naked object

A Naked Object system is simply a collection of classes that are made available to the user. To be rendered visible to the user, an object must be of the type org.nakedobjects.object.Naked. This interface is defined within the Naked Objects framework along with two sub-interfaces. The first is org.nakedobjects.object.NakedValue, for value objects, which store simple data values (such as text, numbers, currency and dates) and are only used inside other objects. The framework provides a number of ready-made classes that implement this interface, several of which we shall be introducing in this section. Programmers can add new NakedValue classes if needed. The second sub-interface is org.nakedobjects.object.NakedObject. This is used for objects that need to be referenced in multiple contexts, which typically means your business objects.

The simplest way to ensure that your business objects conform to the NakedObject definition is to make them sub-classes of org.nakedobjects.object.AbstractNakedObject - a class provided by the framework. If you cannot do this, because your business object must inherit from some other hierarchy, then you will have to implement the methods required by NakedObject yourself.

AbstractNakedObject implements all of the methods in the NakedObject interface except for title, which we will discuss shortly. Thus it provides you with a foundation for building your business objects. We will use this class throughout our discussions.

The Naked class hierarchy

If a business object class is defined as a subclass of AbstractNakedObject, then you must also do the following in order for the framework to be able to access and manipulate these objects, and thereby make them available to users:

  • Declare the class as public.
  • Ensure the class has a zero-parameter constructor (this is often referred to as the default constructor).
  • Declare as public all of its methods that are to be made available to the framework.
  • Implement the abstract title method from the superclass so that it returns a non-null reference. The title method will help users to distinguish each object from the rest of the instances of a specific type - but it could also be used for other purposes, such as searching or report generation.