Java Design Patterns Bridge Patterns Example 1

Introduction

Example for the Bridge pattern

The following code shows how to divide complex behavior among two classes - the abstraction and the implementation.

BaseList provides core functionality, while the UnorderedList expands on the model by adding a list character.

The OrderedHtmlList class provides the underlying storage capability for the list, and can be flexibly paired with either of the classes which provide the abstraction.

import java.util.ArrayList;
import java.util.List;


interface HtmlList {
   public void addItem(String item);

   public void addItem(String item, int position);

   public void removeItem(String item);

   public int getNumberOfItems();

   public String getItem(int index);

   public boolean supportsOrdering();
}

class BaseList {//from w  ww.j a  v  a2s  .  co  m
   protected HtmlList implementor;

   public void setImplementor(HtmlList impl) {
      implementor = impl;
   }

   public void add(String item) {
      implementor.addItem(item);
   }

   public void add(String item, int position) {
      if (implementor.supportsOrdering()) {
         implementor.addItem(item, position);
      }
   }

   public void remove(String item) {
      implementor.removeItem(item);
   }

   public String get(int index) {
      return implementor.getItem(index);
   }

   public int count() {
      return implementor.getNumberOfItems();
   }
}

class NumberedList extends BaseList {
   public String get(int index) {
      return (index + 1) + ". " + super.get(index);
   }
}

class OrderedHtmlList implements HtmlList {
   private List<String> items = new ArrayList<>();

   public void addItem(String item) {
      if (!items.contains(item)) {
         items.add(item);
      }
   }

   public void addItem(String item, int position) {
      if (!items.contains(item)) {
         items.add(position, item);
      }
   }

   public void removeItem(String item) {
      if (items.contains(item)) {
         items.remove(items.indexOf(item));
      }
   }

   public boolean supportsOrdering() {
      return true;
   }

   public int getNumberOfItems() {
      return items.size();
   }

   public String getItem(int index) {
      if (index < items.size()) {
         return items.get(index);
      }
      return null;
   }
}

class UnorderedList extends BaseList {
   private char itemType;

   public char getItemType() {
      return itemType;
   }

   public void setItemType(char newItemType) {
      if (newItemType > ' ') {
         itemType = newItemType;
      }
   }

   public String get(int index) {
      return itemType + " " + super.get(index);
   }
}

public class Main {
   public static void main(String[] arguments) {
      HtmlList implementation = new OrderedHtmlList();

      BaseList listOne = new BaseList();
      listOne.setImplementor(implementation);

      listOne.add("One");
      listOne.add("Two");
      listOne.add("Three");
      listOne.add("Four");
      System.out.println();

      UnorderedList listTwo = new UnorderedList();
      listTwo.setImplementor(implementation);
      listTwo.setItemType('+');

      NumberedList listThree = new NumberedList();
      listThree.setImplementor(implementation);
      System.out.println();

      for (int i = 0; i < listOne.count(); i++) {
         System.out.println(listOne.get(i));
      }
      System.out.println();

      System.out.println("Printing out second list (UnorderedList)");
      for (int i = 0; i < listTwo.count(); i++) {
         System.out.println(listTwo.get(i));
      }
      System.out.println();

      System.out.println("Printing our third list (NumberedList)");
      for (int i = 0; i < listThree.count(); i++) {
         System.out.println(listThree.get(i));
      }
   }
}



PreviousNext

Related