Naked Objects
Appendix A: Getting startedWriting an Application | |
To understand the principles of coding Naked Object applications we will now look at a very simple, but complete, application. The requirement is for a system that allows us to quickly organize employees into teams so they can work on specific projects. When a project is set up, distinct skills are identified, and individuals are assigned to the project based on those requirements. This will require three classes whose objects will represent the projects, the employees and the roles (a role being a combined set of skills). These classes will need to relate to each other: a project identifying its required roles and each role identifying the individual who is to join the team. The classes we will define for this application, therefore, are:
We start by defining the classes for the three types of
object. For each class we need to create a .java file and
within it declare the class as an extension to the
Start the three classes as follows:- Project.java import org.nakedobjects.object.AbstractNakedObject; public class Project extends AbstractNakedObject { } Role.java import org.nakedobjects.object.AbstractNakedObject; public class Role extends AbstractNakedObject { } Employee.java import org.nakedobjects.object.AbstractNakedObject; public class Employee extends AbstractNakedObject { } Now we consider what fields each
object should have. First we add fields that allow
the user to identify the different objects (e.g. so we can see
that one object represents Dave and the other represents John). Next we
add fields that describe any relationships between the objects (e.g.
every role is performed by an employee so a Employee ObjectsTaking the simplest object first, an Employee.java import org.nakedobjects.object.AbstractNakedObject; import org.nakedobjects.object.value.TextString; public class Employee extends AbstractNakedObject { private final TextString name = new TextString(); public TextString getName() { return name; } } Role ObjectsNext, we do a similar thing for the Inside these two methods you will see two
necessary calls that keep the object current in the object store (our
link to a persistence mechanism) and in any other views of the same
object (on both the local and any remote clients). In the
Within Role.java import org.nakedobjects.object.AbstractNakedObject; import org.nakedobjects.object.value.TextString; public class Role extends AbstractNakedObject { private final TextString name = new TextString(); private Employee employee; public TextString getName() { return name; } public Employee getEmployee() { resolve(employee); return employee; } public void setEmployee(Employee employee) { this.employee = employee; objectChanged(); } } Project Objects
As for the two previous classes the Project.java import java.util.Enumeration; import org.nakedobjects.object.AbstractNakedObject; import org.nakedobjects.object.collection.InternalCollection; import org.nakedobjects.object.control.About; import org.nakedobjects.object.control.ActionAbout; import org.nakedobjects.object.value.Case; import org.nakedobjects.object.value.TextString; public class Project extends AbstractNakedObject { private final TextString name = new TextString(); private final InternalCollection roles = new InternalCollection(Role.class, this); public TextString getName() { return name; } public InternalCollection getRoles() { return roles; } } In addition to the two fields that this object offers we will add the
following method, which will be made available to the user from the
object's pop-up menu. This method is simple: it creates a new
public Role actionAddProjectLeader() { Role projectLeader = (Role)createInstance(Role.class); projectLeader.getName().setValue("Project Leader"); roles.add(projectLeader); return projectLeader; } The above method, however, is only suitable when no
project leader has been assigned already (as will be the case before the
method is first invoked). Methods like this can be controlled by a
corresponding The method below is matched to the
public About aboutActionAddProjectLeader(){ Enumeration e = getRoles().elements(); while(e.hasMoreElements()){ Role role = (Role)e.nextElement(); if(role.getName().contains("project leader", Case.INSENSITIVE)){ return ActionAbout.DISABLE; } } return ActionAbout.ENABLE; } TitlesBefore we can compile and run the application we have to implement
the public Title title() { return name.title(); } The import org.nakedobjects.object.Title;
|
|
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. |