Archive

Archive for January, 2016

Bridge Pattern

January 18, 2016 Leave a comment

Bridge pattern is used to decouple an abstraction from its implementation so that two independent each other. In other words, by moving the set of abstract operations to an interface, so that an abstraction will depend on an implementation of the interface.

Here is example.

Bridge Pattern

Bridge Pattern

 

  • AbstractPrinter as Abstraction to define abstraction to interface and hold reference object of type implementor.
  • ConcretePrinter as RefinedAbstraction to extend interface defined by abstraction.
  • IPrinterDocument as Implementator to define the interface for implementation classes.
  • WordPrinterDocument and PdfPrinterDocument as implementation classes.

 

AbstractPrinter.java

public abstract class AbstractPrinter {

protected IPrinterDocument printerDocument;

public AbstractPrinter(IPrintDocument printerDocument){
this.printerDocument = printerDocument;
}

public abstract void printDocument();
}

ConcretePrinter.java

public class ConcretePrinter extends AbstractPrinter {

public ConcretePrinter(IPrinterDocument printerDocument) {
super(printerDocument);
}

@Override
public void printDocument() {
printerDocument.printDocument();
}
}

IPrinterDocument.java

public interface IPrintDocument {
 public void printDocument();
}

PdfPrinterDocument.java

public class PdfPrinterDocument implements IPrinterDocument {
 
 public void printDocument() {
 System.out.println("Pdf Document is being printed");
 }
}

WorderPrintDocument.java

public class WordPrinterDocument implements IPrinterDocument {
 
 public void printDocument() {
 System.out.println("Word Document is being printed");
 }
}
Categories: Java Tags: , ,