The first part of petclinic source code is pojo.

Source: Internet
Author: User
Part 1 pojo
Bascentity
/**
* Simple JavaBean domain object with an ID property.
* Used as a base class for objects needing this property.
*
* @ Author Ken Krebs
* @ Author Juergen hoeller
*/
Public class baseentity {

Private integer ID;
.....................................
(The get/set method is omitted, the same below)
}
This class is the unified parent class of pojo corresponding to the object class and provides the ID attribute. Because each object has an ID attribute as the identifier, the ID is abstracted as a parent class.
Namedentity
/**
* Simple JavaBean domain object adds a Name property to <code> baseentity </code>.
* Used as a base class for objects needing these properties.
*
* @ Author Ken Krebs
* @ Author Juergen hoeller
*/
Public class namedentity extends baseentity {

Private string name;
..................................

}
This class is the unified parent class of pojo corresponding to a famous object and provides the name attribute. For example, pet has a name, and pettype also has a name. Therefore, the name is abstracted to the parent class.
Person
/**
* Simple JavaBean domain object representing an person.
*
* @ Author Ken Krebs
*/
Public class person extends baseentity {
Private string firstname;
Private string lastname;
....................................
}
The pojo corresponding to the object, providing the firstname and lastname for its subclass. For example, pet owner and veterinary vet are both human, but they have their own attributes. To avoid repeated code, they are abstracted into the person class.
Owner
/**
* Simple JavaBean domain object representing an owner.
*
* @ Author Ken Krebs
* @ Author Juergen hoeller
*/
Public class owner extends person {
Private string address;
Private string city;
Private string telephone;
Private set pets;
.........................................
// Why is the set method followed by internal?
Protected void setpetsinternal (set pets ){
This. Pets = pets;
}
// This method has an additional initialization task, mainly to prevent the returned result from being null and avoid // nullpointexception
Protected set getpetsinternal (){
If (this. Pets = NULL ){
This. Pets = new hashset ();
}
Return this. Pets;
}
/*
Why is the protected method?
*/
// This method sorts the owner's pets by name and returns a list object.
Public list getpets (){
List sortedpets = new arraylist (getpetsinternal ());
Propertycomparator. Sort (sortedpets, new mutablesortdefinition ("name", true, true ));
Return collections. unmodifiablelist (sortedpets );
}

Public void addpet (Pet ){
Getpetsinternal (). Add (PET );
Pet. setowner (this );
}

/**
* Return the pet with the given name, or null if none found for this owner.
*
* @ Param name to test
* @ Return true if pet name is already in use
*/
Public pet getpet (string name ){
Return getpet (name, false );
}

/**
* Return the pet with the given name, or null if none found for this owner.
*
* @ Param name to test
* @ Return true if pet name is already in use
*/
Public pet getpet (string name, Boolean ignorenew ){
Name = Name. tolowercase ();
For (iterator it = getpetsinternal (). iterator (); it. hasnext ();){
Pet = (PET) it. Next ();
If (! Ignorenew |! Pet. isnew ()){
String compname = pet. getname ();
Compname = compname. tolowercase ();
If (compname. Equals (name )){
Return PET;
}
}
}
Return NULL;
}
// The ignorenew of the second getpet method does not know how to use it, and the pet class does not have the isnew () method.
}
Vet
/**
* Simple JavaBean domain object representing a veterinarian.
*
* @ Author Ken Krebs
* @ Author Juergen hoeller
*/
Public class vet extends person {
Private set <specialty> specialties;

Protected void setspecialtiesinternal (set <specialty> specialties ){
This. specialties = specialties;
}

Protected set <specialty> getspecialtiesinternal (){
If (this. specialties = NULL ){
This. specialties = new hashset <specialty> ();
}
Return this. specialties;
}

Public list getspecialties (){
List <specialty> sortedspecs = new arraylist <specialty> (getspecialtiesinternal ());
Propertycomparator. Sort (sortedspecs, new mutablesortdefinition ("name", true, true ));
Return collections. unmodifiablelist (sortedspecs );
}

Public int getnrofspecialties (){
Return getspecialtiesinternal (). Size ();
}

Public void addspecialty (specialty ){
Getspecialtiesinternal (). Add (specialty );
}
}
// The pojo corresponding to the Veterinary entity. First, it inherits to person, and then extends its attributes specialties.
Pet
/**
* Simple JavaBean business object representing a pet.
*
* @ Author Ken Krebs
* @ Author Juergen hoeller
*/
Public class pet extends namedentity {
Private date birthdate;
Private pettype type;
Private owner;
Private set <visit> visits;
........................................ ....
Protected void setvisitsinternal (set <visit> visits ){
This. Visits = visits;
}

Protected set <visit> getvisitsinternal (){
If (this. Visits = NULL ){
This. Visits = new hashset <visit> ();
}
Return this. visits;
}

Public list <visit> getvisits (){
List <visit> sortedvisits = new arraylist <visit> (getvisitsinternal ());
Propertycomparator. Sort (sortedvisits, new mutablesortdefinition ("date", false, false ));
Return collections. unmodifiablelist (sortedvisits );
}

Public void addvisit (visit ){
Getvisitsinternal (). Add (visit );
Visit. setpet (this );
}
}
// The pojo corresponding to the pet.
Visit
/**
* Simple JavaBean domain object representing a visit.
*
* @ Author Ken Krebs
*/
Public class visit extends baseentity {
/** Holds value of property date .*/
Private date;
/** Holds value of property description .*/
Private string description;
/** Holds value of property pet .*/
Private PET;
/** Creates a new instance of visit for the current date */
Public Visit (){
This. Date = new date ();
}
}
Specialty
/**
* Models A {@ link vet's} specialty (for example, dentistry ).
*
* @ Author Juergen hoeller
*/
Public class specialty extends namedentity {

}
Pettype
/**
* @ Author Juergen hoeller
*/
Public class pettype extends namedentity {

}
In fact, pojo is only a simple JavaBean. The reason for this analysis is that the pojo design in this project is deeply integrated with the principle of object-oriented design, we hope to get some experience of object-oriented design from these pojo, and also hope to be applied in our actual projects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.