Home > Java > Bridge Pattern

Bridge Pattern

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");
 }
}
Advertisements
Categories: Java Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: