Search |
||
Introduction To Naked Objects
Tue, 2003-07-15
Unzip! Unsnap! AHHHH!! Our business object is naked. It is time to strip applications of complex UIs and give users direct access to the business objects. The concept is simple: write behaviorally complete business model objects and use generic views and controllers. Thus, if a business model object supports a public behavior, then the user has access to that behavior. So why do we need naked objects?
Chances are you have struggled to truly understand the
Model-View-Controller (MVC) pattern. Why is this? The concepts are easy,
right? The Model contains the core business logic; the View is responsible
for displaying a given model's data; the Controller controls the interactions
between the model and the view, typically through event notification.
Of course, anyone who has worked with Swing knows that the View and Controller
are often combined in the same component. For example, a Our Simple Naked Objects ProjectWe are going to build a simple address book using the Naked Objects framework. An address book works nicely because it fits into the naked objects philosophy and, of course, it is easy to understand. You may download the example application here. Our first requirement is that our address book may contain zero or more people. Here is the first iteration of our naked Person:
The Naked Objects framework requires that all naked objects implement
the
Naked Objects Attributes
Our Person object now has a first name, last name, and birthdate. This is
plenty for now. You should have noticed that we did not use a
Naked Object MethodsEach mutable attribute requires that you provide a corresponding
"getter" method. The framework locates all "getters"
using reflection, and based on the
return type, builds the correct UI component. The first and last names are
simple text fields, and the birthdate is
a text field with date-parsing behavior.
Labels are generated automatically,
too, by stripping the "get" from each "getter" method
and putting a space between characters that differ in capitalization.
You may have noticed that
Our Naked Object Exposed
We did not code anything GUI-specific. We simply created a business object, and the Naked Objects framework, using reflection, created the Person view. Remember that if our object supports it, the user gets it. Diagram 2 shows a quick view of some of the built-in Naked Object types. For details on these types beyond what is presented in the examples in this article, please consult the Naked Objects documentation.
Unit Testing Naked ObjectsNow that we have seen how to create a simple naked object, let's turn our attention to unit testing. The Naked Objects framework provides a very slick way to unit test our objects. Here is the start of our test fixture:
This code should look very familiar if you have worked with JUnit.
The only exception is a Naked Object's test fixture extends
Testing the Object
Once again, if you are familiar with how to write tests, then this
test should be fairly straightforward. There are a few things to mention,
though, before we continue. First, remember that our person does not contain
any "setters". The Naked Objects framework only
requires "setters" if an instance of an object can be changed.
Let's remove the deep chaining to see what is going on.
Second, the Naked Object's
Testing the View
One of the most impressive and powerful features of the Naked Objects
framework is the use of Views. Views represent the "graphical"
equivalent of an object, but without the object visible on the screen. This
allows us to test our objects and their interactions without
special scripting or complex GUI-testing frameworks. Here is how we can
test our
Testing views requires understanding how the framework locates
objects (views) and their fields. The first few lines of this test retrieve the
person view. All views are retrieved using the plural
name. If you were to print out the plural name for our
To specify field names, you must break apart the
"getter" methods. For example, One Last Feature ...
Remember that every naked object must implement a method that returns
back a title. This method comes from the
And here is how we might implement it:
Titles show up at the top of each object's window (next to the icon). You can set a title to anything you like. Just make sure it is descriptive enough for a user to understand the object.
Adding AddressesSo far we have looked at a very simple naked object. The next step is to add one or more addresses to a person. Here is how:
The NakedObjects framework contains an
Here is the
There is nothing new in the
Adding an Address to a Person
If you want to see all of the attributes from the
To remove an address, simply right-click on the
Testing One-to-Many RelationshipsWe have seen how to write and test simple naked objects. We have also seen how to associate objects by dragging and dropping. Now let's look at how we can test this behavior. Here is a simple test:
Creating a Runnable ApplicationSo far we have seen how to create and test naked objects. Now let's delve into creating a Naked Objects application. Here is the code to launch our address book:
When developing a Naked Objects application, you should extend from
Here is a screen shot showing the classes available to user:
To create a new person, right-click on a class and select "New Person..."
Object StoresThe framework supports multiple types of object stores. Here is a list of object stores readily available:
To facilitate rapid prototyping, the framework defaults to a transient store.
As the application stabilizes, it is appropriate to plug in a persistent
object store, such as XML or a database. We can change the object store
by overriding the
The complexity of a Naked Objects application grows when dealing with more complex persistence capabilities. For more information on creating a robust, persistence-aware application, consult the Naked Objects documentation. Other GoodiesThe Naked Objects framework relies heavily on reflection, as we have seen throughout this article. Don't be scared, because there is plenty of documentation to get you started. You can start by reading the online book, which also contains great chapter on object-oriented programming. Listed below are a few treasures that you'll uncover from the online book. Changing the Plural NameBy default, the framework adds an "s" to the classname to create the plural name. Irregular nouns can be changed by implementing this method:
Here is how we change "Persons" to "People"
Ordering Objects
How objects are ordered is specific to how
Here is how we might order our objects:
Changing IconsIcons are simple to add to your application. Simply create an
images
directory that contains your images and place it on the
SummaryNaked objects provide an interesting way to think about developing software. In this article, we built and tested a simple application using the open source Naked Objects framework. Overall, this framework offers a substantial amount of flexibility and structure when developing GUI applications, and we barely scratched the surface with what this framework can do. One of the most powerful features of this framework is its use of views, which allow us to test our objects and their interactions much like an end user might use them. Finally, even if you never build a Naked Object system, hopefully you will start thinking in terms of writing behaviorally complete objects. »
Related Topics >>
Programming
Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|