Observable

Defines a one to many relationship between the Subject (the object being observed) and the Observers where the Observers are notified of changed state in the Subject.

The problem

We have a Temperature object and we want to log the changes to the temperature.\\ Using OOPS we would have to modify the Temperature class.\\ Using AOP we can Introduce new behaviour without touching the original class.

We already have a logging utility class and we don't need to change that either.\\ There is no need for it to know about the Temperature class.\\ Again we can Introduce new behaviour.

Implementation

The jboss implementation of the pattern

Existing Code

The exisiting application that knows nothing about the Observable pattern

Integration Code

To integrate the two classes we write an Introduction for LogUtil that tells it log the changes in Subjects.

Configuration

Now we use AOP to tie it all together

The test class

The output

It doesn't look very exciting but ...
Received Notification: Temperature=10

Conclusion

Things to try