aspose file tools
The moose likes Java in General and the fly likes Object Oriented Design: Abstract Class vs. Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Object Oriented Design: Abstract Class vs. Interface" Watch "Object Oriented Design: Abstract Class vs. Interface" New topic
Author

Object Oriented Design: Abstract Class vs. Interface

Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
I am currently designing an e-commerce application and am trying to devise the best design structure for the product.

1) I have a notion of a GenericProduct that can be used to do many things. The most simple use of of this class is for insertion into a web site.

2) I also have supplier specific product such as AcmeProduct that extend GenericProduct's capabilities to do things like add all the Acme specific attributes and stuff to the product while still leveraging polymorphism to insert any GenericProduct into the database.

3) I am currently building a sub component to the application that can insert a GenericProduct in Google Base (Google's free shopping comparison service).

4) Not all products need to go into Google Base, so I thought of creating an interface called GoogleBaseInsertableProduct that AcmeProduct will implement that will allow me to enforce a contract on all Google Base insertable products.

My issue, however, is this ....

In-order to insert a product to Google Base I need lots of GenericProduct stuff such as the ability to get the number of products in-stock, the price, the URL of product ... etc.

If, however, my AcmeProduct implements GoogleBaseInsertableProduct and I use GoogleBaseInsertableProduct to send to my Google Base insertion method, I loose the ability to call GenericProduct functions such as getQuanitity(), getSku(), getPrice() etc ....

What I really need is a GenericProduct that implements GoogleBaseInsertableProduct that I can have AcmeProduct extend.

The problem is, is that I don't want every GenericProduct to implement GoogleBaseInsertableProduct, a GenericProduct should be just that, generic, and not be forced to define Google Base specific methods.

The only thing I can think of is to do something like this ....



The problem with this, however, is lets say I want to do something else like give AcmeProduct the ability to interact with Amazon Marketplace and still be able to do things like getPrice(), getUrl() etc. ... then I will not be able to do that because AcmeProduct will forever be a GoogleBaseInsertableProduct.

One other thing I could do would be to declare all those GenericProduct methods like getPrice(), getUrl() etc. in GoogleBaseInsertableProduct and then simply do this ....



The problem with this approach is that I need to redefine all the basic GenericProduct methods such as getPrice(), getSku() ...

Perhaps this is the correct design, but it seems redundant ... no?


Here is some sample Code so that you get the gist of what I am trying to do.




Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3091
    
    5

You need to leverage interfaces a bit more. Something like:



Then any GoogleBaseProduct or AmazonProduct reference can be used as a Procuct, and any AcmeProduct can be used as a GoogleBaseProduct, and those sub-class of AmazonReadAcmeProcucts can be used as GoogleBaseProducts or AmazonProduct, or simple Product, etc...


Steve
Aryeh Golob
Ranch Hand

Joined: Nov 12, 2006
Posts: 37
Thanks Steve ... I appreciate the clear and concise breakdown.

This makes lots of sense .... cheers ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Oriented Design: Abstract Class vs. Interface
 
Similar Threads
IO NullPointerException
Sorting Arrays
How to retrive values from the database
Unknown problem with Tomcat and Axis2
What's wrong with this code?? please help