JR 精品文章 - 研读Spring发布包带的例子petClinic源码之第一部分 POJO
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发框架 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
研读Spring发布包带的例子petClinic源码之第一部分 POJO
reverocean 原创   更新:2007-03-21 15:35:57  版本: 1.0   

第一部分 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中得出面向对象设计的一些体会,同时也希望在我们实际的项目中能够加以应用。

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     6       1
作者其它文章: 作者全部文章
评论人:gaoshan_28 发表时间: Mon Mar 26 18:05:41 CST 2007
good [good][good]

这个文章共有 1 条评论
主题: 在struts里的实现dtree通用树型结构 上一篇文章
返回文章列表 返回〔开发框架〕
下一篇文章 主题: ajax、Struts、spring的无缝结合


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号