关于组合 和 集成 先放两篇文章:这两篇文章写的太好了.
http://blog.csdn.net/u013905744/article/details/51752044 Java的组合(持有对象)与继承的区别
http://blog.csdn.net/ycjnx/article/details/40952969 java继承和组合的区别,看两个例子,你就明白了
然后下面 我就贴出代码,给出一些具体的实例:
下面贴出代码:
IModel 接口:
package com.etoak.shejimoshi.basequery.model; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.sql.SQLException; import java.util.Map; public interface IModel { public JSONArray select(Map map); public JSONObject selectPageEasyUI(Map map); public JSONObject updatePageEasyUI(Map map) throws SQLException; public JSONObject deletePageEasyUI(Map map) throws SQLException; }
Model 实体类
package com.etoak.shejimoshi.basequery.model; import com.etoak.shejimoshi.basequery.dao.SimpleDAO; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import java.sql.SQLException; import java.util.*; public class Model extends HashMap implements IModel { public Model() { super(); } private String rows = "10"; private String page = "1"; private String nameSpace; private SimpleDAO dao; JsonConfig jf = new JsonConfig(); public SimpleDAO getDao() { return dao; } public void setDao(SimpleDAO dao) { this.dao = dao; } public String getNameSpace() { return nameSpace; } public void setNameSpace(String nameSpace) { this.nameSpace = nameSpace; } public String getRows() { return rows; } public String getPage() { return page; } public void setRows(String rows) { this.rows = rows; } public void setPage(String page) { this.page = page; } public JSONArray select(Map map) { if (map.containsKey("sqlId") && map.get("sqlId") != null) { return dao.select(nameSpace, (String) map.get("sqlId"), map, jf); } else { return dao.select(nameSpace, map, jf); } } public JSONArray select(String sqlid, Map map) { return dao.select(nameSpace, sqlid, map, jf); } public JSONObject selectPageEasyUI(Map map) { return dao.selectPageEasyUI(nameSpace, map, jf); } public JSONObject selectPageEasyUI(String sqlid, Map map) { return dao.selectPageEasyUI(nameSpace, sqlid, map, jf); } public JSONObject updatePageEasyUI(Map map) throws SQLException { return dao.updatePageEasyUI(nameSpace, map); } public JSONObject update(String string, Map map) throws SQLException { return dao.updatePageEasyUI(nameSpace, string, map); } public JSONObject deletePageEasyUI(Map map) throws SQLException { return dao.deletePageEasyUI(nameSpace, map); } }
dao接口
package com.etoak.shejimoshi.basequery.dao; import com.ibatis.sqlmap.client.SqlMapClient; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import java.sql.SQLException; import java.util.Map; public interface SimpleDAO { public JSONObject selectPageEasyUI(String nameSpace, Map map, JsonConfig jf); public JSONObject selectPageEasyUI(String nameSpace, String sqlid, Map map, JsonConfig jf); public JSONArray select(String nameSpace, Map map, JsonConfig jf); public JSONArray select(String nameSpace, String sqlid, Map map, JsonConfig jf); public JSONObject updatePageEasyUI(String nameSpace, Map map) throws SQLException; public JSONObject updatePageEasyUI(String nameSpace, String sqlid, Map map) throws SQLException; public JSONObject deletePageEasyUI(String nameSpace, Map map) throws SQLException; public SqlMapClient getSqlMapClient(); }
dao实现类
package com.etoak.shejimoshi.basequery.dao; import java.sql.SQLException; import java.util.Map; import com.ibatis.sqlmap.client.SqlMapClient; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class SimpleDaoImpl implements SimpleDAO{ @Override public JSONObject selectPageEasyUI(String nameSpace, Map map, JsonConfig jf) { return null; } @Override public JSONObject selectPageEasyUI(String nameSpace, String sqlid, Map map, JsonConfig jf) { return null; } @Override public JSONArray select(String nameSpace, Map map, JsonConfig jf) { return null; } @Override public JSONArray select(String nameSpace, String sqlid, Map map, JsonConfig jf) { return null; } @Override public JSONObject updatePageEasyUI(String nameSpace, Map map) throws SQLException { return null; } @Override public JSONObject updatePageEasyUI(String nameSpace, String sqlid, Map map) throws SQLException { return null; } @Override public JSONObject deletePageEasyUI(String nameSpace, Map map) throws SQLException { return null; } @Override public SqlMapClient getSqlMapClient() { return null; } }
user实体类:
package com.etoak.shejimoshi.userquery.model; import com.etoak.shejimoshi.basequery.dao.SimpleDAO; import com.etoak.shejimoshi.basequery.model.IModel; import com.etoak.shejimoshi.basequery.model.Model; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.sql.SQLException; import java.util.Map; public class User implements IModel { public static User config; private Model model; private SimpleDAO dao; public SimpleDAO getDao() { return dao; } public void setDao(SimpleDAO dao) { this.dao = dao; config.model.setDao(config.getDao()); } public User() { super(); if (config == null) { config = this; config.model = new Model(); } } @Override public JSONArray select(Map map) { return config.model.select(map); } @Override public JSONObject selectPageEasyUI(Map map) { return config.model.selectPageEasyUI(map); } @Override public JSONObject updatePageEasyUI(Map map) throws SQLException { return config.model.updatePageEasyUI(map); } @Override public JSONObject deletePageEasyUI(Map map) throws SQLException { return config.model.deletePageEasyUI(map); } }