Decorator Blueprint Pattern Inwards Coffee Alongside Representative Coffee Tutorial

I was thinking to write on decorator pattern pattern inward Java when I outset wrote 10 interview questions on Singleton Pattern inward Java. Since pattern pattern is quite of import piece edifice software in addition to it’s as of import on whatsoever Core Java Interview, It’s ever practiced to accept clear agreement of diverse pattern patterns inward Java. In this article nosotros volition explore in addition to acquire Decorator Design pattern inward Java which is a prominent heart Java pattern pattern in addition to you lot tin come across lot of its illustration inward JDK itself. JDK usage decorator pattern inward 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 pattern pattern inward Java. From pattern perspective its besides practiced thought to acquire how existing things piece of work within JDK itself for illustration How HashMap industrial plant inward Java or How SubString method piece of work inward Java, that volition hand you lot to a greater extent than or less thought of things you lot demand to continue inward hear piece designing your Class or interface inward Java. Now let’s Move on to Decorator pattern inward Java.

Java Decorator Design Pattern

In this Java tutorial nosotros volition see:
What is decorator pattern inward Java?
When to usage decorator pattern inward Java?
How to usage decorator pattern inward Java?
Example of decorator pattern pattern
Advantage in addition to Disadvantage of decorator pattern inward Java

What is decorator pattern pattern inward Java?


·          Decorator pattern 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 degree volition non hold out affected yesteryear this in addition to thus 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 almost widely used.


I prefer to respond What is decorator pattern pattern inward signal format merely to stress on of import signal similar this pattern operator at private object level. This inquiry besides asked inward many Core Java interviews inward Investment banks

Problem which is solved yesteryear Decorator Pattern:


When to usage Decorator pattern inward Java

·          When sub classing is acquire impractical in addition to nosotros demand large release of dissimilar possibilities to brand independent object or nosotros tin state nosotros accept release of combination for an object.

·          Secondly when nosotros desire to add together functionality to private object non to all object at run-time nosotros usage decorator pattern pattern.

Code Example of decorator pattern pattern:

To amend empathize concept of decorator pattern pattern allow come across a code illustration using Decorator Pattern inward Java. You tin besides 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";

 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 degree 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 US of America 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 agree 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 empathize this inward next term;
1.      Component Interface: In our illustration Currency interface is gene 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 inward elementary give-and-take nosotros tin state it has a illustration variable that holds reference for gene they implement same gene which they are going to decorate. Here a Decorator is an abstract degree 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 gene interface or the affair which they are going to decorate.

Advantage of Decorator pattern Pattern inward Java

In brief nosotros come across what the primary 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 heighten or modification the object functionality

Disadvantage

Main disadvantage of using Decorator Pattern inward Java is that the code maintenance tin hold out a occupation as it provides a lot of similar form of pocket-size objects (each decorator).

That’s all on decorator pattern pattern inward 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 mean value of scenario where inheritance is impractical in addition to you lot await to a greater extent than flexibility in addition to sweat to use decorator pattern inward Java  there.

Further Learning
How to override equals method inward Java
How to implement Thread inward Java ?Example of Runnable interface
Difference betwixt ConcurrentHashMap in addition to Collections.synchronizedMap in addition to Hashtable inward Java
Advanced concept on Enum inward Java amongst Example

Sumber https://javarevisited.blogspot.com/

0 Response to "Decorator Blueprint Pattern Inwards Coffee Alongside Representative Coffee Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel