Naked Objects A method that does not have to edit user interface. Advance the user interface. This method is very interesting. Turn the object directly to the user. There is a very detailed information on www.nakedobjects.org. You can take a look. Here is an article on the introduction of Naked Objects. Sharing with everyone. Naked Objects Toolbox Introduction
Here we will build a simple address this program using the Naked Objects toolbox.
Naked Objects Toolbox requires all Naked objects to implement NakeDObjects interfaces. For the sake of simplicity, we inherit the AbstractnakeDObject. AbstractnakeDObject class provides the basic features that make up a NakeD object, allowing us to pay attention to the transaction problem. All Objects users who are "naked" can be seen. Now we need to add some properties to the Person class and provide some ways to modify the properties. We add some properties: first name, last name, birthdate.
Imports.Object.abstractnakedObject;
Import org.nakedObjects.Object.value.date;
Imports.Object.value.TextString;
Public class persot extends AbstractnakeDObject {
PRIVATE FINAL TEXTSTRING FIRSTNAME
PRIVATE FINAL TEXTSTRING LASTNAME
PRIVATE FINAL DATE BIRTHDATE
Public persons () {
Firstname = new text4tring ();
Lastname = new text4tring ();
BirthDate = new date ();
}
Public final textstring getFirstname () {
Return firstname;
}
Public final textstring getlastname () {
Return Lastname;
}
Public final date getBIRTHDATE () {
Return birthdate;
}
Public title title () {
Return NULL;
}
}
Naked Objects attribute
We must pay attention to we do not use "java.lang.string or stringbuffer". In order to use the org.nake.textstrings.naked Objects toolbox to use TextString to operate a text domain and tell the frame to create a displayed text box. You can also mark the attribute that does not change to Final.
Naked Objects method
Each change property requires you to provide a corresponding "getter" method. The framework uses the image to find all "getter", construct the corresponding UI component according to different return types. The label is automatically generated, from each getter method to get Get, plus spaces in front of the uppercase letter. You may notice "Person" does not contain the "setter" method. The reason is very simple: TextStrings is variable, so we have no reason to change this example. When you need "setter", you can only wear a pair of shoes at one time. Wear tennis shoes a day, the next day sandals. In this example, "Setter" needs to change a person's shoes. In addition to "getter" and "setter", there are still many ways to find.
Our naked class
We did not write any gui. We just created a business model, the NakeD Objects framework used the image and created a view. Remember what if our object supports, what users get. unit test
We have seen how to construct the Naked class, let's take a look at the unit test. The Naked Objects framework provides a very flexible way for unit testing.
Imports.Object.nakedClass;
Import org.nakedObjects.testing.view;
Imports.testing.nakedtests.testing.nakedTestCase;
Import java.util.calendar;
Public Class TestPerson Extends Nakedtestcase {
/ **
* YES, You Must Supply a Constructor. Hopefully The next Version of The Next Version of The Next Version of Thae
* Naked Objects Framework Will Use Junit
3.8.1
.
* /
Public TestPerson (String Name) {
Super (Name);
}
Protected void setup () throws exception {
// Initialize An Object Store, OtherWise A Null Pointer Exception
// is thrown when Trying to create a new view instance.
INIT ();
RegisterClass (Person.class);
}
}
If you have used JUnit, these code should be very familiar. The only difference is Naked Objects' test fixture inherits to NakedTestCase. This is the base class inherited from junit.framework.testcase. Provide some convenient methods to register objects and build Object Store.
test
Public void testpersonattributes () {
Person Person = New Person ();
Person.getfirstname (). setValue ("brian");
Person.getlastName (). SetValue ("Coyner");
// Note That The Naked Object Date Starts with 1 (1 = Jan, 12 = DEC).
// this is different tour java.util.calendar.
Person.getBIRTHDATE (). SETVALUE (1900, 9, 22);
Assertequals ("First Name.", "Brian", Person.getFirstName (). stringValue ());
Assertequals ("Last Name.", "Coyner", Person.getLastName (). stringValue ());
Calendar Calendar = Calendar.getInstance ();
Calendar.Set (1900, Calendar.september, 22, 0, 0, 0);
Calendar.Set (Calendar.Millisecond, 0);
Assertequals ("Birthdate.", Calendar.gettime (), Person.getBIRTHDATE (). DateValue ());
}
If you are familiar with the writing test, then this test should be very simple. Here, there are some things that need to be explained.
1. Remember that our Person does not contain any "setter". The Naked Objects framework only needs "setter" when an instance of an object can be changed. TextString is variable, we only need to retrieve the TEXTString to change its value. Here and some of our previous APIs are somewhat different. We only need to get a reference to saving the future textString object, and then modify the value. Person.getfirstname (). setValue ("brian");
2, Naked Objects's Date starts with 1 month. Java.util.calender starts from 0.
Test view
Naked Objects Frame is the most powerful and most attractive characteristic is the usage of views. The view represents graphicalization of an object. . . . . . .
The last feature (talk about the setting of Title)
slightly
Add address
Add one or several addresses to Person
Import org.nakedObjects.Object.collection.internalcollection;
Public class persot extends AbstractnakeDObject {
.
PRIVATE INTERCOLLECTION Addresses
Public persons () {
.
Addresses = new intenalcollection (address.class, this);
}
Public interfacecollection getaddresses () {
Resolve (addresses);
Return Addresses;
}
.
}
The NakeDObjects framework includes an internalCollection object to store objects of the specified type. In our example is to store the Address object. We must also provide a collection of a parent class (owner) to create a one-to-many relationship. "Getter" method introduces a new method: resolve (namedObject). The resolve method is a static method of AbstractNakeDObject to ensure that the object exists, fully formed, and exists in memory before use. This indicates that we can construct a Person, but load Addresses when you call getAddresses for the first time later.
Add an address to Person
1. Create a new Address object
2. Drag and drop Address object to the Person's Addresses domain
You can see this domain, indicating that this object is correct fill. The frame will be careful. If an object can be put down. This domain is redistributed to not be filled.
Test a couple of relationships
slightly
Create a program that can run
Imports.Exploration;
Imports.Object.nakedcoS.Object.nakedClassList;
Import ORG.NAKEDOBJECTS.Object.nakedObjectStore;
Imports.Object.TransientObjectStore;
Public class addressbookapp extends expends {
Public void classset (nakedclasslist classes) {
Classes.addclass (persu.class);
Classes.addclass (address.class);
}
Public static void main (String [] args) {
New addressbookapp ();
}
}
When developing a Naked Objects application, you should inherit to Exploration, which is a prototype template. Explo Day ations includes a lot of default configurations. One you must give the object that is displayed. It is done by implementing abstract method classset (nakedclasslist). This method tells the framework which object users can see. Once this system is stable, you can move the configuration from the Exploration object to the configuration file. This is fully customized without recompiling. Object storage
The frame supports a variety of forms of object storage.
1, XML object storage
Store instances of each NakeD Objects in a separate XML file. This method can be used when processing a small amount of object.
2, continuous object storage
Store each NakeD Objects instance in a separate file and uses serialization. This is only suitable for processing a small amount of object. But it is much faster than using XML files.
3, SQL object storage
Use JDBC Storage Naked Objects Based Object Storage (MySQL, Oracle, etc.)
4, EJB object storage
Allow NAKED objects to use EJB
5, short object storage
This is the default object storage method, all objects are stored in memory, lost when the program exits.
In order to promote rapid prototyping, the framework defaults to short storage. For program stability, it is more suitable for stabilizing object storage, such as XML or database. We can change storage by overloading the InstallObjectStore () method.
If you use XML object storage
Protected nakedObjectStore InstallObjectStore () {
Return new xmlobjectStore ();
}
other
slightly
Change the complex name
The default is to add S after class name to create a plural name. The irregular noun can be modified by implementing the following method
Public static string pluralName ();
Change Person to People:
Public static string pluralName () {
Return "people";
}
Object sort
Public static string fieldorder ();
Public static string infordorder () {
Return "First Name, Last Name, Birthdate, Addresses";
}
Replace icon