CSC/ECE 517 Summer 2008/wiki1 5 a5: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:
providing hooks, and supporting full AOP, and why is it more convenient to program this way in Ruby than Java? Give a few code sequences to justify your conclusions.
providing hooks, and supporting full AOP, and why is it more convenient to program this way in Ruby than Java? Give a few code sequences to justify your conclusions.


==Hooks==
== Hooks ==
   
   
There is a strong desired from developers to have control on object life cycle. They
There is a strong desired from developers to have control on object life cycle. They
Line 11: Line 11:
making big changes in object model.  
making big changes in object model.  
   
   
===Hooks Implementation===
=== Hooks Implementation ===
   
   
Most of dynamic languages provide some ways to execute custom code at different
Most of dynamic languages provide some ways to execute custom code at different
steps of object life cycle. Most of them built this functionality as part of
steps of object life cycle. Most of them built this functionality as part of
language design. They use interceptor design pattern {} as implementation guideline.
language design. They use interceptor design pattern {} as implementation guideline.
=== Hooks in Ruby ===
Ruby {} is a dynamic and pure object oriented language. It has a very strong support
for metaprogramming {}. It provides system hooks for monitoring events like object
creation. The technique use by Ruby to provide this functionality is a simple
example of interceptor design pattern.  By intercepting calls to system classes
developer could modify the system behavior without changing application code. 
Example Code:
Following is an example code from Programming Ruby book {} for hooking objects
creation event. This code modifies two Ruby system classes (Class and Object).  It
renames and redefines the Class new method and modifies the Object class to store
timestamp.

Revision as of 00:20, 5 June 2008

Introduction

Ruby has hooks that allows trapping a certain event (e.g., object creation) and running a particular code sequence whenever the event occurs. There's no comparable facility in Java. But both Ruby and Java have support for aspect-oriented programming (AspectR and AspectJ, respectively). What's the difference between simply providing hooks, and supporting full AOP, and why is it more convenient to program this way in Ruby than Java? Give a few code sequences to justify your conclusions.

Hooks

There is a strong desired from developers to have control on object life cycle. They would like to know when an object is created or destroyed, or some specific method is executed. They would like to monitor what application is doing inside and look for possibility to change application behavior or add new functionality without making big changes in object model.

Hooks Implementation

Most of dynamic languages provide some ways to execute custom code at different steps of object life cycle. Most of them built this functionality as part of language design. They use interceptor design pattern {} as implementation guideline.

Hooks in Ruby

Ruby {} is a dynamic and pure object oriented language. It has a very strong support for metaprogramming {}. It provides system hooks for monitoring events like object creation. The technique use by Ruby to provide this functionality is a simple example of interceptor design pattern. By intercepting calls to system classes developer could modify the system behavior without changing application code.

Example Code:

Following is an example code from Programming Ruby book {} for hooking objects creation event. This code modifies two Ruby system classes (Class and Object). It renames and redefines the Class new method and modifies the Object class to store timestamp.