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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

public class CPU {

public void freeze() {
System.out.println("CPU is freezing");
}

public void jump(long position) {
System.out.println("CPU jumped to " + position);
}

public void execute() {
System.out.println("CPU is executing");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

public interface Computer {

void start();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

public class ComputerFacade implements Computer {

private final static long BOOT_ADDRESS = 0x0;
private final static long BOOT_SECTOR = 0xF;
private final static int SECTOR_SIZE = 16;

private final CPU processor;
private final Memory ram;
private final HardDrive hd;

public ComputerFacade() {
this.processor = new CPU();
this.ram = new Memory();
this.hd = new HardDrive();
}

@Override
public void start() {
processor.freeze();
byte[] data = hd.read(BOOT_SECTOR, SECTOR_SIZE);
ram.load(BOOT_ADDRESS, data);
processor.jump(BOOT_ADDRESS);
processor.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

public class HardDrive {

public byte[] read(long lba, int size) {
System.out.println(size + " bits is reading from " + lba);
return new byte[size];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

public class Memory {

public void load(long position, byte[] data) {
System.out.println("Data are loading");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.mperor.lab.java.design.pattern.structural.facade;

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

public class ComputerFacadeTest {

@Test
public void shouldAllowToRunComputerInVerySimpleWayHidingItsComplexity() {
var out = TestUtils.setTempSystemOut();
Computer pc = new ComputerFacade();
pc.start();
Assertions.assertNotNull(pc);
Assertions.assertTrue(out.all().contains("CPU is executing"));
TestUtils.resetSystemOut();
}

}
Loading