首页 > 其他分享 >设计满足开闭原则的策略模式

设计满足开闭原则的策略模式

时间:2022-12-08 19:24:02浏览次数:44  
标签:ip 模式 开闭 满足 springframework org import com CrawlerDTO

开闭原则是说不修改原有代码的基础上,对于新增加的功能采用实现接口的方法即可实现

策略模式将对象的行为抽象为一个通用的方法,不同的对象实现同一个方法进行重写,可以减少复杂的if  else的出现

代码如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ip</groupId>
    <artifactId>ip-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencyManagement>

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>



        </dependencies>
    </dependencyManagement>


    <dependencies>

        <!--客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--tomcat容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!--java爬虫需要的jar包-->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.2</version>
        </dependency>
        <!--判断空的用法  -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!--时间日期工具类-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.10.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>1.7.2</version>
        </dependency>
         <!--RateLimiter的底层是基于令牌桶算法来实现的,来自谷歌的Guava包中-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0-jre</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>1.7.2</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
        <finalName>server</finalName>
    </build>


</project>

  

package com.ip.controller.front;

import com.ip.bean.CrawlerDTO;
import com.ip.config.SpringBeanUtil;
import com.ip.handle.strategy.CrawlerHandleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yourheart
 * @Description
 * @create 2022-04-26 21:40
 */

@Controller
@RequestMapping("/crawler")
@Slf4j
public class CrawlerController {


    /**
     * 自动获取网页的名称
     * @param crawlerDTO
     * @return
     */
    @PostMapping("/crawlerAddress")
    @ResponseBody
    public CrawlerDTO  crawlerAddress(@RequestBody CrawlerDTO crawlerDTO){
        log.info("【自动获取网页的名称】入参:{}",crawlerDTO);
        String serverName=crawlerDTO.getUrlDatasource()+"_crawler";
        CrawlerHandleStrategy bean = SpringBeanUtil.getBean(serverName, CrawlerHandleStrategy.class);
        CrawlerDTO crawlerDto = bean.getCrawlerDto(crawlerDTO);
        return crawlerDto;
    }
}

  

package com.ip.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/8 18:42
 */
@Component
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (context==null){
            context=applicationContext;
        }
    }

    public static ApplicationContext getContext(){
        return context;
    }

    /**
     * 通过name找到对应的实例
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getContext().getBean(name);
    }

    /**
     * 通过class获取实例
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> tClass){
        return getContext().getBean(tClass);
    }

    public static <T> T getBean(String name,Class<T> tClass){
        T bean = getContext().getBean(name, tClass);
        return bean;
    }


}

  

package com.ip.handle.strategy;

import com.ip.bean.CrawlerDTO;

/**
 * @Description: 处理爬虫的策略接口
 * @Author: Yourheart
 * @Create: 2022/12/8 15:15
 */
public interface CrawlerHandleStrategy {

    /**
     * 通过爬虫获取网页地址的名称
     * @param crawlerDTO
     * @return
     */
    CrawlerDTO getCrawlerDto(CrawlerDTO crawlerDTO);

}

  

package com.ip.handle.strategy.impl;

import com.ip.bean.CrawlerDTO;
import com.ip.handle.strategy.CrawlerHandleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/8 15:38
 */
@Slf4j
@Service("default_crawler")
public class DefaultCrawlerHandleStrategy implements CrawlerHandleStrategy {

    /**
     * 通过爬虫获取网页地址的名称
     *
     * @param crawlerDTO
     * @return
     */
    @Override
    public CrawlerDTO getCrawlerDto(CrawlerDTO crawlerDTO) {
        log.info("使用默认方法获取网页地址.....");
        return null;
    }
}

  

package com.ip.handle.strategy.impl;

import com.ip.bean.CrawlerDTO;
import com.ip.handle.strategy.CrawlerHandleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/8 15:37
 */
@Slf4j
@Service("jianshu_crawler")
public class JianShuCrawlerHandleStrategy implements CrawlerHandleStrategy {
    /**
     * 通过爬虫获取网页地址的名称
     *
     * @param crawlerDTO
     * @return
     */
    @Override
    public CrawlerDTO getCrawlerDto(CrawlerDTO crawlerDTO) {
        log.info("爬虫简书的地址...");
        return null;
    }
}

  

package com.ip.handle.strategy.impl;

import com.ip.bean.CrawlerDTO;
import com.ip.handle.strategy.CrawlerHandleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Description:
 * @Author: Yourheart
 * @Create: 2022/12/8 15:37
 */
@Slf4j
@Service("weixin_crawler")
public class WeiXinCrawlerHandleStrategy implements CrawlerHandleStrategy {
    /**
     * 通过爬虫获取网页地址的名称
     *
     * @param crawlerDTO
     * @return
     */
    @Override
    public CrawlerDTO getCrawlerDto(CrawlerDTO crawlerDTO) {
        log.info("爬虫微信的地址...");
        String url = crawlerDTO.getUrl();
        try {
            Document doc = Jsoup.connect(url).get();
            Elements select = doc.select("meta[property=twitter:title]");
            StringBuilder stringBuilder=new StringBuilder();
            for (Element element : select) {
                String string = element.toString();
                string = string.replace("<", "");
                string = string.replace(">", "");
                string = string.replace("\"", "");
                String[] split = string.split("=");
                Arrays.stream(split).forEach(a->{
                    log.info("a:{}",a);
                });
                stringBuilder.append(split[split.length - 1]);
            }
        } catch (IOException e) {
            log.error("【微信爬虫失败】:{}",e);
        }
        return null;
    }
}

  

标签:ip,模式,开闭,满足,springframework,org,import,com,CrawlerDTO
From: https://www.cnblogs.com/q202105271618/p/16967036.html

相关文章

  • Redis的三种模式
    一、主从复制1.1主从复制的概念主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(Master),后者称为从节点(Slave);数据的复制是单向的,只......
  • Redis 主从复制、哨兵模式、Cluster集群
    一、Redis主从复制1.1、什么是主从复制如图:将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(master),后者称为从节点(slave),数据的复制是单向的,只能由......
  • 设计模式及设计规则详解(1)
    设计模式什么是设计模式?是在系统设计中针对场景的一种方案,是解决功能逻辑开发的一种共性问题。设计模式学习的是什么?学习的是设计模式的思想,不能直接进行硬套设计原则......
  • vs2017中解决“调试模式下无法修改代码”的问题
    …不是在启用“编辑并继续”的情况下编译的。请确保使用程序数据库的“编辑并继续(/ZI)”选项编译此文件。相信在编程中你也会遇到这种问题,不要慌,这篇博客将帮你彻底解决......
  • 命令模式
    智能生活项目需求命令模式基本介绍命令模式原理类图   命令模式解决智能生活项目 代码实现 packagecom.sky.command;//创建命令接口publicinte......
  • lua模式匹配
    新入门skynet系列视频b站网址https://www.bilibili.com/video/BV19d4y1678X普通字符串模式字符串普通字符串在普通字符串中,字符\是转移字符。也就是说\有特殊......
  • Java单例模式的最佳实践?
    “读过书,……我便考你一考。茴香豆的茴字,怎样写的?”——鲁迅《孔乙己》0x00大纲目录0x00大纲0x01前言0x02单例的正确性new关键字clone方法克隆反序列化反射0x03最......
  • 【221208-5】已知:实数xy满足x平方/5+y平方=1.求:(x-2)平方+y平方的最大值?
    ......
  • 3、设计模式—单例、工厂、代理、适配器、观察者
     1、 设计模式——单例模式 单例模式负责创建自己的对象,同时确保只有一个对象被创建。比如,我们要统计网站的访问用户数,在任何时候都只有一个对象在记录用户数,否则就可能......
  • PDF转Word完全指南:3大方法满足各种场景!
    还不知道PDF怎么转Word吗,本文将提供完整的PDF转Word方案,包括离线、在线或者SDKAPI等各种方式,总有一款满足您的需求。什么是PDF转Word?PDF是出版和图形领域的软件厂商Adob......