Java Reuse, Legacy Code, and Polymorphism (cont'd)

The Legacy Class
For starters, let me introduce the reuse candidate: a legacy class called Visa, which implements no interfaces or abstract classes, and exposes the following methods: setOwnerName (the owner of the Visa card), setCCNumber (the card number), setExpirationDate (the expiration date), charge (charges the credit card), and credit (credits money back to the credit card). The Visa class is shown in Figure 1.
 
Figure 1 | Example: "Visa" legacy class

Though a simplified example, a similar class should exist in most e-commerce architectures. Perhaps for the original business model, charging only a Visa card was a sufficient set of functionality, so the developers coded directly to the exact use case in all handler code (all other application code that references Visa), without much thought given to future extensibility. By addressing the specific use case, rather than an abstracted generalization of it, the chance that code can be reused without additional structural changes in the future is greatly reduced.

An Extensibility Request
Suppose a request is received to extend the application to allow charging to Master Card and American Express cards. In surveying the landscape, it is apparent that a little foresight in the original design could have made this task much easier. The most extensible applications are built on the foundation of interfaces (and/or abstract classes), and handler code utilizes the technique of polymorphism in manipulating the interface or abstract class, rather than offering code that handles each potential implementation.

Polymorphism is the ability of a class or subclass to be used in the same manner as the interface it implements or the same manner as its superclass, respectively. In this way, the handler code never has to be changed to add new implementations of the interface or abstract class—and this is what the original developers in this example failed to take into account. Quite often, it is assumed that the high price of changing a class lies in the class itself. The changes to the class in question will always be finite, and though in reality the changes to the handler code will be some countable number, in theory, the potential changes to this handler code are infinite.

If the changes needed to extend code are contained within the class in question, rather than propagated to the handling code around it, then the design is properly abstracted to easily allow changes. The current design, coded directly against the Visa class, is not. Let's alter the original design to provide a greater level of abstraction while enhancing the ability to reuse code.


Introduction

Retrofitting the Legacy Code

In this Article
Introduction Retrofitting the Legacy Code
The Legacy Class








FEATURE SOFTWARE:
dtSearch Web
Add power searching to your web site.
Buy Now!

Encrypt It
Encrypt and Decrypt Data, Passwords and Files within your application.
Buy Now!

FEATURE BOOK:
PointBase Mobile Edition
Enable local data access for mobile users.
Buy Now!
Accessibility Legacy Issues (from Java.Sun.com)

JCE 1.2 Legacy Code (from Java.Sun.com)

DevX Java Zone



Product Review of the Week
Rational Suite 2001

Site of the Week

Book Review of the Week
Object-Oriented Implementation of Numerical Methods

Tip of the Day
Use Math.PI and Math.E for Scientific Calculations

Download of the Week
Ebitec JMS Mail Bridge





 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement