如何摆脱不必要的代码?-适应DRY原理

mar*_*122 5 java dry solid-principles

不久前,我遇到了类似的话题。我正在看我的应用程序,我认为它有很多不必要的代码。我的意思是,我拥有负责从两个书店中的不同类别的书中抓取数据的服务。现在我有5个类别,所以我有5个方法,但是如果我要添加一些新类别呢?我将不得不添加更多方法...,我认为这不是一个好选择。现在看起来像这样:

控制者

@GetMapping("/romances")
    public Map<Bookstore, List<Book>> get15RomanticBooks() {
        return categorizedBookService.get15BooksFromRomanceCategory();
    }

    @GetMapping("/biographies")
    public Map<Bookstore, List<Book>> get15BiographiesBooks() {
        return categorizedBookService.get15BooksFromBiographiesCategory();
    }

    @GetMapping("/guides")
    public Map<Bookstore, List<Book>> get15GuidesBooks() {
        return categorizedBookService.get15BooksFromGuidesCategory();
    }

    @GetMapping("/fantasy")
    public Map<Bookstore, List<Book>> get15FantasyBooks() {
        return categorizedBookService.get15BooksFromFantasyCategory();
    }
Run Code Online (Sandbox Code Playgroud)

在这里我在想

@GetMapping("/{category}")
public Map<......> get 15BooksFromCategory(@PathVariable CategoryType category)
{...}
Run Code Online (Sandbox Code Playgroud)

我认为这是最好的方法,但是使用服务很难。

服务如下:

package bookstore.scraper.book.scrapingtypeservice;

import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.fetcher.empik.EmpikFetchingBookService;
import bookstore.scraper.fetcher.merlin.MerlinFetchingBookService;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.urlproperties.MerlinUrlProperties;
import bookstore.scraper.utilities.JSoupConnector;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CategorizedBookService {

    private final EmpikFetchingBookService empikBookService;
    private final MerlinFetchingBookService merlinFetchingBookService;
    private final EmpikUrlProperties empikUrlProperties;
    private final MerlinUrlProperties merlinUrlProperties;
    private final JSoupConnector jSoupConnector;

    @Autowired
    public CategorizedBookService(EmpikFetchingBookService empikBookService, MerlinFetchingBookService merlinFetchingBookService, EmpikUrlProperties empikUrlProperties, MerlinUrlProperties merlinUrlProperties, JSoupConnector jSoupConnector) {
        this.empikBookService = empikBookService;
        this.merlinFetchingBookService = merlinFetchingBookService;
        this.empikUrlProperties = empikUrlProperties;
        this.merlinUrlProperties = merlinUrlProperties;
        this.jSoupConnector = jSoupConnector;
    }

    public Map<Bookstore, List<Book>> get15BooksFromRomanceCategory() {
        return get15BooksFrom(empikUrlProperties.getEmpik().getRomances(), merlinUrlProperties.getMerlin().getRomances());
    }

    public Map<Bookstore, List<Book>> get15BooksFromFantasyCategory() {
        return get15BooksFrom(empikUrlProperties.getEmpik().getFantasy(), merlinUrlProperties.getMerlin().getFantasy());
    }

    public Map<Bookstore, List<Book>> get15BooksFromCrimeCategory() {
        return get15BooksFrom(empikUrlProperties.getEmpik().getCrime(), merlinUrlProperties.getMerlin().getCrime());
    }

    public Map<Bookstore, List<Book>> get15BooksFromGuidesCategory() {
        return get15BooksFrom(empikUrlProperties.getEmpik().getGuides(), merlinUrlProperties.getMerlin().getGuides());
    }

    public Map<Bookstore, List<Book>> get15BooksFromBiographiesCategory() {
        return get15BooksFrom(empikUrlProperties.getEmpik().getBiographies(), merlinUrlProperties.getMerlin().getBiographies());
    }

    private Map<Bookstore, List<Book>> get15BooksFrom(String bookStoreEmpikURL, String bookStoreMerlinURL) {
        Map<Bookstore, List<Book>> bookstoreWith15CategorizedBooks = new EnumMap<>(Bookstore.class);

        bookstoreWith15CategorizedBooks.put(Bookstore.EMPIK, empikBookService
                .get15BooksFromCategory(jSoupConnector.connect(bookStoreEmpikURL)));
        bookstoreWith15CategorizedBooks.put(Bookstore.MERLIN, merlinFetchingBookService
                .get15BooksFromCategory(jSoupConnector.connect(bookStoreMerlinURL)));

        return bookstoreWith15CategorizedBooks;
    }
}
Run Code Online (Sandbox Code Playgroud)

我必须通过2个不同的链接,具体取决于调用的类别。有什么办法吗?

EmpikBookService/merlinFetchingBookService 是使用Jsoup抓取数据的服务。

package bookstore.scraper.fetcher.empik;

import bookstore.scraper.book.Book;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Service
public class EmpikFetchingBookService {

    private static final int FIRST_PART_PRICE = 0;
    private static final int SECOND_PART_PRICE = 1;

    private static final int BESTSELLERS_NUMBER_TO_FETCH = 5;
    private static final int CATEGORIZED_BOOKS_NUMBER_TO_FETCH = 15;
    private static final String DIV_PRODUCT_WRAPPER = "div.productWrapper";
    private static final String DATA_PRODUCT_ID = "data-product-id";

    private final EmpikUrlProperties empikUrlProperties;

    @Autowired
    public EmpikFetchingBookService(EmpikUrlProperties empikUrlProperties) {
        this.empikUrlProperties = empikUrlProperties;
    }

    public Book getMostPreciseEmpikBook(Document document) {
        String author = document.select("div.smartAuthorWrapper.ta-product-smartauthor").select("a").first().text();
        String price = convertEmpikPriceWithPossibleDiscountToActualPrice(document.select("div.price.ta-price-tile").first().text());
        String title = document.select(DIV_PRODUCT_WRAPPER).select("strong").first().text();
        String productID = document.select(DIV_PRODUCT_WRAPPER).select("a").first().attr(DATA_PRODUCT_ID);
        String bookUrl = createBookURL(title, productID);

        return Book.builder()
                .author(author)
                .price(price)
                .title(title)
                .productID(productID)
                .bookURL(bookUrl).build();
    }

    public List<Book> get5BestSellersEmpik(Document document) {
        List<Element> siteElements = document.select(DIV_PRODUCT_WRAPPER);
        List<Book> empikBestSellers = new ArrayList<>();

        IntStream.range(0, BESTSELLERS_NUMBER_TO_FETCH)
                .forEach(iteratedElement -> {

                    String author = siteElements.get(iteratedElement).select("div.smartAuthorWrapper.ta-product-smartauthor").select("a").first().text();
                    String price = convertEmpikPriceWithPossibleDiscountToActualPrice(siteElements.get(iteratedElement).select("div.price.ta-price-tile").first().text());
                    String title = siteElements.get(iteratedElement).select("strong").first().ownText();
                    String productID = siteElements.get(iteratedElement).select(DIV_PRODUCT_WRAPPER).select("a").first().attr(DATA_PRODUCT_ID);
                    String bookUrl = createBookURL(title, productID);

                    empikBestSellers.add(Book.builder()
                            .author(author)
                            .price(price)
                            .title(title)
                            .productID(productID)
                            .bookURL(bookUrl)
                            .build());
                });
        return empikBestSellers;
    }

    public List<Book> get15BooksFromCategory(Document document) {
        List<Book> books = new ArrayList<>();
        List<Element> siteElements = document.select("div.productBox__info");

        IntStream.range(0, CATEGORIZED_BOOKS_NUMBER_TO_FETCH)
                .forEach(iteratedElement -> {

                    String author = executeFetchingAuthorProcess(siteElements, iteratedElement);
                    String price = convertEmpikPriceWithPossibleDiscountToActualPrice(siteElements.get(iteratedElement).select("div.productBox__price").first().text());
                    String title = siteElements.get(iteratedElement).select("span").first().ownText();
                    String productID = siteElements.get(iteratedElement).select("a").first().attr(DATA_PRODUCT_ID);
                    String bookUrl = createBookURL(title, productID);

                    books.add(Book.builder()
                            .author(author)
                            .price(price)
                            .title(title)
                            .productID(productID)
                            .bookURL(bookUrl)
                            .build());
                });

        return books;
    }

    private String convertEmpikPriceWithPossibleDiscountToActualPrice(String price) {
        String[] splittedElements = price.split("\\s+");
        return splittedElements[FIRST_PART_PRICE] + splittedElements[SECOND_PART_PRICE];
    }

    private String createBookURL(String title, String productID) {
        return String.format(empikUrlProperties.getEmpik().getConcreteBook(), title, productID);
    }

    //method is required as on empik site, sometimes occurs null for author and we need to change code for fetching
    private static String executeFetchingAuthorProcess(List<Element> siteElements, int i) {
        String author;
        Element authorElements = siteElements.get(i).select("span > a").first();
        if (authorElements != null)
            author = authorElements.ownText();
        else
            author = siteElements.get(i).select("> span > span").first().text();
        return author;
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*lko 3

(1) 名称get15BooksFromCategory(CategoryType)不正确:您将要返回的一些书籍硬编码到方法名称中。

今天你返回15,明天你需要返回20,周日你可能需要返回5,对于安德鲁斯你可能需要返回50。你明白了。

考虑这些签名。

getAllBooksFromCategory(CategoryType);
getNBooksFromCategory(CategoryType, Integer);
Run Code Online (Sandbox Code Playgroud)

(2) 在服务中去掉这些字段。

private final EmpikUrlProperties empikUrlProperties;
private final MerlinUrlProperties merlinUrlProperties;
private final JSoupConnector jSoupConnector;
Run Code Online (Sandbox Code Playgroud)

前两个分别是EmpikFetchingBookService和的一部分MerlinFetchingBookServiceJSoupConnector是一个较低级别的抽象,它不应该出现在这个级别。它可以驻留在这些图书服务的共同父级中,也可以是JSoupService共同父级所依赖的独立服务。

(3) 理想情况下,您最终应该得到一个非常简单的服务,该服务具有单一职责 - 从其来源收集书籍。

 class BookService {
      private List<BookServiceSource> sources;

      public Map<String, List<Book>> getBooksByCategory(Category category) {
          return sources.stream()
              .collect(Collectors.toMap(BookServiceSource::getName, 
                  source -> source.getBooksByCategory(category)));
      }
 }
Run Code Online (Sandbox Code Playgroud)

BookServiceSource具有类似的界面BookService。然而,MerlinSource作为 的子类BookServiceSource,并不将工作委托给其他人。相反,它会准备一个 URL 并将其提供给JSoupService.

a 的职责BookServiceSource是准备请求参数并将 a 返回的结果转换JSoupService为 a List<Book>。由于每个书店都有不同的 DOM,因此您需要知道如何将特定 DOM 映射到您的结构中。

interface BookServiceSource {
    String getName();
    List<Book> getBooksByCategory(Category category);
}

class MerlinSource implements BookServiceSource {
    private JSoupService service;
    private MerlinUrlProperties properties;

    @Override
    public String getName() {
      return "merlin";
    }

    @Override
    public List<Book> getBooksByCategory(Category category) {
      // at this point, we have both 
      // JSoupService (to make a real request) and 
      // MerlinUrlProperties (to prepare everything for that request)
    }
}
Run Code Online (Sandbox Code Playgroud)

可以将其视为MerlinUrlProperties一个实用程序,它可以在类别和该类别图书的 URL 之间提供映射。

MerlinUrlPropertiesMap如果它只包含一堆返回 URL 的方法,则它可能是一个自身。关键是您不必为新类别定义新方法,也不必强迫每个使用您的 API 的人都改变自己以包含 API 的新部分。使用Map或枚举,接口会更稳定。

Map<String, String> categoryToMarlinURL = new HashMap<>();

categoryToMarlinURL.put("horror", "marlin.com/horror");
categoryToMarlinURL.put("drama", "marlin.com/drama");
Run Code Online (Sandbox Code Playgroud)

您拥有所需的一切:

  • 类别 ( category),
  • 该类别的 URL ( categoryToMarlinURL.get(category)),
  • 发出请求的服务 ( jSoupService.connect(categoryToMarlinURL.get(category)))。