JavaAdvanced#design-patterns

What is the difference between the Adapter and Decorator patterns?

Adapter converts one interface into another that a client expects, bridging incompatible interfaces. Decorator wraps an object to add new behavior dynamically without altering its interface or subclassing.

Example
// Adapter: makes LegacyPrinter usable as Printer
class PrinterAdapter implements Printer { LegacyPrinter lp; public void print(String s) { lp.oldPrint(s); } }
// Decorator: adds behavior
InputStream in = new BufferedInputStream(new FileInputStream("f.txt"));

Related Questions

1
JavaAdvanced#design-patterns#oop

What is the difference between composition and inheritance, and why is 'favor composition over inheritance' recommended?

Open
2
JavaAdvanced#design-patterns

What is the Template Method pattern?

Open
3
JavaBeginner#jdbc

What is JDBC and what are its main components?

Open