您当前的位置: 首页 > 

java持续实践

暂无认证

  • 5浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式 模板方法模式

java持续实践 发布时间:2022-04-04 09:26:56 ,浏览量:5

文章目录
      • 模板方法模式
      • 模板方法模式实战

模板方法模式

模板方法模式适用于统一了执行的顺序和基本策略, 但具体每一步如何实现, 由各自的方法去实现.

模板方法模式实战

模拟获取京东 , 淘宝, 当当网的数据. 虽然他们网站的结构不一样, 但获取数据的过程是一致的, 模拟登录, 获取数据, 对数据进行处理, 生成海报等.

模板方法模式的uml图如下 抽象类NetMall 如下

public abstract class NetMall {
    protected Logger logger = LoggerFactory.getLogger(NetMall.class);

    String uId;// 用户id
    String uPwd; // 用户密码

    public NetMall(String uId, String uPwd) {
        this.uId = uId;
        this.uPwd = uPwd;
    }

    /**
     * 可被外部访问的方法
     *  生成商品推广海报
     * @param skuUrl
     * @return
     */
    public String generateGoodsPoster(String skuUrl) {
        // 1. 验证登录
        if (!login(uId, uPwd)) return null;
        // 2. 爬虫商品
        Map reptile = reptile(skuUrl);
        // 3. 组装海报
        return createBase64(reptile);
    }

    //提供三个具体的抽象方法,让外部继承方实现

    // 模拟登录
    protected abstract boolean login(String uId, String uPwd);

    // 爬虫提取商品信息(登录后的优惠价格)
    protected abstract Map reptile(String skuUrl);

    // 生成商品海报信息
    protected abstract String createBase64(Map reptile);
}

HttpClient 工具类

public class HttpClient {

    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is,并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuilder sbf = new StringBuilder();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            assert connection != null;
            connection.disconnect();// 关闭远程连接
        }

        return result;
    }

}

京东网站的实现

/**
 * 类名称:JDNetMall
 * 创建时间:2022/4/3 19:59
 */
public class JDNetMall extends NetMall {

    public JDNetMall(String uId, String uPwd) {
        super(uId, uPwd);
    }

    @Override
    protected String createBase64(Map goodsInfo) {
        BASE64Encoder encoder = new BASE64Encoder();
        logger.info("模拟生成京东商品base64海报");
        return encoder.encode(JSON.toJSONString(goodsInfo).getBytes());
    }

    @Override
    protected Map reptile(String skuUrl) {
        String str = HttpClient.doGet(skuUrl);
        Pattern pattern = Pattern.compile("(?).*(?=            
关注
打赏
1658054974
查看更多评论
0.2316s