Wednesday, May 9, 2007

Reflection programming model

I have been hearing a lot about the Reflection programming model.
The initial impression has been that how could a program modify itself and
where in the world does a software engineer need this ?

So here goes my hunt to demystify the myth behind this Reflection pattern.

Reflection is the ability to determine the behavior of your program at runtime.
Whats great? I can have many if conditions to get this done...
Yes you can change the behavior of the program with if clauses, but you cant dynamically determine what function to call without knowing the function name at compile time.

Reflection provides you a way to retrieve the name of the data members and member functions of a class and invoke them selectively at run time.
This is a read only reflection which is provided by Java.

Some languages (interpreters like PHP ) provides you an ability to add data members and member functions to a class at runtime.

In C a minimal set of reflection features can be obtained by dynamic libraries. A function can be looked up in the shared libraries using dlopen, dlsym and invoked.

The advantage Less code modification.


References :

1. http://en.wikipedia.org/wiki/Reflection_%28computer_science%29

For people familiar with php
http://php.corp.yahoo.com/manual/en/language.oop5.reflection.php

No comments: