领域层类可以理解为程序运行时数据流的功能单位,而服务层类是为领域层类提供服务的,常见的服务有增删改查等操作,在没有泛型之前,我们只能采用一个服务类为一个领域类提供服务的方案,如果说服务的性质类似,如都是增删改查等,则代码的功能重复性很高,维护起来也很麻烦.如果采用了泛型类,我们就可以把性质相似的服务类归纳成一个,很大程度上能简化编码.
下面请看例子,我们有两个领域对象类:
Member类:
package com.sitinspring.domain;

import org.dom4j.Element;


/** *//**
* 成员类
* @author sitinspring
*
* @date 2007-12-28
*/

public class Member implements CanbeXmlElementable
{
// 成员名称
private String name;
// 年龄
private int age;

/** *//**
* 构造函数
* @param name
* @param age
*/

public Member(String name,int age)
{
this.name=name;
this.age=age;
}

/** *//**
* 将Member实例转化为XML中一个节点
*/

public Element generateElement(Element parent)
{
Element elm=parent.addElement("member");
Element nameElm=elm.addElement("name");
nameElm.setText(name);
Element ageElm=elm.addElement("age");
ageElm.setText(String.valueOf(age));
return elm;
}

public String toString()
{
return "Name="+name+" Age="+age;
}


public int getAge()
{
return age;
}


public void setAge(int age)
{
this.age = age;
}


public String getName()
{
return name;
}


public void setName(String name)
{
this.name = name;
}
}
公司类:
package com.sitinspring.domain;

import org.dom4j.Element;


/** *//**
* 公司类
* @author sitinspring
*
* @date 2007-12-28
*/

public class Company implements CanbeXmlElementable
{
// 公司名
private String name;
// 地址
private String address;

/** *//**
* 构造函数
* @param name
* @param address
*/

public Company(String name,String address)
{
this.name=name;
this.address=address;
}

/** *//**
* 将Company类的实例转化为XML的一个节点
*/

public Element generateElement(Element parent)
{
Element elm=parent.addElement("company");
Element nameElm=elm.addElement("name");
nameElm.setText(name);
Element ageElm=elm.addElement("address");
ageElm.setText(address);
return elm;
}

public String toString()
{
return "Company Name="+name+" Address="+address;
}


public String getName()
{
return name;
}


public void setName(String name)
{
this.name = name;
}


public String getAddress()
{
return address;
}


public void setAddress(String address)
{
this.address = address;
}
}
它们都实现了一个名为CanbeXmlElementable的接口:
package com.sitinspring.domain;

import org.dom4j.Element;


/** *//**
* 接口,强制子类实现generateElement函数
* @author sitinspring
*
* @date 2007-12-28
*/

public interface CanbeXmlElementable
{

/** *//**
* 用于把领域对象实例转化为XML中一个节点
* @param parent
* @return
*/
public Element generateElement(Element parent);
}
为什么要实现这个接口呢,下面就知道了.
对这两个对象需要实现集中管理和将集合存储到XML文件中,现在我们可以用泛型类实现这两个功能,下面看泛型类实现的新服务类:
package com.sitinspring.service;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.sitinspring.domain.CanbeXmlElementable;


/** *//**
* 服务类,持有对象的集合,负责对象的存储
* @author sitinspring
*
* @date 2007-12-28
*/

public class Service<T extends CanbeXmlElementable>
{
private List<T> elements;

/** *//**
* 添加集合元素
* @param element
*/

public void add(T element)
{

if(elements==null)
{
elements=new ArrayList<T>();
}
elements.add(element);
}

/** *//**
* 将集合保存成XML文件
* @param xmlFile
*/

public void saveToXml(String xmlFile)
{

try
{
Document document=DocumentHelper.createDocument();
Element root=document.addElement("elements");

for(T t:elements)
{
t.generateElement(root);
}
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK"); // 指定XML编码
XMLWriter writer = new XMLWriter(new FileWriter(xmlFile),format);
writer.write(document);
writer.close();
}

catch(Exception ex)
{
ex.printStackTrace();
}
}
}
对于两个领域对象的集合管理和XML持久化,使用这一个类就可以了,下面是测试代码:
package com.sitinspring;

import com.sitinspring.domain.Company;
import com.sitinspring.domain.Member;
import com.sitinspring.service.Service;


/** *//**
* 测试类
*
* @author sitinspring
*
* @date 2007-12-28
*/

public class Main
{

public static void main(String[] args)
{
// 泛型类测试一
Service<Member> memberService = new Service<Member>();

memberService.add(new Member("Andy", 25));
memberService.add(new Member("Bill", 35));
memberService.add(new Member("Cindy", 45));
memberService.add(new Member("Sitinspring", 55));

memberService.saveToXml("member.xml");

// 泛型类测试二
Service<Company> companyService = new Service<Company>();

companyService.add(new Company("IBM","NewYork"));
companyService.add(new Company("Microsoft", "beijing"));
companyService.add(new Company("Google", "Shanghai"));
companyService.add(new Company("Citigroup", "Dalian"));

companyService.saveToXml("company.xml");
}
}
文件结果:
member.xml:
<?xml version="1.0" encoding="GBK"?>

<elements>
<member>
<name>Andy</name>
<age>25</age>
</member>
<member>
<name>Bill</name>
<age>35</age>
</member>
<member>
<name>Cindy</name>
<age>45</age>
</member>
<member>
<name>Sitinspring</name>
<age>55</age>
</member>
</elements>

company.xml:
<?xml version="1.0" encoding="GBK"?>

<elements>
<company>
<name>IBM</name>
<address>NewYork</address>
</company>
<company>
<name>Microsoft</name>
<address>beijing</address>
</company>
<company>
<name>Google</name>
<address>Shanghai</address>
</company>
<company>
<name>Citigroup</name>
<address>Dalian</address>
</company>
</elements>

现在可以看出,以前使用多个类才能实现的功能在泛型类诞生后只需要一个类就可以了,它能高度归纳类的共性,减少功能相似类的数目,减少重复代码,减轻了项目的复杂度,真的是"很好,很强大!".
代码下载:
http://www.blogjava.net/Files/sitinspring/GenericPersistence20071228164755.rar
如果你对泛型类不是很熟悉可以参考:
http://www.blogjava.net/sitinspring/archive/2007/12/28/171212.html