Saturday, November 13, 2010

..........Facade Pattern

Facade - The front(face) of a building,In Facade pattern a single class represent the entire system and hide the complexity of the system.


class CPU {

public void freeze() { ... }
public void jump(long position) { ... }
public void execute() { ... }

}

class Memory {

public void load(long position, byte[] data) { ... }

}

class HardDrive {

public byte[] read(long lba, int size) { ... }

}

/* Facade */

class Computer {

private CPU cpu;
private Memory memory;
private HardDrive hardDrive;

public Computer() {
this.cpu = new CPU();
this.memory = new Memory();
this.hardDrive = new HardDrive();
}

public void startComputer() {
cpu.freeze();
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
cpu.jump(BOOT_ADDRESS);
cpu.execute();
}

}

/* Client */

class You {

public static void main(String[] args) {
Computer facade = new Computer();
facade.startComputer();
}

}

We do not know anything about Computer we know only one function startComputer what is happening inside startComputer no need to worry about that.

No comments:

Followers

Link