Decorator Blueprint Pattern Inwards Coffee Amongst Representative Coffee Tutorial

I was thinking to write on decorator blueprint pattern inwards Java when I starting fourth dimension wrote 10 interview questions on Singleton Pattern inwards Java. Since blueprint pattern is quite of import piece edifice software in addition to it’s as of import on whatever Core Java Interview, It’s e'er practiced 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 essence Java blueprint pattern in addition to y'all tin post away run into lot of its instance inwards JDK itself. JDK utilisation 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 blueprint pattern inwards Java. From blueprint perspective its too practiced thought to acquire how existing things piece of work within JDK itself for instance How HashMap industrial plant inwards Java or How SubString method piece of work inwards Java, that volition hand y'all around thought of things y'all demand to popular off along inwards hear piece 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 item object at run-time or dynamically.
·          At the same fourth dimension other instance of same flat volition non hold upwards affected past times this in addition to hence 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 around widely used.


I prefer to response What is decorator blueprint pattern inwards signal format simply 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 utilisation Decorator pattern inwards Java

·          When sub classing is acquire impractical in addition to nosotros demand large reveal of dissimilar possibilities to brand independent object or nosotros tin post away tell nosotros convey reveal 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 amend empathize concept of decorator blueprint pattern allow run into a code instance using Decorator Pattern inwards Java. You tin post away too await 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";

 public String getCurrencyDescription() {
  return description;
 }

 public abstract double cost(double value);

}


// Concrete Component

public class Rupee extends Currency {
double value;

 public Rupee() {
  description = "indian rupees";
 }

 public double cost(double v){
  value=v;
  return value;
 }

}

//Another Concrete Component

public flat Dollar extends Currency{
double value;

 public 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;
 }


 public String getDescription(){
  return currency.getDescription()+" ,its U.S. Dollar";
 }


}



//Another Concrete Decorator

public class SGDDecorator extends Decorator{
 Currency currency;

 public SGDDecorator(Currency currency){
  this.currency = currency;
 }


 public String getDescription(){
  return currency.getDescription()+" ,its singapore Dollar";
 }

}


Now its fourth dimension to banking concern 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 post away empathize this inwards next term;
1.      Component Interface: In our instance 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 deportment 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 elementary give-and-take nosotros tin post away tell it has a instance variable that holds reference for cistron they implement same cistron which they are going to decorate. Here a Decorator is an abstract flat 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 instance variable for cistron interface or the affair which they are going to decorate.

Advantage of Decorator blueprint Pattern inwards Java

In brief nosotros run into what the original 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 alteration the object functionality

Disadvantage

Main disadvantage of using Decorator Pattern inwards Java is that the code maintenance tin post away hold upwards a work as it provides a lot of similar variety of minor objects (each decorator).

That’s all on decorator blueprint 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 intend of scenario where inheritance is impractical in addition to y'all 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

Komentar

Postingan populer dari blog ini

Virtualbox - /Sbin/Mount.Vboxsf: Mounting Failed Amongst The Error: Protocol Fault [Solution]

Top Ten Jdbc Interview Questions Answers For Coffee Programmer

Fix Protocol As Well As Cause Messaging Interview Questions