Java का Decorator Pattern
In this page:
class Decorator implements Component {
private Component wrapped;
public void operation() {
wrapped.operation();
// extra behavior
}
}
What is the Decorator Pattern?
Decorator pattern किसी object में अतिरिक्त responsibilities dynamically जोड़ता है उसे एक दूसरे object में wrap करके जो वही interface implement करता है, behavior को extend करने के लिए subclassing का एक flexible विकल्प देते हुए।
उदाहरण: What is the Decorator Pattern?
public class Main {
interface Coffee { String describe(); }
static class SimpleCoffee implements Coffee {
public String describe() { return "Coffee"; }
}
static class MilkDecorator implements Coffee {
Coffee wrapped;
MilkDecorator(Coffee wrapped) { this.wrapped = wrapped; }
public String describe() { return wrapped.describe() + " + Milk"; } // wraps, same interface
}
public static void main(String[] args) {
Coffee coffee = new MilkDecorator(new SimpleCoffee());
System.out.println(coffee.describe());
}
}
Login to try C/C++/Java/PHP code in the editor
Wrapping an Object
एक decorator उसी interface को implement करता है जिसे यह wrap करता है, अपने core behavior के लिए wrapped object को delegate करते हुए और उस delegated call से पहले या बाद में अपनी खुद की logic जोड़ते हुए।
उदाहरण: Wrapping an Object
public class Main {
interface Coffee { String describe(); }
static class SimpleCoffee implements Coffee {
public String describe() { return "Coffee"; }
}
static class SugarDecorator implements Coffee {
Coffee wrapped;
SugarDecorator(Coffee wrapped) { this.wrapped = wrapped; }
public String describe() { return wrapped.describe() + " + Sugar"; } // delegates, then adds
}
public static void main(String[] args) {
System.out.println(new SugarDecorator(new SimpleCoffee()).describe());
}
}
Login to try C/C++/Java/PHP code in the editor
Stacking Multiple Decorators
कई decorators एक-दूसरे के ऊपर किसी भी order या combination में layer किए जा सकते हैं, क्योंकि हर एक एक ही interface को implement और wrap करता है, runtime पर behavior को flexibly compose होने देते हुए।
उदाहरण: Stacking Multiple Decorators
public class Main {
interface Coffee { String describe(); }
static class SimpleCoffee implements Coffee { public String describe() { return "Coffee"; } }
static class MilkDecorator implements Coffee {
Coffee wrapped; MilkDecorator(Coffee wrapped) { this.wrapped = wrapped; }
public String describe() { return wrapped.describe() + " + Milk"; }
}
static class SugarDecorator implements Coffee {
Coffee wrapped; SugarDecorator(Coffee wrapped) { this.wrapped = wrapped; }
public String describe() { return wrapped.describe() + " + Sugar"; }
}
public static void main(String[] args) {
Coffee coffee = new SugarDecorator(new MilkDecorator(new SimpleCoffee())); // layered, any order
System.out.println(coffee.describe());
}
}
Login to try C/C++/Java/PHP code in the editor
Decorator vs Inheritance
optional behavior के हर combination को जोड़ने के लिए inheritance का उपयोग करना हर combination के लिए एक नई subclass चाहता है, जल्दी ही unmanageable हो जाते हुए, जबकि decorators ज़रूरी classes की संख्या को बढ़ाए बिना स्वतंत्र रूप से combine होते हैं।
उदाहरण: Decorator vs Inheritance
public class Main {
interface Coffee { String describe(); }
static class SimpleCoffee implements Coffee { public String describe() { return "Coffee"; } }
static class Decorator implements Coffee {
Coffee wrapped; String extra;
Decorator(Coffee wrapped, String extra) { this.wrapped = wrapped; this.extra = extra; }
public String describe() { return wrapped.describe() + " + " + extra; }
}
public static void main(String[] args) {
// One Decorator class combines freely, instead of MilkCoffee, SugarCoffee, MilkSugarCoffee subclasses...
Coffee coffee = new Decorator(new Decorator(new SimpleCoffee(), "Milk"), "Sugar");
System.out.println(coffee.describe());
}
}
Login to try C/C++/Java/PHP code in the editor
Java's Built-in Decorators
Java की अपनी I/O classes Decorator pattern का एक जाना-पहचाना असली-दुनिया उदाहरण हैं: BufferedReader, BufferedWriter, और PrintWriter सभी किसी दूसरे Reader या Writer को wrap करते हैं, उसके ऊपर buffering या formatting जोड़ते हुए।
उदाहरण: Java's Built-in Decorators
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(new BufferedWriter(sw)); // both wrap and add formatting/buffering
pw.println("wrapped output");
pw.flush();
System.out.println(sw.toString().trim());
}
}
Login to try C/C++/Java/PHP code in the editor
- decorator के अंदर wrapped object के method को call करना भूल जाना, जिससे original behavior खो जाता है।
- decorator को उसके wrap किए object से अलग interface implement करवाना, जिससे यह दूसरे decorators के साथ stack नहीं हो सकता।
- यह नज़रअंदाज़ करना कि stacking का order मायने रखता है, जिससे अलग order में wrap करना एक अलग result देता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern