1
edit
Changes
Bridge
,→Consequences
The '''Bridge pattern''' is intended allows you to decouple an abstraction from its implementation so both can vary independently. The Bridge pattern is also know as Handle/Body.<br/><br/>
__TOC__
== UML Structure ==
== Applicability ==
The Bridge pattern is applicable when:
<ul><ul>
<li>we want to avoid a permanent binding between an abstraction and its implementation</li>
<li>both the abstractions and their implementations should be extensible by subclassing</li>
<li>we want to hide the implementation of an abstraction completely from clients</li>
<li>we want to share an implementation among multiple objects, and this fact should be hiddent from the client</li>
<li>useful in graphic and windowing systems that need to run over multiple platforms</li>
<li>useful any time we need to vary an interface and an implementation in different ways</li>
</ul></ul>
== Participants ==
The classes and/or objects participating in this pattern are:
<li>defines the abstraction's interface</li>
<li>maintains a reference to an object of type Implementor</li>
<li>extends the interface defined by Abstraction.</li>
<li>implements the Implementor interface and defines its concrete implementation.</li>
</ul>
</ul>
== Code Examples ==
====PHP Java Bridge in Java Code====
The following code is found on "Oregon State University Open Source Lab" which illustrate the Bridge pattern. The following is a part of the code which allows user to vary the class loader. <br/>
Source: http://gentoo.osuosl.org/distfiles/php-java-bridge_2.0.8.tar.bz2<br/>
File: JavaBridge.java<br/>
<pre>
package php.java.bridge;
/**
* A bridge pattern which allows us to vary the class loader as run-time.
* The decision is based on whether we are allowed to use a dynamic
* classloader or not (loader==null).
* @see DynamicJavaBridgeClassLoader
* @see java.lang.ClassLoader
*/
public class JavaBridgeClassLoader {
DynamicJavaBridgeClassLoader cl = null;
ClassLoader scl = null;
private JavaBridge bridge;
public JavaBridgeClassLoader(JavaBridge bridge, DynamicJavaBridgeClassLoader loader) {
this.bridge = bridge;
this.cl = loader;
if(this.cl==null)
this.scl = bridge.getClass().getClassLoader();
else
cl.clear();
}
/**
* Append the path to the current library path
* @param path A file or url list, separated by ';'
* @param extensionDir Usually ini_get("extension_dir");
*/
public void updateJarLibraryPath(String path, String extensionDir) {
if(cl==null) {
bridge.logMessage("You don't have permission to call java_set_library_path()
or java_require(). Please store your libraries in the lib
folder within JavaBridge.war");
return;
}
cl.updateJarLibraryPath(path, extensionDir);
}
/**
* Only for internal use
* @return the classloader
*/
public ClassLoader getClassLoader() {
if(cl!=null) return (ClassLoader)cl;
return scl;
}
/**
* reset loader to the initial state
*/
public void reset() {
if (cl!=null) cl.reset();
}
/**
* clear all loader caches but
* not the input vectors
*/
public void clearCaches() {
if (cl!=null) cl.clearCaches();
}
/**
* Load a class.
* @param name The class, for example java.lang.String
* @return the class
* @throws ClassNotFoundException
*/
public Class forName(String name) throws ClassNotFoundException {
if(cl==null) return Class.forName(name, false, scl);
return cl.loadClass(name);
}
}
</pre>
====Real-world Sample code in C#====
This real-world code demonstrates the Bridge pattern in which a BusinessObject abstraction is decoupled from the implementation in DataObject. The DataObject implementations can evolve dynamically without changing any clients. <br/>Source: http://www.dofactory.com/Patterns/PatternBridge.aspx
<pre>
// Bridge pattern -- Real World example
====Sample Code in Java====
The following Java program illustrates is an easier to follow example of the 'shape' example given above and will outputBridge pattern:<br/>Source: http://en.wikipedia.org/wiki/Bridge_pattern
<pre>
import java.util.*;
<li>[http://www.dofactory.com/Patterns/PatternBridge.aspx DoFactory.com - Bridge Design Pattern]</li>
<li>[http://home.earthlink.net/~huston2/dp/all_uml.html Design Class Diagrams - Bridge]</li>
<li>[http://gentoo.osuosl.org/distfiles/php-java-bridge_2.0.8.tar.bz2 Oregon State University Open Source Lab - JavaBridge.java]</li>
<li>Gamma, E., Helm, R., Johnson, R., Vlissides, J. (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2 </li>
</ul>