请教彭老师

能否将第八章中的modelPoolFactory,ModelMapping类代码贴上来,或将设计思路给我顺一下,谢谢。
modelPoolFactory类似一个对象池工厂,主要是为提高性能,对象池组件apache有很多源码参考。类似SPring中PoolFactory拦截器,在本书中是直接调用,没有使用AOP拦截。

ModelMapping主要是XML配置文件的Java映射,类似Spring中的XmlBeanFactory,没它那么复杂。

根据程序相关调用及彭老师的指点,动手将这两个类实现了,现贴出来与需要者共享,也请彭老师继续指点。

//ModelMapping
package com.jdon.strutsutil;
/
*

Title: A framework project.


*

Description:


*

Copyright: Copyright (c) 2004 .


*

Company:


* @author Written by liyong.
* @version 1.0
*
* * formName = "itemForm"
* key="itemId"
* model="com.jdon.estore.model.Item"
* handler = "com.jdon.estore.web.catalog.ItemHandler" />
*

*/

public class ModelMapping {

private String key;
private String model;
private String handler;

public ModelMapping() { }

public void setKeyValue(String key) {
this.key = key;
}

public String getKeyValue() {
return this.key;
}

public String getHandler() {
return handler;
}

public void setHandler(String handler) {
this.handler = handler;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}
}


//ModelPoolFactory
package com.jdon.strutsutil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.jdon.controller.model.Model;

/
*

Title: A framework project.


*

Description:


*

Copyright: Copyright (c) 2004 .


*

Company:


* @author Written by liyong.
* @version 1.0
*
* * formName = "itemForm"
* key="itemId"
* model="com.jdon.estore.model.Item"
* handler = "com.jdon.estore.web.catalog.ItemHandler" />
*

*/

public class ModelPoolFactory {

private static final String MODEL_MAPPING_FILE = "/modelmapping.xml";
private static final String DEFAULT_MODEL_MAPPINGS = "modelmappings";
private static final String DEFAULT_MODEL_MAPPING = "modelmapping";
private static final String ATTRIBUTE_FORMNAME = "formName";
private static final String ATTRIBUTE_KEY = "key";
private static final String ATTRIBUTE_MODEL = "model";
private static final String ATTRIBUTE_HANDLER = "handler";

private static ModelPoolFactory instance = new ModelPoolFactory();
private Map modelMap = new HashMap();

/
* Initialization.
* Read configuration from file : /modelmapping.xml.
*/
private ModelPoolFactory() {
try {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(getClass().getResourceAsStream(MODEL_MAPPING_FILE));
Element root = doc.getRootElement();
List list = root.getChildren(DEFAULT_MODEL_MAPPING);
for (int i=0; i Element child = (Element)list.get(i);
ModelMapping mapping = new ModelMapping();
mapping.setKeyValue(child.getAttribute(ATTRIBUTE_KEY).getValue());
mapping.setModel(child.getAttribute(ATTRIBUTE_MODEL).getValue());
mapping.setHandler(child.getAttribute(ATTRIBUTE_HANDLER).getValue());
String key = child.getAttribute(ATTRIBUTE_FORMNAME).getValue();
modelMap.put(key, mapping);
//System.out.println(child.getAttribute(ATTRIBUTE_FORMNAME).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_KEY).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_MODEL).getValue());
//System.out.println(child.getAttribute(ATTRIBUTE_HANDLER).getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}

/
* Return instance of ModelPoolFactory.
* Singleton pattern.
* @return
*/
public static ModelPoolFactory getInstance() {
return instance;
}

/
* Get Handler Object.
* @param formName
* @return
*/
public ModelHandler getHandlerObject(String formName){
ModelHandler modelHandler = null;
try {
ModelMapping mapping = (ModelMapping)modelMap.get(formName);
String className = mapping.getHandler();
modelHandler = (ModelHandler) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return modelHandler;
}

/
* Get Model Object.
* @param formName
* @return
*/
public Model getModelObject(String formName){
Model model = null;
try {
ModelMapping mapping = (ModelMapping)modelMap.get(formName);
String className = mapping.getModel();
model = (Model) Class.forName(className).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return model;
}

/
* Return the model mapping.
* @return
*/
public Map getModelMappings() {
return this.modelMap;
}

/
* Test entry.
* @param args
*/
public static void main(String[] args) {
ModelPoolFactory mpf = ModelPoolFactory.getInstance();
}
}

不错,非常棒,这样能够自己动手写这些辅助类是我不公布源码的目的。这样的好处很多,一方面读者更加加深了认识和理解,二者,整个系统源码可以由读者自己支配灵活使用在自己项目中。
惭愧,学习java的时间不长,还请多指教。
随着开发项目的增加,越来越觉得重复的事情做得越来越多,经常性的为了一个小小的代码问题而耗费精力。自然,编写代码也变得越来越是个痛苦的事。幸而,拜读了彭老师的书,使我有了豁然开朗的感觉,于是就想按书上的思想去实现它,当然,因自己水平的关系,代码只能在不断地完善和提炼。