• Post Reply Bookmark Topic Watch Topic
  • New Topic

Override object method at runtime  RSS feed

 
Eswar Rao
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an object retrived from the factory method of a thirdparty library and I want to override a method on this object.

Please clarify How best I can do this.

Thanks & Regards
Eshwar.
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eswar Rao wrote:I have an object retrived from the factory method of a thirdparty library and I want to override a method on this object.
Please clarify How best I can do this.

If it implements an interface (as most factory objects do), write a wrapper class that implements the same interface, and:
1. Implement your own version of the particular method.
2. Implement the rest of the methods as forwarders.
You might want to look at the Decorator Pattern for more info.

If it doesn't, you have more of a problem.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are many methods, you can even consider using a Dynamic Proxy to implement the above idea.

http://docs.oracle.com/javase/1.4.2/docs/guide/reflection/proxy.html

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!