- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
前段时间我也在讨论类似的话题。我正在查看我的应用程序,我认为它有很多不必要的代码。我的意思是我有服务负责从两家书店的不同类别的书籍中抓取数据。现在我有 5 个类别,所以我有 5 个方法,但如果我要添加一些新类别怎么办?我将不得不添加更多方法......我认为这不是一个好的选择。现在它看起来像这样:
Controller
@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();
}
我在想
@GetMapping("/{category}")
public Map<......> get 15BooksFromCategory(@PathVariable CategoryType category)
{...}
我认为这是最好的方式,但服务更难。
它的服务看起来像这样:
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;
}
}
我必须传递 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;
}
}
最佳答案
(1) 姓名get15BooksFromCategory(CategoryType)
不对:您正在将要返回的一些书籍硬编码到方法名称中。
今天返15,明天返20,周日可能返5,安德鲁斯可能返50,你懂的。
考虑这些签名。
getAllBooksFromCategory(CategoryType);
getNBooksFromCategory(CategoryType, Integer);
(2) 去掉服务中的这些字段。
private final EmpikUrlProperties empikUrlProperties;
private final MerlinUrlProperties merlinUrlProperties;
private final JSoupConnector jSoupConnector;
前两个是 EmpikFetchingBookService
的一部分和 MerlinFetchingBookService
, 分别。 JSoupConnector
是一个更底层的抽象,它不应该出现在这个级别。它可能驻留在这些图书服务的共同父项中,或者是一个单独的 JSoupService
。共同 parent 所依赖的。
(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)));
}
}
BookServiceSource
具有与 BookService
相似的界面做。然而,MerlinSource
, 作为 BookServiceSource
的子类,不会将工作委托(delegate)给其他人。相反,它会准备一个 URL 并将其提供给 JSoupService
.
一个BookServiceSource
的责任准备请求参数并对JSoupService
返回的结果进行转换进入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)
}
}
想想 MerlinUrlProperties
作为一个实用程序,可以在类别和该类别的书籍的 URL 之间提供映射。
MerlinUrlProperties
可能是 Map
如果它只包含一堆返回 URL 的方法,那么它本身就是一个。关键是您不必为新类别定义新方法并强制使用您的 API 的每个人都改变自己以包含 API 的新部分。用Map
或者枚举,接口(interface)会更稳定。
Map<String, String> categoryToMarlinURL = new HashMap<>();
categoryToMarlinURL.put("horror", "marlin.com/horror");
categoryToMarlinURL.put("drama", "marlin.com/drama");
你拥有你需要的一切:
category
),categoryToMarlinURL.get(category)
),jSoupService.connect(categoryToMarlinURL.get(category))
)。关于java - 如何摆脱不必要的(?)代码——适应 DRY 原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57383806/
GitOps描述了一种使用植根于 Git 版本控制系统的方法来操作和管理软件的方法。使用基于 GitOps 的工作流,通过要求将系统的特征定义为 Git 存储库中的文件,可以更轻松地开发、部署、维护和
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
命令行货币转换器应用程序,提示用户输入源货币、源货币代码和目标货币代码,例如 C:\workspace> java CurrencyConverter 100.50 EUR GBP 应用程序返回源金额
得到这个实体: /** * @ORM\Table(name="shop_payment_details") * @ORM\Entity(repositoryClass="Acme\ShopBund
我有一个原则实体,无需调用 persist 或 flush 即可持久保存到数据库中。 我在下面很简单地重现了这个问题。正如您将看到的,此脚本从名为 MyEntity 的数据库表中加载一行,并获取一个以
在我的编程实践中,我经常遇到客户端和服务器端脚本之间数据重复的问题。 在这种情况下,我们可以讨论客户端的 JavaScript 和服务器端的 PHP 或 C# (ASP.NET)。 比方说,我有一段
简介 我在写关于继承问题的硕士论文并解决了一些问题 表明存在继承问题的指标。 像下面的例子: 示例 public static String getAnimalNoise(Animal animal)
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
当我注意到this answer时,我一直在阅读里氏替换原理。 。它有一个 Circle 和一个 ColoredCircle 类型,其中 ColoredCircle 的构造函数需要一个额外的参数; 颜
这段代码是否违反了DRY原则? if (notAuthorized) { return sendErrorCode(new ForbiddenException()) } else if (n
我在查询中使用 Doctrine 2 的结果缓存来检索用户(消息传递应用程序)的新消息数量: $query->useResultCache(true, 500, 'messaging.nb_new_m
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
如何设置包含类名的变量,例如 android.util.Log 中的 TAG,同时尊重 Dont-Repeat-Yourself? 以下是一些可能性: 在 Google 代码中,它的常用用法如下 pu
我有以下查询: $roles = array(); $roles[] = 'ROLE_SUPER_ADMIN'; $roles[] = 'ROLE_ADMIN';
下面的代码违反了哪一条 SOLID 原则? public class A { void hello(){ //some code here } } public class B ext
我目前有一个 Message_Repository 类,它有如下方法: getLocationDetailsByID($messageId), getCustomerDetailsById($mess
我不知道它到底叫什么,但现在我将它称为“非空测试”。在 C# 8 中有一个新的行为允许测试一个对象是否不为空,例如: Foo foo = new Foo(); if(foo is { }) {
我正在学习 Doctrine。我在多对多关系中有两个实体 Article 和 Category,我正在尝试获取所有不是特定文章的类别。 文章实体: class Article extends Base
在阅读了一本书和一篇在线文章中有关 SOLID 代码的内容后,我想重构一个现有的类,使其与“SOLID”兼容。 但我想我迷路了,尤其是依赖注入(inject):当我想实例化类的一个对象时,我需要“注入
我的项目中有类似的东西,这个项目已经完成了(它正在运行)我只想知道 SOLID 原则是否可以接受 static public class Tools { static public GetPr
我是一名优秀的程序员,十分优秀!