• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Adapter Design Pattern
 

Adapter Design Pattern

on

  • 8,145 views

Assessing the Java Native Interface

Assessing the Java Native Interface

Statistics

Views

Total Views
8,145
Views on SlideShare
8,127
Embed Views
18

Actions

Likes
2
Downloads
199
Comments
0

2 Embeds 18

http://www.slideshare.net 17
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • Java Native Interface provides an means of interacting with native platform classes and functions through the creation of shared libraries. <br /> <br /> <br /> <br /> JNI can be used with most languages such as C, C++, Fortran, Cobol, etc... <br />
  • Full two-way interaction. <br /> <br /> <br /> <br /> Native code can interact with Java objects and their methods. Can also generate and throw exceptions that can be handled by Java code. <br /> <br /> <br /> <br /> Java code has full access to functionality contained with the native code through the generated interface definitions. <br /> <br /> <br /> <br /> Full passing of primitive parameters after conversion (strings to UTF) <br />
  • There is a ton of existing useful code in “native” languages: <br /> Numerical analysis libraries like LAPACK and BLAS in Fortran <br /> Lots of business logic contained with Cobol code <br /> Much high-speed code is written in C or assembly <br />
  • <br /> <br />
  • Keyword 'native' used to indicate methods that are available through shared libraries of native code. <br />
  • Loading the library: <br /> <br /> On Unix, looks for “libhello.so” or similar <br /> <br /> <br /> <br /> On Win, looks for “hello.dll” <br /> <br /> <br /> <br /> Show how to generate library later... <br />
  • Once library is loaded, can call native methods defined earlier. <br /> <br /> <br /> <br /> Finally, compile the class with: <br /> <br /> <br /> <br /> javac HelloWorld.java <br />
  • Autogenerated with call to: <br /> <br /> <br /> <br /> javah -jni HelloWorld <br />
  • Signature indicates no parameters being passed and void return clause. <br />
  • The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions. <br /> <br /> <br /> <br /> JNIENV is an interface pointer used to invoke JNI calls. <br /> <br /> <br /> <br /> jobject is the Java object on which the call is invoked. <br />
  • The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions. <br /> <br /> <br /> <br /> JNIENV is an interface pointer used to invoke JNI calls. <br /> <br /> <br /> <br /> jobject is the Java object on which the call is invoked. <br />
  • The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions. <br /> <br /> <br /> <br /> JNIENV is an interface pointer used to invoke JNI calls. <br /> <br /> <br /> <br /> jobject is the Java object on which the call is invoked. <br />
  • The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions. <br /> <br /> <br /> <br /> JNIENV is an interface pointer used to invoke JNI calls. <br /> <br /> <br /> <br /> jobject is the Java object on which the call is invoked. <br />
  • The JNIEXPORT and JNICALL macros are defined in jni_md.h which contains platform specific definitions. <br /> <br /> <br /> <br /> JNIENV is an interface pointer used to invoke JNI calls. <br /> <br /> <br /> <br /> jobject is the Java object on which the call is invoked. <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br />

Adapter Design Pattern Adapter Design Pattern Presentation Transcript

  • Adapter SENG 609.04 Design Patterns Winter, 2003 Presented by Ming Zhou, Guy Davis
  • Agenda Intent & Motivation  Structure  Applicability  Consequences  Known Uses  Related Patterns  References  - Total 17 pages - 1
  • What is Adapter? Change the interface of a  class into another interface which is expected by the client. A.K.A. Wrapper  - Total 17 pages - 1
  • What is Adapter? Change the interface of a  class into another interface which is expected by the client. A.K.A. Wrapper  - Total 17 pages - 1
  • What is Adapter? Change the interface of a  class into another interface which is expected by the client. A.K.A. Wrapper  - Total 17 pages - 1
  • Motivation - Total 17 pages - 1
  • Motivation - Total 17 pages - 1
  • Motivation - Total 17 pages - 1
  • Motivation 1 1 - Total 17 pages - 1
  • Structure (Class) - Total 17 pages - 1
  • Structure (Object) - Total 17 pages - 1
  • Applicability - Total 17 pages - 1
  • Applicability Use an existing class whose interface  does not match the requirement - Total 17 pages - 1
  • 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
  • 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
  • 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
  • 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
  • Pluggable Adapters implemented with abstract operations  - Total 17 pages - 1
  • Pluggable Adapters implemented with delegate objects  - Total 17 pages - 1
  • 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
  • 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
  • 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
  • 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
  • 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
  • Questions? - Total 17 pages - 1
  • Questions? - Total 17 pages - 1
  • Java Native Interface (JNI) SENG 609.04 Design Patterns Winter, 2003 Presented by Guy Davis, Ming Zhou
  • Agenda Overview  Justification  Hello World!  Summary  References  Q&A  - Total 17 pages - 1
  • JNI Overview - Total 17 pages - 1
  • Interactions with Native Code Access to Java world from native code Access to- native code from Java Total 17 pages - 1
  • 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
  • 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
  • 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
  • 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
  • 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
  • HelloWorld.h #include “jni.h” /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern “C” { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif - Total 17 pages - 1
  • HelloWorld.h #include “jni.h” /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern “C” { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif - Total 17 pages - 1
  • HelloWorld.h #include “jni.h” /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern “C” { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif - Total 17 pages - 1
  • HelloWorldImp.c #include <jni.h> #include quot;HelloWorld.hquot; #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf(quot;Hello world!nquot;); return; } - Total 17 pages - 1
  • HelloWorldImp.c #include <jni.h> #include quot;HelloWorld.hquot; #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf(quot;Hello world!nquot;); return; } - Total 17 pages - 1
  • HelloWorldImp.c #include <jni.h> #include quot;HelloWorld.hquot; #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf(quot;Hello world!nquot;); return; } - Total 17 pages - 1
  • HelloWorldImp.c #include <jni.h> #include quot;HelloWorld.hquot; #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf(quot;Hello world!nquot;); return; } - Total 17 pages - 1
  • 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
  • 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
  • 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
  • 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
  • Questions? - Total 17 pages - 1
  • Questions? - Total 17 pages - 1