CSC/ECE 517 Fall 2012/ch2b 2w37 ms

From Expertiza_Wiki
Revision as of 14:33, 17 November 2012 by Mstonse (talk | contribs)
Jump to navigation Jump to search

Adapter pattern and the related patterns (Bridge, Decorator, Facade)

The adapter design pattern allows the user to make changes to the existing class with other class libraries without changing the code for the existing class. The Bridge, Decorator and the Facade pattern look somewhat similar to the adapter pattern but their intent is different and that intent is what separates the above patterns from each other.

Adapter Pattern

Adapter pattern plays an important role when you want to incompatible interfaces to work together.The real world example for an adapter pattern is the travel power adapter.Different countries have different socket and plug configuration. So you can use an adapter to fit a plug into a socket that initially was not possible due to different interface designs.

Implementattion

Adapter implementation using inheritance

This method can be used when you have incompatible method that needs to be used in other class. Using inheritance a "is-a" relationship is established between the base class and super class. The new compatible methods will be contained in the inherited adapter class.

public class CylindricalSocket {
  public String supply(String cylinStem1, String cylinStem1) {
    System.out.println("Power power power...");
  }
}

public class RectangularAdapter extends CylindricalSocket {
  public String adapt(String rectaStem1, Sting rectaStem2) {
    //some conversion logic
    String cylinStem1 = rectaStem1;
    String cylinStem2 = rectaStem2;
    return supply(cylinStem1, cylinStem2);
  }
}

public class RectangularPlug {
  private String rectaStem1;
  private String rectaStem2;
  public getPower() {
    RectangulrAdapter adapter = new RectangulrAdapter();
    String power = adapter.adapt(rectaStem1, rectaStem2);
    System.out.println(power);
  }
}
Adapter implementation using composition

The other approach is to have a base class as an attribute in the adapter class. This creates an "has-a"relationship between the base class and the sub class.

public class CylindricalSocket {
  public String supply(String cylinStem1, String cylinStem1) {
    System.out.println("Power power power...");
  }
}

public class RectangularAdapter {
  private CylindricalSocket socket;

  public String adapt(String rectaStem1, Sting rectaStem2) {
    //some conversion logic
    socket = new CylindricalSocket();
    String cylinStem1 = rectaStem1;
    String cylinStem2 = rectaStem2;
    return socket.supply(cylinStem1, cylinStem2);
  }
}

public class RectangularPlug {
  private String rectaStem1;
  private String rectaStem2;
  public getPower() {
    RectangulrAdapter adapter = new RectangulrAdapter();
    String power = adapter.adapt(rectaStem1, rectaStem2);
    System.out.println(power);
  }
}
Composition or inheritance to implement Adapter Pattern?
  • Composition is preferred over inheritance since using composition it is easy to change the behavior of a class.
Adapter pattern in Java
  • java.io.InputStreamReader(InputStream)
  • java.io.OutputStreamWriter(OutputStream)


Bridge Pattern

As stated by GoF a bridge design patterns intent is to “Decouple an abstraction from its implementation so that the two can vary independently”.

UML Diagram for Bridge-Pattern
File:Wbp.jpg
Without Bridge-Pattern
With Bridge-Pattern
Bridge vs Adapter pattern
  • Two incompatable classes can be made to work together using adapter pattern.
  • Bridge pattern creates two separate hierarchies by separating the abstraction from the implementation.

Decorator Pattern

Facade Pattern