2. Agenda
● Motivation
● Run-Time Type Information
● Reflection
● ClassLoader
● Proxy Pattern
● Dynamic Proxy
● Example
● Summary
3. Motivation
Since we want to integral all of player in our
'AVMMediaPlayer' framework, then need to specified
framework API according by customer's requirement but
don't want much maintain cost...
Interface 1
ImpIementation
Interface 2 Interface 3 Interface 4
API 1 API 1API 2
4. Motivation
Problem:
1. Implementation's type is same to API.
2. Implementation is not implement interface directly.
3. That is strange of architecture.
Interface 1
ImpIementation
Interface 2 Interface 3 Interface 4
API 1 API 2
5. Run-Time Type Information
This mechanism can exposes information about an object's
data type at run-time. The C++ run-time type information
permits performing safe typecasts and manipulate type
information at run time.
Parent
Derived
Parent *p = new Derived;
Derived *d = dynamic_cast<Derived*>(p);
Ref: Wikipedia - http://en.wikipedia.org/wiki/Run-time_type_information
6. Reflection
Reflection is the ability of a computer program to examine
and modify the structure and behavior (specifically the
values, meta-data, properties and functions) of an object at
run-time.
Ref: Wikipedia - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
Object Data or Method
7. Reflection (cont.)
A language supporting reflection provides a number of features
available at run-time that would otherwise be very obscure to
accomplish in a lower-level language. Some of these features are
the abilities to:
● Discover and modify source code constructions (such as code
blocks, classes, methods, protocols, etc.) as a first-class object
at run-time.
● Convert a string matching the symbolic name of a class or
function into a reference to or invocation of that class or
function.
● Evaluate a string as if it were a source code statement at run-
time.
● Create a new interpreter for the language's byte-code to give a
new meaning or purpose for a programming construct.
Ref: Wikipedia - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
8. Reflection (cont.)
For example:
// Without reflection
new Foo().hello();
// With reflection
Class<?> clazz = Class.forName("Foo");
clazz.getMethod("hello").invoke(clazz.newInstance());
Ref: Wikipedia - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
ObjectData or
Method
9. ClassLoader
The Java Classloader is a part of the Java Runtime Environment
that dynamically loads Java classes into the Java Virtual Machine.
Usually classes are only loaded on demand. The Java run time
system does not need to know about files and file systems because
of class loaders.
When the JVM is started, three class loaders are used:
● Bootstrap class loader (loads the core Java libraries located in the
<JAVA_HOME>/lib directory)
● Extensions class loader (loads the code in the extensions directories,
<JAVA_HOME>/lib/ext, or any other directory specified by the java.ext.dirs system
property)
● System class loader (loads code found on java.class.path, which maps to the
system CLASSPATH variable.)
Ref: Wikipedia - http://en.wikipedia.org/wiki/Java_Classloader
10. ClassLoader (cont.)
Briefly speaking the class loader is:
Class byte code => 'Class' object (class's reference)
Class A bytecode bytecode
Java
Compiler
Java
Classloader
11. Proxy Pattern
A proxy, in its most general form, is a class functioning as an interface to
something else. The proxy could interface to anything: a network connection, a
large object in memory, a file, or some other resource that is expensive or
impossible to duplicate.
Ref: Wikipedia - http://en.wikipedia.org/wiki/Proxy_pattern
12. Dynamic Proxy
Proxy is not Interface's instance, is reflecting to concrete interface's
implementation dynamically.
<<interface>>
Subject
RealSubject
Client
Dynamic Proxy
delegate
class
loader
class
13. Example
Independent with API and Implementation.
Interface 1
ImpIementation
Interface 2 Interface 3 Interface 4
API 1 API 2
Dynamic Proxy
14. Summary
● RTTI is saving type information at run time.
● Reflction mechanism can let we know a Object at run
time.
● In order to use a Object at run time need class loader.
● Achieve dynamic proxy.