Facade Java Designs Patterns
What is design Patterns :
Design patterns are solutions to a common problem , there are three types of design patterns .
Examples are singleton, factory,builder,factory method and abstract factory etc.
Examples are strategy , command, mediator ,observer etc.
- Structural Design Patterns
Intent of Facade Design Patterns:
Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use.
Motivation:
Medium to large scale software systems consists of several subsystems
For example:
A compiler system might consist of scanner, node builder, parser and code generator subsystems A database system consists of connections, statements, resultsets.
Applicability:
Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use.
Motivation:
Medium to large scale software systems consists of several subsystems
For example:
A compiler system might consist of scanner, node builder, parser and code generator subsystems A database system consists of connections, statements, resultsets.
Applicability:
- Make a software library easier to use and understand
- Reduce dependencies of the client code on the inner working of the library
- Wrap a poorly designed APIs with a better API
Facade class:
- knows which subsystem classes are responsible for a request
- delegates client request to appropriate subsystem objects
Subsystem classes:
- implement subsystem functionality
- handle work assigned by Facade object
- have no knowledge about facade object
- Generally implemented as Singleton
- Facade as an interface or abstract class with different concrete subclasses for different implementations of the subsystem
- Transparent vs Opaque implementations
- Should minimize exposure of subsystem classes to clients
- Façade class should not be known to the inner subsystem classes
Below is an example of facade design patterns ,Computer is facade for users. The inner functionality of Computer CPU, Memory And hard drive don't know about the computer, it just handle work assigned by the Computer. the computer know about the CPU,Memory and hard drive Classes
public class ComputerClient {
public static void main(String[] args) {
IComputerFacade cf = Computer86Facade.getInstance();
cf.startComputer();
}
}
public static void main(String[] args) {
IComputerFacade cf = Computer86Facade.getInstance();
cf.startComputer();
}
}
Below is the code example of Facade Class which implements the IcomputerFacade Interface
public class Computer86Facade implements IComputerFacade{
CPU cpu;
Memory memory;
HardDrive hardDrive;
private static final long BOOT_SECTOR = 1;
private static final int SECTOR_SIZE = 100;
private static final long BOOT_ADDRESS = 1200;
private static final Computer86Facade _instance = new Computer86Facade();
private Computer86Facade(){}
public static IComputerFacade getInstance() {
return _instance;
}
public void startComputer() {
cpu.freeze();
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
cpu.jump(BOOT_ADDRESS);
cpu.execute();
}
}
public interface IComputerFacade {
public void startComputer();
}
public void startComputer();
}
The CPU class has method freeze(),jump() and execute()
public class CPU {
public void freeze() { }
public void jump(long position) { }
public void execute() { }
}
public class HardDrive {
public byte[] read(long lba, int size) { return null; }
}
public void freeze() { }
public void jump(long position) { }
public void execute() { }
}
public class HardDrive {
public byte[] read(long lba, int size) { return null; }
}
public class Memory {
public void load(long position, byte[] data) { }
}
public void load(long position, byte[] data) { }
}
