Skip to content

Commit ddc31bd

Browse files
authored
Add facade design pattern (#40)
1 parent af5d0ec commit ddc31bd

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
2+
3+
public class CPU {
4+
5+
public void freeze() {
6+
System.out.println("CPU is freezing");
7+
}
8+
9+
public void jump(long position) {
10+
System.out.println("CPU jumped to " + position);
11+
}
12+
13+
public void execute() {
14+
System.out.println("CPU is executing");
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
2+
3+
public interface Computer {
4+
5+
void start();
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
2+
3+
public class ComputerFacade implements Computer {
4+
5+
private final static long BOOT_ADDRESS = 0x0;
6+
private final static long BOOT_SECTOR = 0xF;
7+
private final static int SECTOR_SIZE = 16;
8+
9+
private final CPU processor;
10+
private final Memory ram;
11+
private final HardDrive hd;
12+
13+
public ComputerFacade() {
14+
this.processor = new CPU();
15+
this.ram = new Memory();
16+
this.hd = new HardDrive();
17+
}
18+
19+
@Override
20+
public void start() {
21+
processor.freeze();
22+
byte[] data = hd.read(BOOT_SECTOR, SECTOR_SIZE);
23+
ram.load(BOOT_ADDRESS, data);
24+
processor.jump(BOOT_ADDRESS);
25+
processor.execute();
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
2+
3+
public class HardDrive {
4+
5+
public byte[] read(long lba, int size) {
6+
System.out.println(size + " bits is reading from " + lba);
7+
return new byte[size];
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
2+
3+
public class Memory {
4+
5+
public void load(long position, byte[] data) {
6+
System.out.println("Data are loading");
7+
}
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.mperor.lab.java.design.pattern.structural.facade;
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 ComputerFacadeTest {
8+
9+
@Test
10+
public void shouldAllowToRunComputerInVerySimpleWayHidingItsComplexity() {
11+
var out = TestUtils.setTempSystemOut();
12+
Computer pc = new ComputerFacade();
13+
pc.start();
14+
Assertions.assertNotNull(pc);
15+
Assertions.assertTrue(out.all().contains("CPU is executing"));
16+
TestUtils.resetSystemOut();
17+
}
18+
19+
}

0 commit comments

Comments
 (0)