Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DesignPatterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ with practical examples and best practices for using design patterns to create r

- [Command](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/command) 📝
- [Execute Around Method (EAM)](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/eam) ⭕
- [Strategy](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy) 🎯
- [Observer](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/observer) 👀
- [Strategy](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy) 🎯
- [Template Method](src/main/java/pl/mperor/lab/java/design/pattern/behavioral/template/method) 📋

### Creational Design Patterns 🏭

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.mperor.lab.java.design.pattern.behavioral.template.method;

public abstract class BackupProcess {

public final void execute() {
if (!canExecuteBackup()) {
System.out.println("⛔ Backup not possible. Process terminated.");
return;
}
performBackup();
verifyBackup();
cleanup();
}

protected boolean canExecuteBackup() {
return true;
}

protected abstract void performBackup();

protected abstract void verifyBackup();

protected void cleanup() {
System.out.println("🧹 Deleting old backup files...");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.mperor.lab.java.design.pattern.behavioral.template.method;

public class DatabaseBackup extends BackupProcess {

DatabaseBackup() {
System.out.println("===== Database Backup =====");
}

@Override
protected boolean canExecuteBackup() {
System.out.println("🗄️ Checking database connection...");
return true;
}

@Override
protected void performBackup() {
System.out.println("💾 Dumping database to SQL file...");
}

@Override
protected void verifyBackup() {
System.out.println("✅ Checking database dump integrity...");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.mperor.lab.java.design.pattern.behavioral.template.method;

public class LocalFileBackup extends BackupProcess {

LocalFileBackup() {
System.out.println("===== File Backup =====");
}

@Override
protected boolean canExecuteBackup() {
System.out.println("🔍 Checking disk space for backup...");
return true;
}

@Override
protected void performBackup() {
System.out.println("📂 Copying files to backup directory...");
}

@Override
protected void verifyBackup() {
System.out.println("🔢 Comparing checksums of original and copied files...");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pl.mperor.lab.java.design.pattern.behavioral.template.method;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.common.TestUtils;

public class BackupProcessTemplateTest {

@Test
public void shouldAllowToExecuteBackupProcessForDifferentDataSources() {
var out = TestUtils.setTempSystemOut();
new DatabaseBackup().execute();
Assertions.assertLinesMatch("""
===== Database Backup =====
🗄️ Checking database connection...
💾 Dumping database to SQL file...
✅ Checking database dump integrity...
🧹 Deleting old backup files...
""".lines(), out.lines().stream());

out = TestUtils.setTempSystemOut();
new LocalFileBackup().execute();
Assertions.assertLinesMatch("""
===== File Backup =====
🔍 Checking disk space for backup...
📂 Copying files to backup directory...
🔢 Comparing checksums of original and copied files...
🧹 Deleting old backup files...
""".lines(), out.lines().stream());

TestUtils.resetSystemOut();
}
}