Sunday, March 27, 2005
Wednesday, March 16, 2005
Authored By Monish
Aspect-oriented programming
AOP is an extension of OOP but NOT limited by it.
Aspect = appearance to the eye or mind . This is a synonym of perception.
Example : red color - this is just a particular light frequency - however humans discern it distinctly from other
frequencies w/o an instrument to measure the wavelength or frequency.
another example : the common phrase "the sun is rising "- this is NOT factually true. We imagine that it is rising.
In programming: An aspect is part of a program that arises between different business logic(s).For example:let's say
we need to check if a user is logged in before executing different methods - where each method maybe different sets
of business logic.Further assume that each method is a static call i.e no states are saved between methods - then we
have an aspect that cuts across different concerns( business logic). NOTE: traditionally the code for this call is
put in one place - however , all these different methods have to call this common code before executing themselves.
With AOP you may write this code in one place - which is no different from usual programming technique,but the Aspect
compiler takes care of calling the code when required. Thus ,it further decouples the business logic from
administrative , security and other auxillary logic.It smacks of triggers in db procedure calls - where one can define
a set of functions that must execute before , after and around a certain event execution.
The other side of aspect programming is : weaving.When you have various aspects defined - like security aspects,alert
aspects etc , you want to now weave them into a single whole w/ the system.This makes the system coherent.Traditionally,
the weaving process is transparent to the aspect oriented programmer.It's like the creation of server side and client
side stubs by a compiler like rmi. The stubs are complex routines , since they need to know about their environment ,
sockets , ports , protocol and connect the code and data in network programming.However, these are the same for
different sets of business logic.
On a more personal note : I've always wanted to do something like AOP when i write code.
And i am sure you'll agree w/ me when you see the following code fragment :
Java example:
.....
public String getData(Date fromDate,Date toDate)
{
try
{
validateDate(fromDate);
//gosh -again?
validateDate(toDate);
//call db w/ from and to date
....
}
catch(InvalidDateException e)
{
askForProperDateValue();
}
}
public void setFromDate(Date fromDate) throws InvalidDateException
{
//and again??
validateDate(fromDate);
this.fromDate = fromDate;
}
public void setToDate(Date toDate) throws InvalidDateException
{
//and again??
validateDate(toDate);
this.toDate = toDate;
}
....
public boolean validateDate(Date date) throws InvalidDateException
{
//validate the date using some obscure logic :-)
if ( date.isNotValid())
throw InvalidDateException();
return true;
}
C Example:
.....
data_t get_data(char* from_date,char* to_date)
{
/** i'm checking if the date is valid all the time .. doosh ! **/
if( validate_date(from_date) == 0 && validate_date(to_date) ==0)
{
}
else
{
.....
}
........
}
void set_from_date(char* from_date)
{
/** and here too **/
if (validate_date(from_date) == 0 )
{
/** set the from date **/
state_t.from_date = from_date;
}
else
lm_message(....);
}
void set_to_date(char* to_date)
{
/** and here again -damn !!**/
if (validate_date(to_date) == 0 )
{
/** set the to date **/
state_t.to_date = to_date;
}
else
lm_message(....);
}
....
int validate_date(char* date)
{
int result = VALID_DATE_FLAG;
/** logic to validate date here **/
....
....
return result;
}
AOP addresses the above problem by having point cuts that allow you to call all the validate logic
in one place. What this means is your code will NOT keep trying to validate the date inside the code,
but will do so w/ the use of AOP "advices" like : before , after and around .
Excited ? Read about it then - esply Resources 2 for OOP developers and maybe the PPT for C-coders
Resources:
- Wikipedia link
- Hands-on AOP
- Interview with Gregor Kiczales
- SPL Projects
- AspectC
- [PPT]Using AspectC to improve modularity...
Wednesday, March 02, 2005
convert date-time to UTC seconds ( local time zone used )
$date --date='2005-02-15 09:45:58 ' +%s
1108489558
convert UTC seconds to date-time
$date -d '1970-01-01 UTC 1109833507 seconds' +"%Y-%m-%d %T %z"
2005-03-02 23:05:07 -0800
$date --date='2005-02-15 09:45:58 ' +%s
1108489558
convert UTC seconds to date-time
$date -d '1970-01-01 UTC 1109833507 seconds' +"%Y-%m-%d %T %z"
2005-03-02 23:05:07 -0800