Adapter

SENG 609.04 Design Patterns

         Winter, 2003



     Presented by Ming Zhou, Guy Davis
Agenda
      Intent & Motivation
  

      Structure
  

      Applicability
  

      Consequences
  

      Known Us...
What is Adapter?
    Change the interface of a


    class into another interface
    which is expected by the
    client...
What is Adapter?
    Change the interface of a


    class into another interface
    which is expected by the
    client...
What is Adapter?
    Change the interface of a


    class into another interface
    which is expected by the
    client...
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...
Applicability
    Use an existing class whose interface


    does not match the requirement
    Create a reusable class ...
Applicability
  Use an existing class whose interface


  does not match the requirement
 Create a reusable class though...
Class Adapter Pattern
    Pros


        Only 1 new object, no additional indirection
    


        Less code required ...
Object Adapter Pattern
      Pros
  

          More flexible than class Adapter
      


          Doesn't require sub-...
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 {
    ...
Adapting Local Classes to RMI
Comparison:
 Increases reusability

  of local class
 Improves performance

  of local cla...
Related Patterns
    Adapter can be similar to the remote


    form of Proxy. However, Proxy doesn't
    change interfac...
Conclusions
    Allows collaboration between classes


    with incompatible interfaces
    Implemented in either class-b...
References
    Becker, Dan. Design networked applications in RMI using the Adapter design




    pattern. JavaWorld Maga...
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
  

     ...
JNI Overview




          - Total 17 pages -   1
Interactions with Native Code



      Access to Java world from native code




      Access to- native code from Java
  ...
Justification
Pros:
 Reuse: allows access to useful native code
 Efficiency: use best language for the task
Cons:
 Appl...
HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
    System.loadLibrary(quot;helloquot;...
HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
    System.loadLibrary(quot;helloquot;...
HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
    System.loadLibrary(quot;helloquot;...
HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
    System.loadLibrary(quot;helloquot;...
HelloWorld.h
#include “jni.h”
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
...
HelloWorld.h
#include “jni.h”
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
...
HelloWorld.h
#include “jni.h”
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
...
HelloWorldImp.c

#include <jni.h>
#include quot;HelloWorld.hquot;
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWor...
HelloWorldImp.c

#include <jni.h>
#include quot;HelloWorld.hquot;
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWor...
HelloWorldImp.c

#include <jni.h>
#include quot;HelloWorld.hquot;
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWor...
HelloWorldImp.c

#include <jni.h>
#include quot;HelloWorld.hquot;
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWor...
Create a Shared Library
class HelloWorld {
   ...
        System.loadLibrary(quot;helloquot;);
   ...
}

 Compile the nati...
Run the Program
Command:

 java HelloWorld
Result:

 Hello World!

Possible
exceptions:

 java.lang.UnsatisfiedLinkError: ...
Summary
    Connect Java with native languages
    Code reuse
    Powerful
    Compromise of Java safety features,


    ...
References
 Glenn L. Vanderburg. et al., Tricks of the Java
    Programming Gurus, 1996, http://docs.rinet.ru:8080/
    J...
Questions?




             - Total 17 pages -   1
Questions?




             - Total 17 pages -   1
Upcoming SlideShare
Loading in …5
×

Adapter Design Pattern

8,055
-1

Published on

Assessing the Java Native Interface

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,055
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
294
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
















































  • 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 &#x201C;native&#x201D; 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 &#x201C;libhello.so&#x201D; or similar



    On Win, looks for &#x201C;hello.dll&#x201D;



    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.










  • Adapter Design Pattern

    1. 1. Adapter SENG 609.04 Design Patterns Winter, 2003 Presented by Ming Zhou, Guy Davis
    2. 2. Agenda Intent & Motivation  Structure  Applicability  Consequences  Known Uses  Related Patterns  References  - Total 17 pages - 1
    3. 3. 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
    4. 4. 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
    5. 5. 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
    6. 6. Motivation - Total 17 pages - 1
    7. 7. Motivation - Total 17 pages - 1
    8. 8. Motivation - Total 17 pages - 1
    9. 9. Motivation 1 1 - Total 17 pages - 1
    10. 10. Structure (Class) - Total 17 pages - 1
    11. 11. Structure (Object) - Total 17 pages - 1
    12. 12. Applicability - Total 17 pages - 1
    13. 13. Applicability Use an existing class whose interface  does not match the requirement - Total 17 pages - 1
    14. 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. 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. 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. 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. 18. Pluggable Adapters implemented with abstract operations  - Total 17 pages - 1
    19. 19. Pluggable Adapters implemented with delegate objects  - Total 17 pages - 1
    20. 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. 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. 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. 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. 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
    25. 25. Questions? - Total 17 pages - 1
    26. 26. Questions? - Total 17 pages - 1
    27. 27. Java Native Interface (JNI) SENG 609.04 Design Patterns Winter, 2003 Presented by Guy Davis, Ming Zhou
    28. 28. Agenda Overview  Justification  Hello World!  Summary  References  Q&A  - Total 17 pages - 1
    29. 29. JNI Overview - Total 17 pages - 1
    30. 30. Interactions with Native Code Access to Java world from native code Access to- native code from Java Total 17 pages - 1
    31. 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. 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. 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. 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. 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
    36. 36. 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
    37. 37. 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
    38. 38. 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
    39. 39. 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
    40. 40. 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
    41. 41. 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
    42. 42. 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
    43. 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. 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. 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. 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
    47. 47. Questions? - Total 17 pages - 1
    48. 48. Questions? - Total 17 pages - 1
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×