★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/JavaOO/index3.htmlNouvelle recherche | Portail wanayoo
80211 Planet

The Event for Search Engine Marketing & Optimization


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Tech Magazines - FREE
Track Defects Online
Hacker's Secrets
Register Domains
Business Search
Website Monitoring
Send a Press Release
Best Deals on PDAs!
Reach IT Professionals
Build an Online Store
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


Tutorials : Fine-tuning Abstraction :

Fine-tuning Abstraction

by Samudra Gupta

In the past two articles about application design, I explained three vital design techniques: the Open-Closed Principle, Liskov’s Substitution Principle, and the Dependency Inversion Principle. These principles address the creation of a more flexible design level capable of withstanding the growing pressure of scalability.

In discussing those principles we arrived at design solutions that are based on abstraction. Thus, one of the salient features of a well-designed system is the abstract coupling of different objects interacting with each other. A few points about abstraction remain to be discussed.

Namely:

  • To what level should abstraction take place?
  • When is the correct level achieved?

I'll try to answer these questions utilizing the Composite Reuse Principle and the Interface Segregation Principle.

Composite Reuse Principle

The Composite Reuse Principle (CRP) is a source of never-ending debate amongst designers. The basic idea of this principle is to favor composition over inheritance as a way of achieving polymorphism.

Polymorphism, in Object Oriented terminology, means that any particular class can exist as multiple distinct sub-classes (sub-types). The following is an example of polymorphism:

abstract class Animal {
  abstract void talk();
}

Class Dog extends Animal {
  public void talk() {
    System.out.println("Scooby dooby Doo");
  }
}

Class Cat extends Animal {
  public void talk() {
    System.out.println("Meow....");
  }
}


Animal is the super class, and it can exist in the form of either a Dog or a Cat. When objects exist in the real world, they exhibit some behavior to the external world. Thus, the Dog and Cat objects expose the behavior of talking and, as they talk differently, they are polymorphic.

The above example demonstrates how polymorphism can easily be achieved with inheritance. However, inheritance based polymorphism often is ineffective, as an explosion of subtypes may cause the system to run out of flexibility. Take a look at the following example:

Imagine that you have been assigned the task of designing a payroll system for an organization. As Christmas is near, your first job is to make some provision for employee bonus payments . The company has three types of employees: Permanent, Temporary and Part-time.

In your initial design, you have considered each employee to be a polymorph of the base type Employee. You have also determined that the bonus for all employees is calculated in a similar manner and defined the calculateBonus() method in the Employee class. The other operations, such as leave and insurance premium calculations, are specific to the type of Employee. Thus those methods are declared as abstract in the super class and provided the implementation in the specific Employee classes. The initial design is shown in Figure 1.


Figure 1: The initial Employee class hierarchy

Later, the project manager changed the rate at which part-time employee bonuses should be calculated. To accomplish that, simply override the calculateBonus() method in the PartTime class.

The manager is impressed. Then he asks that the company consultants get bonuses, calculated at the same rate as part-time employees.

  • You could create a new class called Consultant and make it a sub-class of PartTime to inherit the calculateBonus() implementation. However, this causes a problem with the class hierarchy, because Consultants are also Employees. The class hierarchy will always say that Consultants are part time employees, which they aren't. In addition, if on a future date, the Consultant receives a Permanent employee bonus, the hierarchy will not withstand the change.
  • Another possibility would be to override the calculateBonus() method in the Consultant class, and copy the same implementation of PartTime class there. However, duplicate code is not the reusability we desire.

The original class structure is highly limiting when new parameters are added. Lets now examine the following solution and explanation.

To date, we've made a fundamental assumption that the bonus calculation is a frozen arithmetic function and pushed it to the super class. In reality, the calculation of bonus can change algorithms more frequently, and each type of employee can have a unique bonus calculation algorithm. Inheritance is only applicable in the context of a generalized relationship where the sub-type is a super-type. Or, in other words, there is an ISA relationship. In Liskov’s Substitution Principle, the main criterion for the ISA relationship is whether the sub-class exposes the same behavior as the super-class. Each time one has to override the methods from the super class, one violates this principle and the super-class becomes a specialized version of the sub-class rather than a generalized version of the sub-class. Further, each time that occurs one runs the risk of having the same problem we are faced with in the previous example.

The elegant solution is to define an abstract BonusCalculator and attach the appropriate BonusCalculator instance to each Employee instance. The following design depicts the proposed solution (Figure 2):


Figure 2: The CRP based Employee class hierarchy

The diagram above demonstrates that the BonusCalculator is a composite of all the different Employee classes, and thus an extremely flexible polymorphism has been achieved. The algorithm can be changed at any time for bonus calculation by attaching a different implementation of the BonusCalculator to any of the Employee objects.

This is where Composition is a superior choice. Inheritance ties you to a particular implementation or forces you to give up the original idea of generalization by having to constantly override the super-class methods in each sub-class. This is, however, not exactly a limitation of inheritance but more of an issue with how the inheritance is applied. As you gain experience, you will find that utilizing CRP is a safer approach.


Samudra Gupta has six years of Java related application development experience. He has been involved in various research based projects in Java including e-commerece based and application design and development projects. He is based in the United Kingdom. In his freetime, he is a columnist in different Java Magazines and Journals and loves to play contract bridge.


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
J2EE Deployment Specification
Why is it that J2EE applications written in the same language, Java, may not coexist on different servers? While the answer isn't simple Sun has set out to alleviate these problems with the "J2EE Deployment Specification".
... [more articles]

New Tutorial:
Designing Packages for Stability
Are your designs stable? This month Samudra will focus on the change impact relationship involved in designing classes showing us how to take them into account when designing the package relationship.
... [more Tutorials]

Year in Review:
The Best of 2002
The year 2002 saw 201 new applets posted to JavaBoutique. Check out the 2 most popular apps from each month of 2002!
... [popular applets]

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

internet.com logo




Developer News

SCO Shuts Down German Site

Salesforce.com Unleashes 'sforce'

OASIS and RosettaNet Set Standards Alliance



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



ROthello
This is an Artificial Intelligence applet which plays Othello. OTHELLO has 2 player white and black.
Download of the Week
BSCOutline v5.10
BSCOutline Java Outline Applet is a customizable tree view control for use in Web pages, similar to that used by Windows File Manager or Explorer. BSCOutline has been written to be the simple to use and small to store.

JB User Poll
How does .NET fit into your programming?
The JavaBoutique Top 15:
1. ChompText
2. PingPong
3. DJPopMenu
4. NavBar
5. PulseText
6. RushHour
7. 3DMaze
8. AScroll2
9. AnimatedTreeControl
10. Encyclo
11. AnimLetters_anim
12. Nibbly
13. Distorter
14. Viewer
15. BMMenu
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Verizon Ordered to Reveal Names of Music Pirates
The U.S. Court of Appeals Wednesday afternoon rejected Verizon's for request for a stay in earlier court ruling ordering the telecom giant to turn over the names of two subscribers suspected of illegally downloading copyrighted music.

E-mail Archiving Emerges as Critical Function
In today's corporate world, e-mail holds a company's critical information. Now a growing number of businesses are realizing that they need to save these e-mails. And they just can't pack them away like receipts you toss in a box at home.

The Deadly Duo: Spam and Viruses, May 2003
Spam attacks increase another 6+% for the month, as the global ratio of unwanted messages in business e-mail breaks the 50% mark.


Reports on security issues of ecommerce environments

Copyright 2003 Jupitermedia Corporation All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/