Recent Posts

Thursday, 6 October 2016

Bridge Design Pattern Tutorial


Bridge Design Pattern
     Bridge design pattern is one of the Structural design pattern. This is  is used to decouple a class into two parts abstraction and it’s implementation. So that both can evolve in future without affecting each other. It increases the loose coupling between class abstraction and it’s implementation.
Example
FileUploaderAbstraction.java
package com.ashok.designpatterns.bridge;

public interface FileUploaderAbstraction {
   public Object upload(String path);

   public boolean store(Object object);
}
FileUploaderAbstractionImpl.java
package com.ashok.designpatterns.bridge;

public class FileUploaderAbstractionImpl implements FileUploaderAbstraction {

   private FileUploadImplementor provider = null;

   public FileUploaderAbstractionImpl(FileUploadImplementor provider) {
      super();
      this.provider = provider;
   }

   @Override
   public Object upload(String path) {
      return provider.uploadFile(path);
   }

   @Override
   public boolean store(Object object) {
      return provider.storeFile(object);
   }
}
FileUploadImplementor.java
package com.ashok.designpatterns.bridge;

public interface FileUploadImplementor {
   public Object uploadFile(String path);

   public boolean storeFile(Object object);
}
MacFileUploadImplementor.java
package com.ashok.designpatterns.bridge;

public class MacFileUploadImplementor implements FileUploadImplementor {
   @Override
   public Object uploadFile(String path) {
      return new Object();
   }

   @Override
   public boolean storeFile(Object object) {
      System.out.println("File upload successfully in Mac !!");
      return true;
   }
}
WondowsFileUploadImplementor.java
package com.ashok.designpatterns.bridge;

public class WondowsFileUploadImplementorimplements FileUploadImplementor {
   @Override
   public Object uploadFile(String path) {
      return new Object();
   }

   @Override
   public boolean storeFile(Object object) {
      System.out.println("File upload successfully in Windows !!");
      return true;
   }
}
TestBridgeDesignPattern.java
package com.ashok.designpatterns.bridge;

import java.util.Scanner;

public class TestBridgeDesignPattern {
   public static void main(String[] args) {
      Scanner scan = null;
      try {
         scan = new Scanner(System.in);
         System.out.print("Enter OS : ");
         String os = scan.next();
         FileUploaderAbstraction uploader = null;

         switch (os) {
            case "Windows":
               uploader = new FileUploaderAbstractionImpl(new WindowsFileUploadImplementor());
               break;

            case "Mac":
               uploader = new FileUploaderAbstractionImpl(new MacFileUploadImplementor());
               break;

            default:
               System.out.println("OS not supported !!");
         }

         Object fileContent = uploader.upload("/uploads/sample/");
         uploader.store(fileContent);
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (null != scan) {
            scan.close();
         }
      }
   }
}
Enter OS : Mac
File upload successfully in Mac !!

Next Tutorial  Composite Design Pattern Tutorial

Previous Tutorial  Adapter Design Pattern Tutorial

No comments:

Post a Comment