• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CMP set methods?

 
vikram goyal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although we are allowed to expose CMP set methods that are not associated with the primary key, but it is not recomended.

my question is why is it a bad idea to expose set methods to the client?

Thanx
V
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,
One reason that you probably do not want to expose your CMP accessors is that it is similar to the encapsulation problem that you have with public fields... you can't add any other validation code.
If you say:
public abstract void setFoo(int fooValue);
And you expose setFoo(), you are allowing the caller to set *any* value they like for Foo.
But if you say:
public void setFooValue(int fooValue) {
if (...) {
this.setFoo(fooValue);
} else {
...
}
}
Then you now have control over what happens. In any case, writing your *own* accessors into your component interface gives you the flexibility to modify things later without breaking your clients... and as we know, clients can become violent and dangerous when broken
Cheers,
Kathy
 
vikram goyal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
V
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic