Adapter Pattern with Polymorphism
This program integrates Adapter pattern with polymorphism.
Problem Statement
Write a Java program where Adapter converts LegacyPrinter to Printer interface.
Source Code
| 1 | interface Printer { void print(); } |
| 2 | class LegacyPrinter { void oldPrint(){ System.out.println("Eduinq Legacy Printing"); } } |
| 3 | class PrinterAdapter implements Printer { |
| 4 | LegacyPrinter lp=new LegacyPrinter(); |
| 5 | public void print(){ lp.oldPrint(); } |
| 6 | } |
| 7 | public class AdapterPolymorphism { |
| 8 | public static void main(String[] args) { |
| 9 | System.out.println("Eduinq Adapter Polymorphism"); |
| 10 | Printer p=new PrinterAdapter(); |
| 11 | p.print(); |
| 12 | } |
| 13 | } |
Program Output
Eduinq Adapter Polymorphism Eduinq Legacy Printing
Explanation
Adapter pattern converts one interface into another expected by clients. - Polymorphism allows the adapter to be used wherever Printer is expected. - This design is crucial in integrating legacy systems with modern applications. - It demonstrates how polymorphism and design patterns combine to achieve compatibility and flexibility in enterprise systems. Conclusion: Shows Adapter pattern with polymorphism.