I was thinking to write on decorator pattern pattern inwards Java when I showtime wrote 10 interview questions on Singleton Pattern inwards Java. Since pattern pattern is quite of import field edifice software in addition to it’s as of import on whatever Core Java Interview, It’s ever expert to bring clear agreement of diverse pattern patterns inwards Java. In this article nosotros volition explore in addition to larn Decorator Design pattern inwards Java which is a prominent gist Java pattern pattern in addition to you lot tin encounter lot of its instance inwards JDK itself. JDK job decorator pattern inwards IO bundle where it has decorated Reader in addition to Writer Classes for diverse scenario, for instance BufferedReader in addition to BufferedWriter are instance of decorator pattern pattern inwards Java. From pattern perspective its too expert thought to larn how existing things piece of work within JDK itself for instance How HashMap plant inwards Java or How SubString method piece of work inwards Java, that volition hand you lot or then thought of things you lot demand to proceed inwards hear field designing your Class or interface inwards Java. Now let’s Move on to Decorator pattern inwards Java.
Java Decorator Design Pattern
In this Java tutorial nosotros volition see:
What is decorator pattern inwards Java?
When to job decorator pattern inwards Java?
How to job decorator pattern inwards Java?
Example of decorator pattern pattern
Advantage in addition to Disadvantage of decorator pattern inwards Java
What is decorator pattern pattern inwards Java?
· Decorator pattern pattern is used to enhance the functionality of a item object at run-time or dynamically.
· At the same fourth dimension other event of same cast volition non endure affected past times this then private object gets the novel behavior.
· Basically nosotros wrap the original object through decorator object.
· Decorator pattern pattern is based on abstract classes in addition to nosotros derive concrete implementation from that classes,
· It’s a structural pattern pattern in addition to virtually widely used.
I prefer to answer What is decorator pattern pattern inwards signal format merely to stress on of import signal similar this pattern operator at private object level. This enquiry too asked inwards many Core Java interviews inwards Investment banks
Problem which is solved past times Decorator Pattern:
When to job Decorator pattern inwards Java
· When sub classing is choke impractical in addition to nosotros demand large seat out of dissimilar possibilities to brand independent object or nosotros tin state nosotros bring seat out of combination for an object.
· Secondly when nosotros desire to add together functionality to private object non to all object at run-time nosotros job decorator pattern pattern.
Code Example of decorator pattern pattern:
To ameliorate empathise concept of decorator pattern pattern permit encounter a code instance using Decorator Pattern inwards Java. You tin too await within JDK in addition to uncovering what are classes in addition to packages which are using decorator pattern.
// Component on Decorator pattern pattern
public abstract class Currency {
String description = "Unknown currency";
String description = "Unknown currency";
public String getCurrencyDescription() {
return description;
}
return description;
}
public abstract double cost(double value);
}
// Concrete Component
public class Rupee extends Currency {
double value;
public Rupee() {
description = "indian rupees";
}
description = "indian rupees";
}
public double cost(double v){
value=v;
value=v;
return value;
}
}
}
//Another Concrete Component
public cast Dollar extends Currency{
double value;
public Dollar () {
description = "Dollar”;
}
description = "Dollar”;
}
public double cost(double v){
value=v;
return value;
}
}
// Decorator
public abstract class Decorator extends Currency{
public abstract String getDescription();
}
// Concrete Decorator
public class USDDecorator extends Decorator{
Currency currency;
public USDDecorator(Currency currency){
this.currency = currency;
}
this.currency = currency;
}
public String getDescription(){
return currency.getDescription()+" ,its United States of America Dollar";
}
return currency.getDescription()+" ,its United States of America Dollar";
}
}
//Another Concrete Decorator
public class SGDDecorator extends Decorator{
Currency currency;
Currency currency;
public SGDDecorator(Currency currency){
this.currency = currency;
}
this.currency = currency;
}
public String getDescription(){
return currency.getDescription()+" ,its singapore Dollar";
}
return currency.getDescription()+" ,its singapore Dollar";
}
}
Now its fourth dimension to banking company fit currency.
public class CurrencyCheck {
public static void main(String[] args) {
// without adding decorators
Currency curr = new Dollar();
System.out.println(curr.getDescription() +" dollar. "+curr.cost(2.0));
//adding decorators
Currency curr2 = new USDDecorator(new Dollar());
System.out.println(curr2.getDescription() +" dollar. "+curr2.cost(4.0));
Currency curr3 = new SGDDecorator(new Dollar());
System.out.println(curr3.getDescription() +" dollar. "+curr3.cost(4.0));
}
Explanation of the code:
We tin empathise this inwards next term;
1. Component Interface: In our instance Currency interface is part which used on its ain or nosotros demand decorator for that.
2. Concrete Component: it implements Component in addition to nosotros add together novel demeanor to this object at dynamically. Dollar in addition to Rupee are the concrete implementation of currency.
3. Decorator: Decorator contains a HAS a Relationship inwards uncomplicated discussion nosotros tin state it has a event variable that holds reference for part they implement same part which they are going to decorate. Here a Decorator is an abstract cast which extends the currency.
4. Concrete Decorator: it’s an implementation of Decorator So USD Dollar in addition to SGD Dollar are the implementation of Decorator contains event variable for part interface or the matter which they are going to decorate.
Advantage of Decorator pattern Pattern inwards Java
In brief nosotros encounter what the principal advantages of using decorator pattern patterns are.
1. Decorator Pattern is flexible than inheritance because inheritance add together responsibilities at compile fourth dimension in addition to it volition add together at run-time.
2. Decorator pattern get upwards or alter the object functionality
Disadvantage
Main disadvantage of using Decorator Pattern inwards Java is that the code maintenance tin endure a job as it provides a lot of similar form of pocket-sized objects (each decorator).
That’s all on decorator pattern pattern inwards Java. To acquire mastery on decorator pattern I advise looking within JDK library itself in addition to finding what classes are decorated, why they are decorated. Also scream back of scenario where inheritance is impractical in addition to you lot await to a greater extent than flexibility in addition to endeavour to use decorator pattern inwards Java there.
Further Learning
How to override equals method inwards Java
How to implement Thread inwards Java ?Example of Runnable interface
Difference betwixt ConcurrentHashMap in addition to Collections.synchronizedMap in addition to Hashtable inwards Java
Advanced concept on Enum inwards Java amongst Example