Question :What is the difference between inheritance and decorator design
pattern? (DesignPatterns)
Answer :Decorators represent a powerful alternative to inheritance. Inheritance
lets you add functionality to classes
at compile time, decorators let you add functionality to objects at runtime.
For example
1. FileReader frdr = new FileReader(filename);
2. LineNumberReader lrdr = new LineNumberReader(frdr);
Line 1 creates a file reader (frdr), and line 2 adds line-number tracking.
At runtime, decorators forward method calls to the objects they decorate.
For example, in the code above, the line
number reader, lrdr, forwards method calls to the file reader, frdr.
Decorators add functionality either before or
after forwarding to the object they decorate; for example, our line number
reader tracks the current line number as
it reads from an input stream.
www.interviewhelper.org is a hope for all the Job seekers. Users can find unlimited here. These range from technical to HR, from functional to behavioral. www.interviewhelper.org contains in the fields like Ajax,VB,Networking,Sharepoint,JEE,Perl,Javascript,Bioinformatics,Classic ASP,Unix,Linux,Accounting,Oracle DBA,Microprocessor,Bluetooth,jms,jme,.net framework,ABAP,ASP,ASP.net,Basic .net Framework,Interview Questions,C language,C++,database,Oracle,Java,PHP,LAMP,SAP etc.
Friday, March 13, 2009
What is the difference between inheritance and decorator design
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment