File tree Expand file tree Collapse file tree 6 files changed +85
-0
lines changed
main/java/pl/mperor/lab/java/design/pattern/structural/facade
test/java/pl/mperor/lab/java/design/pattern/structural/facade Expand file tree Collapse file tree 6 files changed +85
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package pl .mperor .lab .java .design .pattern .structural .facade ;
2+
3+ public interface Computer {
4+
5+ void start ();
6+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments