13. Applicability
Use an existing class whose interface
does not match the requirement
- Total 17 pages - 1
14. Applicability
Use an existing class whose interface
does not match the requirement
Create a reusable class though the
interfaces are not necessary
compatible with callers
- Total 17 pages - 1
15. Applicability
Use an existing class whose interface
does not match the requirement
Create a reusable class though the
interfaces are not necessary
compatible with callers
Want to use several existing subclasses,
but it is impractical to subclass
everyone. (Object Adapter Only)
- Total 17 pages - 1
16. Class Adapter Pattern
Pros
Only 1 new object, no additional indirection
Less code required than the object Adapter
Can override Adaptee's behaviour as required
Cons
Requires sub-classing (tough for single
inheritance)
Less flexible than object Adapter
- Total 17 pages - 1
17. Object Adapter Pattern
Pros
More flexible than class Adapter
Doesn't require sub-classing to work
Adapter works with Adaptee and all of its
subclasses
Cons
Harder to override Adaptee behavior
Requires more code to implement
properly
- Total 17 pages - 1
18. Pluggable Adapters
implemented with abstract operations
- Total 17 pages - 1
19. Pluggable Adapters
implemented with delegate objects
- Total 17 pages - 1
20. Two-way Adapters
class PegAdapter: public SquarePeg,
class SquarePeg {
RoundPeg {
public:
void virtual squarePegOperation() public:
{ blah }
void virtual roundPegOperation() {
}
add some corners;
squarePegOperation();
class RoundPeg {
}
public:
void virtual squarePegOperation() {
void virtual roundPegOperation()
add some corners;
{ blah }
roundPegOperation();
}
}
}
- Total 17 pages - 1
21. Adapting Local Classes to RMI
Comparison:
Increases reusability
of local class
Improves performance
of local class
Doesn't use Java
single parent by
subclassing (uses
composition)
- Total 17 pages - 1
22. Related Patterns
Adapter can be similar to the remote
form of Proxy. However, Proxy doesn't
change interfaces.
Decorator enhances another object
without changing its interface.
Bridge similar structure to Adapter,
but different intent. Separates
interface from implementation.
- Total 17 pages - 1
23. Conclusions
Allows collaboration between classes
with incompatible interfaces
Implemented in either class-based
(inheritance) or object-based
(composition & delegation) manner
Useful pattern which promotes reuse
and allows integration of diverse
software components
- Total 17 pages - 1
24. References
Becker, Dan. Design networked applications in RMI using the Adapter design
pattern. JavaWorld Magazine, May 1999. http://www.javaworld.com/javaworld/
jw-05-1999/jw-05-networked.html
Buschmann et al. A System of Patterns: Pattern-Oriented Software Architecture.
John Wiley and Sons. Chichester. 1996
Gamma et al. Design Patterns: Elements of Reusable Object-Oriented Software.
Addison-Wesley. Boston. 1995
Nguyen, D.X. Tutorial 10: Stacks and Queues: The Adapter Pattern. Rice
University. 1999. http://www.owlnet.rice.edu/~comp212/99-fall/tutorials/10/
tutorial10.html
Whitney, Roger. CS 635 Advanced Object-Oriented Design & Programming. San
Diego State University. 2001. http://www.eli.sdsu.edu/courses/spring01/cs635/
notes/proxy/proxy.html#Heading10
Shalloway, Alan., and Trott, James R., Design Patterns Explained: A New
Perspective on Object-Oriented Design, Addison-Wesley, 2002.
Rising, Linda., The Patterns Handbook: Techniques, Strategies, and Applications,
Cambridge university Press, 1998.
- Total 17 pages - 1
30. Interactions with Native Code
Access to Java world from native code
Access to- native code from Java
Total 17 pages - 1
31. Justification
Pros:
Reuse: allows access to useful native code
Efficiency: use best language for the task
Cons:
Applets: doesn't work as they're mobile
Portability: native methods aren't portable
Extra work: javah, create shared native libs
- Total 17 pages - 1
32. HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary(quot;helloquot;);
}
public static void main(String[] args) {
new
HelloWorld().displayHelloWorld();
}
}
- Total 17 pages - 1
33. HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary(quot;helloquot;);
}
public static void main(String[] args) {
new
HelloWorld().displayHelloWorld();
}
}
- Total 17 pages - 1
34. HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary(quot;helloquot;);
}
public static void main(String[] args) {
new
HelloWorld().displayHelloWorld();
}
}
- Total 17 pages - 1
35. HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary(quot;helloquot;);
}
public static void main(String[] args) {
new
HelloWorld().displayHelloWorld();
}
}
- Total 17 pages - 1
43. Create a Shared Library
class HelloWorld {
...
System.loadLibrary(quot;helloquot;);
...
}
Compile the native code into a shared library:
(example for MS Windows Visual C++ 4.0)
cl -Ic:javainclude -Ic:javaincludewin32
-LD HelloWorldImp.c -Fehello.dll
- Total 17 pages - 1
44. Run the Program
Command:
java HelloWorld
Result:
Hello World!
Possible exceptions:
java.lang.UnsatisfiedLinkError: no hello in
shared library path at
java.lang.Runtime.loadLibrary(Runtime.java)
at java.lang.System.loadLibrary(System.java)
at java.lang.Thread.init(Thread.java)
- Total 17 pages - 1
45. Summary
Connect Java with native languages
Code reuse
Powerful
Compromise of Java safety features,
cross-platform and dynamic ability
JNI call is slow and costful
Not work well for applets
- Total 17 pages - 1
46. References
Glenn L. Vanderburg. et al., Tricks of the Java
Programming Gurus, 1996, http://docs.rinet.ru:8080/
JaTricks/
Beth Stearns, Trail: Java Native Interface, Sun
Microsystems, Inc., 2003, http://java.sun.com/docs/
books/tutorial/native1.1/
Roedy Green, JNI, The Java Native Interface, Canadian
Mind Products, 2002, http://mindprod.com/jni.html
Kong Wenyu, A Brief Introduction to Java Native
Interface, http://www.geocities.com/kongwenyu/jni.html
- Total 17 pages - 1
Java Native Interface provides an means of interacting with native platform classes and functions through the creation of shared libraries.
JNI can be used with most languages such as C, C++, Fortran, Cobol, etc...
Full two-way interaction.
Native code can interact with Java objects and their methods. Can also generate and throw exceptions that can be handled by Java code.
Java code has full access to functionality contained with the native code through the generated interface definitions.
Full passing of primitive parameters after conversion (strings to UTF)
There is a ton of existing useful code in “native” languages:
Numerical analysis libraries like LAPACK and BLAS in Fortran
Lots of business logic contained with Cobol code
Much high-speed code is written in C or assembly
Keyword 'native' used to indicate methods that are available through shared libraries of native code.
Loading the library:
On Unix, looks for “libhello.so” or similar
On Win, looks for “hello.dll”
Show how to generate library later...
Once library is loaded, can call native methods defined earlier.
Finally, compile the class with:
javac HelloWorld.java
Autogenerated with call to:
javah -jni HelloWorld
Signature indicates no parameters being passed and void return clause.
The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions.
JNIENV is an interface pointer used to invoke JNI calls.
jobject is the Java object on which the call is invoked.
The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions.
JNIENV is an interface pointer used to invoke JNI calls.
jobject is the Java object on which the call is invoked.
The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions.
JNIENV is an interface pointer used to invoke JNI calls.
jobject is the Java object on which the call is invoked.
The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions.
JNIENV is an interface pointer used to invoke JNI calls.
jobject is the Java object on which the call is invoked.
The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions.
JNIENV is an interface pointer used to invoke JNI calls.
jobject is the Java object on which the call is invoked.