Decorator Pattern Pattern Inward Coffee Amongst Example Coffee Tutorial
I was thinking to write on decorator blueprint pattern inwards Java when I offset wrote 10 interview questions on Singleton Pattern inwards Java. Since blueprint pattern is quite of import spell edifice software in addition to it’s every bit of import on whatever Core Java Interview, It’s e'er skilful to convey clear agreement of diverse blueprint patterns inwards Java. In this article nosotros volition explore in addition to acquire Decorator Design pattern inwards Java which is a prominent pith Java blueprint pattern in addition to you lot tin run across lot of its illustration inwards JDK itself. JDK utilisation decorator pattern inwards IO packet where it has decorated Reader in addition to Writer Classes for diverse scenario, for illustration BufferedReader in addition to BufferedWriter are illustration of decorator blueprint pattern inwards Java. From blueprint perspective its besides skilful thought to acquire how existing things piece of work within JDK itself for illustration How HashMap industrial plant inwards Java or How SubString method piece of work inwards Java, that volition compass you lot or in addition to therefore thought of things you lot demand to give-up the ghost along inwards heed spell 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 utilisation decorator pattern inwards Java?
How to utilisation decorator pattern inwards Java?
Example of decorator blueprint pattern
Advantage in addition to Disadvantage of decorator pattern inwards Java
What is decorator blueprint pattern inwards Java?
· Decorator blueprint pattern is used to enhance the functionality of a detail object at run-time or dynamically.
· At the same fourth dimension other illustration of same shape volition non hold out affected yesteryear this in addition to therefore private object gets the novel behavior.
· Basically nosotros wrap the original object through decorator object.
· Decorator blueprint pattern is based on abstract classes in addition to nosotros derive concrete implementation from that classes,
· It’s a structural blueprint pattern in addition to close widely used.
I prefer to respond What is decorator blueprint pattern inwards betoken format only to stress on of import betoken similar this pattern operator at private object level. This query besides asked inwards many Core Java interviews inwards Investment banks
Problem which is solved yesteryear Decorator Pattern:
When to utilisation Decorator pattern inwards Java
· When sub classing is acquire impractical in addition to nosotros demand large disclose of dissimilar possibilities to brand independent object or nosotros tin tell nosotros convey disclose of combination for an object.
· Secondly when nosotros desire to add together functionality to private object non to all object at run-time nosotros utilisation decorator blueprint pattern.
Code Example of decorator blueprint pattern:
To improve empathise concept of decorator blueprint pattern allow run across a code illustration using Decorator Pattern inwards Java. You tin besides hold off within JDK in addition to notice what are classes in addition to packages which are using decorator pattern.
// Component on Decorator blueprint 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 shape 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 U.S. Dollar";
}
return currency.getDescription()+" ,its U.S. 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 concern stand upwards for 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 illustration Currency interface is cistron which used on its ain or nosotros demand decorator for that.
2. Concrete Component: it implements Component in addition to nosotros add together novel behaviour 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 tell it has a illustration variable that holds reference for cistron they implement same cistron which they are going to decorate. Here a Decorator is an abstract shape 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 illustration variable for cistron interface or the affair which they are going to decorate.
Advantage of Decorator blueprint Pattern inwards Java
In brief nosotros run across what the primary advantages of using decorator blueprint 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 heighten or alter the object functionality
Disadvantage
Main disadvantage of using Decorator Pattern inwards Java is that the code maintenance tin hold out a work as it provides a lot of similar sort of small-scale objects (each decorator).
That’s all on decorator blueprint pattern inwards Java. To acquire mastery on decorator pattern I propose looking within JDK library itself in addition to finding what classes are decorated, why they are decorated. Also intend of scenario where inheritance is impractical in addition to you lot hold off to a greater extent than flexibility in addition to elbow grease 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 alongside Example
Belum ada Komentar untuk "Decorator Pattern Pattern Inward Coffee Amongst Example Coffee Tutorial"
Posting Komentar