Generally, a bridge is something that is used to overcome hurdles.
For example, consider there is a lake with a bridge. For crossing the lake, you use the bridge.
You don’t want to know about the architecture of that bridge or about the raw materials that are used to construct that bridge.
This is called abstraction which means “ignoring the internal details about the implementations“.
public interface FileDownloaderInterface { public Object download(String path); public boolean store(Object object); }
public class FileDownloaderAbstractImpl implements FileDownloaderInterface { private FileDownloadImplementor fileDownloader = null; public FileDownloaderAbstractImpl(FileDownloadImplementor provider) { super(); this.fileDownloader = provider; } @Override public Object download(String path) { return fileDownloader.downloadFile(path); } @Override public boolean store(Object object) { return fileDownloader.storeFile(object); } }
public interface FileDownloadImplementor { public Object downloadFile(String path); public boolean storeFile(Object object); }
public class WindowsFileDownloadImplementor implements FileDownloadImplementor { @Override public Object downloadFile(String path) { return new Object(); } @Override public boolean storeFile(Object object) { System.out.println("File stored successfully in WINDOWS machine"); return true; } }
public class LinuxFileDownloadImplementor implements FileDownloadImplementor { @Override public Object downloadFile(String path) { return new Object(); } @Override public boolean storeFile(Object object) { System.out.println("File stored successfully in LINUX machine"); return true; } }
public class BridgeEx { public static void main(String[] args) { String os = "linux"; FileDownloaderInterface downloader = null; switch (os) { case "windows": downloader = new FileDownloaderAbstractImpl(new WindowsFileDownloadImplementor()); break; case "linux": downloader = new FileDownloaderAbstractImpl(new LinuxFileDownloadImplementor()); break; default: System.out.println("OS not supported !!"); } Object fileContent = downloader.download("some path"); downloader.store(fileContent); } }