Java中组合 设计技巧 实例

关于组合 和 集成 先放两篇文章:这两篇文章写的太好了.

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);
    }
}

 

转载于:https://www.cnblogs.com/sanmubird/p/8109487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值