Let decorated Commands take over

An exercise in software maintenance

1 2 Page 2
Page 2 of 2

This pattern is not just restricted to adding business functionality. Nonfunctional and orthogonal concerns can also be implemented with this pattern. For example, if a bug generated exorbitant negative charges, a logging decorator could be devised to transparently add appropriate logging and tracing statements around all the calculators in the system. Once the bug had been found, the decorator could be removed, without affecting the rest of the application. Other uses could be in the areas of performance monitoring, security, and transaction management. Better means of achieving these outcomes may be available through aspect frameworks, containers, and so on, but sometimes your maintenance constraints may prohibit their adoption.

Conclusion

In summary, the benefits derived from using the Decorator pattern to wrap and build upon commands are:

  • The existing code base doesn't change and the amount of code added is minimized and isolated.
  • Decorator existence is transparent to the class's original clients, as they are unaware of the change. The class clients are oblivious to which command they actually use.
  • The tax calculator is usable across all calculators in the system.
  • The tax calculator is completely independent from the existing calculator and, hence, can be tested independently.

In conclusion, I have shown you how to use the Decorator pattern with the Command pattern in a modular, transparent, and highly testable fashion to assist in making your code maintenance jobs easier. Hopefully this will keep you happy until you find that next green pasture.

Ricky Yim is an independent consultant with more than five years of experience in IT and more than 3 years' experience building J2EE applications. He is also experienced in building professional solutions in the telecom, banking, appliances, and gaming industries. He is a certified Java Programmer, Developer, and Web Component Developer and has a bachelor's degree in information technology from the University of Technology, Sydney, Australia. He is a big Timberwolves fan and is just staying in the IT game until the NBA comes knocking.

Learn more about this topic

This story, "Let decorated Commands take over" was originally published by JavaWorld.

Copyright © 2004 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2