refactoring : middle man - replace superclass with delegate

This commit is contained in:
haerong22
2022-04-08 01:15:12 +09:00
parent 38763848b9
commit 852cfbe5af
5 changed files with 117 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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)));
}
}