refactoring : middle man - replace superclass with delegate
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.example.refactoring._18_middle_man._39_replace_superclass_with_delegate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CategoryItem {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
private List<String> tags;
|
||||
|
||||
public CategoryItem(Integer id, String title, List<String> tags) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public boolean hasTag(String tag) {
|
||||
return this.tags.contains(tag);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._18_middle_man._39_replace_superclass_with_delegate;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
public class Scroll {
|
||||
|
||||
private LocalDate dateLastCleaned;
|
||||
|
||||
private CategoryItem categoryItem;
|
||||
|
||||
public Scroll(Integer id, String title, List<String> tags, LocalDate dateLastCleaned) {
|
||||
this.dateLastCleaned = dateLastCleaned;
|
||||
this.categoryItem = new CategoryItem(id, title, tags);
|
||||
}
|
||||
|
||||
public long daysSinceLastCleaning(LocalDate targetDate) {
|
||||
return this.dateLastCleaned.until(targetDate, ChronoUnit.DAYS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.refactoring._18_middle_man._39_replace_superclass_with_delegate._before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CategoryItem {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private String title;
|
||||
|
||||
private List<String> tags;
|
||||
|
||||
public CategoryItem(Integer id, String title, List<String> tags) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public boolean hasTag(String tag) {
|
||||
return this.tags.contains(tag);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.refactoring._18_middle_man._39_replace_superclass_with_delegate._before;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
public class Scroll extends CategoryItem {
|
||||
|
||||
private LocalDate dateLastCleaned;
|
||||
|
||||
public Scroll(Integer id, String title, List<String> tags, LocalDate dateLastCleaned) {
|
||||
super(id, title, tags);
|
||||
this.dateLastCleaned = dateLastCleaned;
|
||||
}
|
||||
|
||||
public long daysSinceLastCleaning(LocalDate targetDate) {
|
||||
return this.dateLastCleaned.until(targetDate, ChronoUnit.DAYS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.refactoring._18_middle_man._39_replace_superclass_with_delegate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ScrollTest {
|
||||
|
||||
@Test
|
||||
void daysSinceLastCleaning() {
|
||||
Scroll scroll = new Scroll(1, "kim", null, LocalDate.of(2022, 1, 10));
|
||||
assertEquals(5, scroll.daysSinceLastCleaning(LocalDate.of(2022, 1, 15)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user