Skip to content

Commit 39edef1

Browse files
authored
Add template design pattern with test case (#53)
1 parent 163b028 commit 39edef1

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

DesignPatterns/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ with practical examples and best practices for using design patterns to create r
1111

1212
- [Command](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/command) 📝
1313
- [Execute Around Method (EAM)](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/eam)
14-
- [Strategy](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy) 🎯
1514
- [Observer](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/observer) 👀
15+
- [Strategy](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy) 🎯
16+
- [Template Method](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/template/method) 📋
1617

1718
### Creational Design Patterns 🏭
1819

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.template.method;
2+
3+
public abstract class BackupProcess {
4+
5+
public final void execute() {
6+
if (!canExecuteBackup()) {
7+
System.out.println("⛔ Backup not possible. Process terminated.");
8+
return;
9+
}
10+
performBackup();
11+
verifyBackup();
12+
cleanup();
13+
}
14+
15+
protected boolean canExecuteBackup() {
16+
return true;
17+
}
18+
19+
protected abstract void performBackup();
20+
21+
protected abstract void verifyBackup();
22+
23+
protected void cleanup() {
24+
System.out.println("🧹 Deleting old backup files...");
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.template.method;
2+
3+
public class DatabaseBackup extends BackupProcess {
4+
5+
DatabaseBackup() {
6+
System.out.println("===== Database Backup =====");
7+
}
8+
9+
@Override
10+
protected boolean canExecuteBackup() {
11+
System.out.println("🗄️ Checking database connection...");
12+
return true;
13+
}
14+
15+
@Override
16+
protected void performBackup() {
17+
System.out.println("💾 Dumping database to SQL file...");
18+
}
19+
20+
@Override
21+
protected void verifyBackup() {
22+
System.out.println("✅ Checking database dump integrity...");
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.template.method;
2+
3+
public class LocalFileBackup extends BackupProcess {
4+
5+
LocalFileBackup() {
6+
System.out.println("===== File Backup =====");
7+
}
8+
9+
@Override
10+
protected boolean canExecuteBackup() {
11+
System.out.println("🔍 Checking disk space for backup...");
12+
return true;
13+
}
14+
15+
@Override
16+
protected void performBackup() {
17+
System.out.println("📂 Copying files to backup directory...");
18+
}
19+
20+
@Override
21+
protected void verifyBackup() {
22+
System.out.println("🔢 Comparing checksums of original and copied files...");
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.template.method;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import pl.mperor.lab.common.TestUtils;
6+
7+
public class BackupProcessTemplateTest {
8+
9+
@Test
10+
public void shouldAllowToExecuteBackupProcessForDifferentDataSources() {
11+
var out = TestUtils.setTempSystemOut();
12+
new DatabaseBackup().execute();
13+
Assertions.assertLinesMatch("""
14+
===== Database Backup =====
15+
🗄️ Checking database connection...
16+
💾 Dumping database to SQL file...
17+
✅ Checking database dump integrity...
18+
🧹 Deleting old backup files...
19+
""".lines(), out.lines().stream());
20+
21+
out = TestUtils.setTempSystemOut();
22+
new LocalFileBackup().execute();
23+
Assertions.assertLinesMatch("""
24+
===== File Backup =====
25+
🔍 Checking disk space for backup...
26+
📂 Copying files to backup directory...
27+
🔢 Comparing checksums of original and copied files...
28+
🧹 Deleting old backup files...
29+
""".lines(), out.lines().stream());
30+
31+
TestUtils.resetSystemOut();
32+
}
33+
}

0 commit comments

Comments
 (0)