These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Design problem with interfaces and inheritance
PostPosted: Wed Aug 24, 2005 11:54 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
I have a design problem in an application that could be extended by other programmers.


Well, suppose I have to map an object customer which, in my default implementation has only two fields, name and address.

I want to define Customer as an interface:


public interface Customer {


public String getName();
public void setName(String name);


public String getAddress();
public void setAddress(String address);

}


public class CustomerImpl extends Customer {

private String name;
private String address;


public String getName() { return name; }
public void setName(String name) { this.name = name }


public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }

}


The table "customer" in the database has only two fields: name and address.




Now, suppose that someone would customize this application adding to customer a field for the phone number.

It should extends CustomerImpl in this way:


public class CustomerExt extends CustomerImpl {

private String phone;

public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }

}


In this case the database table has one more field for the phone number.

Consider that for this extended application, all customers has the phone number and there aren't customers object of type CustomerImpl: all

customer belongs to class CustomerExt.


This is a web application that uses Struts, and in all layers of my programs a customer is referenced by his interface Customer and not by

concrete class CustomerImpl: in actions, in the service layer and in the dao layer.

This is quite important because if I release an hot fix for Customer/CustomerImpl, I would like to minimize the impact on all customizations

that extends CustomerImpl.

There is a class Implementation with a static method getInstance that take as argument an interface and returns an instance of the choosen

(at context initialization) implementation.
For customer: Implementation.getInstance(Customer.class) return an instance of CustomerImpl in the standard application and an instance of

CustomerExt in the customized version.

The question is: is it possible and how can I write mappings for this situation?



Thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 12:01 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
It shouldn't be too difficult at all. The hbm files can be set up with inheritence in them. A code example is

Code:
<class
    name="dars.apis.audit.JobQueueList"
    table="job_queue_list"
>
    <meta attribute="generated-class" inherit="false">dars.apis.audit.BaseJobQueueList</meta>
   
    <composite-id name="comp_id" class="dars.apis.audit.JobQueueListPK">
        <meta attribute="generated-class" inherit="false">dars.apis.audit.BaseJobQueueListPK</meta>


JobQueueList extends BaseJobQueueList, and BaseJobQueueListPK is the composite primary key id of JobQueueList.

The customer might have to add additional lines to the hbm files for the extra attributes and change a class name or two, but it is possible with a minimal amount of extra work on the customer's behalf.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.