Hello all,
Would the following be considered bad practice...
I have a very simple bean called "SimpleTypeBean" which is constructed
as follows:
public class SimpleTypeBean {
private long id;
private String description;
public SimpleTypeBean() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
I then need to declare a very similar bean (in fact, it is identical
in terms of data types), except one of the identifiers is called lob
not id, so I have done the following:
public class LobBean extends SimpleTypeBean {
public LobBean() {
}
public long getLob() {
return super.getId();
}
public void setLob(long lob) {
super.setId(lob);
}
}
Is this considered bad practice or is this what I should be doing?
Many Thanks
Andy
lord.zoltar@gmail.com - 04 Jan 2008 16:25 GMT
On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
> Hello all,
>
[quoted text clipped - 53 lines]
>
> Andy
The subclass will have an id and lob.
What you could do: Create an abstract class "AbstractSimpleTypeBean"
that implements everything EXCEPT the id and lob. Then have subclasses
that implement id and lob as appropriate.
Lew - 05 Jan 2008 01:04 GMT
andymconl...@googlemail.com wrote:
>> public class LobBean extends SimpleTypeBean {
>>
[quoted text clipped - 12 lines]
>>
>> Is this considered bad practice or is this what I should be doing?
> The subclass will have an id and lob.
True - the public parent methods publish a view of the 'id' as an alias for 'lob'.
> What you could do: Create an abstract class "AbstractSimpleTypeBean"
> that implements everything EXCEPT the id and lob. Then have subclasses
> that implement id and lob as appropriate.
Or just use a 'SimpleBean' where 'id' just happens to function as a 'lob',
talks to 'lob' columns of the data store, fills in 'lob' UI fields, and just
isn't named 'lob'.
Or just write another bean that is structurally identical with different field
names.
Inheritance isn't really about refactoring for the sake of refactoring. It's
to capture a modeled "is-a" relationship. If your 'LobBean' /is-a/
'SimpleBean' then inheritance is exactly right.
In this case it partly works - LobBean puts a facade on its parent that lets
it show an 'id' as if it were a 'lob'. You don't actually need the 'super'
decorations to resolve the calls since they're public. You still have that
'id' attribute though, and that doesn't match your logical model. So that
breaks /is-a/.

Signature
Lew
Jason Cavett - 04 Jan 2008 16:26 GMT
On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
> Hello all,
>
[quoted text clipped - 53 lines]
>
> Andy
Two things going on here. First, the simple thing...
I think the important part here is, "One of the components is called
lob ***NOT*** id."
So, you're saying, LobBean extends SimpleTypeBean. So, because of
that, your LobBean has TWO long values (lob and id). I don't think
that's what you're really looking for. So, the simple solution is
that LobBean does not extend SimpleTypeBean.
Slightly more complicated, but makes more sense...
BUT, as you have noticed, both beans do share a description. And, if
you also noticed, "lob" and "id" are *exactly* the same thing...they
are both long values. So, the only difference is what you name them.
In my opinion, that's not a difference. Really, you could use Simple
as an LOB and be perfectly fine. (Of course, if LobBean has OTHER
differences, you would want to extend SimpleBean and then add the
additional features.) The point I'm trying to make is that id = lob
for your purposes. Don't make it more difficult than it is through
your variable names (which nobody is going to see anyway).
Lew - 05 Jan 2008 03:27 GMT
> Two things going on here. First, the simple thing...
>
[quoted text clipped - 17 lines]
> for your purposes. Don't make it more difficult than it is through
> your variable names (which nobody is going to see anyway).
Cogent and wise.

Signature
Lew
Jason Cavett - 04 Jan 2008 16:28 GMT
On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
> Hello all,
>
[quoted text clipped - 53 lines]
>
> Andy
Something I also missed...
Having "setLob" and "getLob" does NOT hide "setId" and "getId." So,
if someone is using your beans, it could be very confusing to the
differences of what an lob is and what an id is.
andymconline@googlemail.com - 04 Jan 2008 16:37 GMT
> On Jan 4, 10:46 am, andymconl...@googlemail.com wrote:
>
[quoted text clipped - 61 lines]
> if someone is using your beans, it could be very confusing to the
> differences of what an lob is and what an id is.
Thanks Jason. I think I will just remove LobBean and go with
SimpleTypeBean
Kind Regards
Andy
Patricia Shanahan - 04 Jan 2008 16:44 GMT
...
>> Having "setLob" and "getLob" does NOT hide "setId" and "getId." So,
>> if someone is using your beans, it could be very confusing to the
>> differences of what an lob is and what an id is.
>
> Thanks Jason. I think I will just remove LobBean and go with
> SimpleTypeBean
That is a good solution if, and only if, each of the bean's methods can
be described by a simple, clear comment. That will be the case if a
"lob" and an "id" do essentially the same job, in the context of the
interface to this bean, as well as having the same type.
Patricia
Roedy Green - 05 Jan 2008 09:15 GMT
>Is this considered bad practice or is this what I should be doing?
this is screwy since you still have getID defined.
The proper way to do this is define an abstract class without id or
lob, then subclass it once with id and once with lob.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com