第一部分 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;
.....................................
(get/set方法略去,下同)
}
该类为实体类对应的POJO的统一父类,提供id属性。因为每个实体都有一个id的属性做为标识,所以这里做为一个父类将id抽象出来。
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;
..................................
}
该类为有名实体对应的POJO的统一父类,提供name属性。比如Pet有名字,同样PetType也有名字,因此又将name抽象到父类。
Person
/**
* Simple JavaBean domain object representing an person.
*
* @author Ken Krebs
*/
public class Person extends BaseEntity {
private String firstName;
private String lastName;
....................................
}
人这个实体所对应的POJO,为其子类提供firstName和lastName。比如Pet的所有者Owner和兽医Vet,他们都是人,但是又有自己的属性,为了避免重复代码,抽象成Person类。
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;
.........................................
//为什么这里的set 方法后面要加上Internal呢?
protected void setPetsInternal(Set pets) {
this.pets = pets;
}
//该方法里面多了一个初始化的工作,主要是为了不让返回结果为null,避免 //NullPointException
protected Set getPetsInternal() {
if (this.pets == null) {
this.pets = new HashSet();
}
return this.pets;
}
/*
为什么要是protected 方法呢?
*/
//该方法把该Owner的宠物通过name排序后返回一个List对象。
public List getPets() {
List sortedPets = new ArrayList(getPetsInternal());
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedPets);
}
public void addPet(Pet 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 = (Pet) it.next();
if (!ignoreNew || !pet.isNew()) {
String compName = pet.getName();
compName = compName.toLowerCase();
if (compName.equals(name)) {
return pet;
}
}
}
return null;
}
//这里第二个getPet的方法的ignoreNew不知何用,而且Pet类也没有isNew()这个 //方法。
}
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 specialty) {
getSpecialtiesInternal().add(specialty);
}
}
//兽医实体所对应的POJO。首先继承至Person,然后扩展了自己的属性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 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 visit) {
getVisitsInternal().add(visit);
visit.setPet(this);
}
}
//宠物所对应的POJO。
Visit
/**
* Simple JavaBean domain object representing a visit.
*
* @author Ken Krebs
*/
public class Visit extends BaseEntity {
/** Holds value of property date. */
private Date date;
/** Holds value of property description. */
private String description;
/** Holds value of property pet. */
private Pet pet;
/** Creates a new instance of Visit for the current date */
public Visit() {
this.date = new Date();
}
}
Specialty
/**
* Models a {@link Vet Vet's} specialty (for example, dentistry).
*
* @author Juergen Hoeller
*/
public class Specialty extends NamedEntity {
}
PetType
/**
* @author Juergen Hoeller
*/
public class PetType extends NamedEntity {
}
其实POJO仅仅是简单的JavaBean,这里之所以要从它们开始分析,是因为这个项目中的POJO的设计深合面向对象设计的原理,希望从这些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;
.....................................
(get/set方法略去,下同)
}
该类为实体类对应的POJO的统一父类,提供id属性。因为每个实体都有一个id的属性做为标识,所以这里做为一个父类将id抽象出来。
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;
..................................
}
该类为有名实体对应的POJO的统一父类,提供name属性。比如Pet有名字,同样PetType也有名字,因此又将name抽象到父类。
Person
/**
* Simple JavaBean domain object representing an person.
*
* @author Ken Krebs
*/
public class Person extends BaseEntity {
private String firstName;
private String lastName;
....................................
}
人这个实体所对应的POJO,为其子类提供firstName和lastName。比如Pet的所有者Owner和兽医Vet,他们都是人,但是又有自己的属性,为了避免重复代码,抽象成Person类。
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;
.........................................
//为什么这里的set 方法后面要加上Internal呢?
protected void setPetsInternal(Set pets) {
this.pets = pets;
}
//该方法里面多了一个初始化的工作,主要是为了不让返回结果为null,避免 //NullPointException
protected Set getPetsInternal() {
if (this.pets == null) {
this.pets = new HashSet();
}
return this.pets;
}
/*
为什么要是protected 方法呢?
*/
//该方法把该Owner的宠物通过name排序后返回一个List对象。
public List getPets() {
List sortedPets = new ArrayList(getPetsInternal());
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedPets);
}
public void addPet(Pet 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 = (Pet) it.next();
if (!ignoreNew || !pet.isNew()) {
String compName = pet.getName();
compName = compName.toLowerCase();
if (compName.equals(name)) {
return pet;
}
}
}
return null;
}
//这里第二个getPet的方法的ignoreNew不知何用,而且Pet类也没有isNew()这个 //方法。
}
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 specialty) {
getSpecialtiesInternal().add(specialty);
}
}
//兽医实体所对应的POJO。首先继承至Person,然后扩展了自己的属性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 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 visit) {
getVisitsInternal().add(visit);
visit.setPet(this);
}
}
//宠物所对应的POJO。
Visit
/**
* Simple JavaBean domain object representing a visit.
*
* @author Ken Krebs
*/
public class Visit extends BaseEntity {
/** Holds value of property date. */
private Date date;
/** Holds value of property description. */
private String description;
/** Holds value of property pet. */
private Pet pet;
/** Creates a new instance of Visit for the current date */
public Visit() {
this.date = new Date();
}
}
Specialty
/**
* Models a {@link Vet Vet's} specialty (for example, dentistry).
*
* @author Juergen Hoeller
*/
public class Specialty extends NamedEntity {
}
PetType
/**
* @author Juergen Hoeller
*/
public class PetType extends NamedEntity {
}
其实POJO仅仅是简单的JavaBean,这里之所以要从它们开始分析,是因为这个项目中的POJO的设计深合面向对象设计的原理,希望从这些POJO中得出面向对象设计的一些体会,同时也希望在我们实际的项目中能够加以应用。