JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Decorator Pattern
  • Attach additional responsibilities or functions to an 
    object dynamically or statically. Also known as Wrapper.
  • Provide an alternative to subclassing.
  • Add new function to an object without affecting other objects.
  • Make a responsibility easily added and removed dynamically.
  • More flexibility than static inheritance.
  • Transparent to the object.
  • The decorator pattern is an alternative to subclassing.
  • Subclassing adds behavior at compile time whereas

Purpose
  • Attach additional responsibilities to an object dynamically.
  • Decorators provide a flexible alternative to subclassing for extending functionality.
Applications
  • To add responsibilities to individual objects dynamically and transparently, that
    is, without affecting other objects.
  • For responsibilities that can be withdrawn.
  • when extension by subclassing is impractical. Sometimes a large number of
    independent extensions are possible and would produce an explosion of
    subclasses to support every combination. Or a class definition may be hidden or
    otherwise unavailable for subclassing.

Examples of  Decorator Pattern
The Decorator attaches additional responsibilities to an object dynamically

Example:-1

Sale.java
package com.model;

public class Sale {
private int nop;
private double saleamt;
public int getNop() {
  return nop;
}
public void setNop(int nop) {
  this.nop = nop;
}
public double getSaleamt() {
  return saleamt;
}
public void setSaleamt(double saleamt) {
  this.saleamt = saleamt;
}

public Sale(int nop, double saleamt) {
  super();
  this.nop = nop;
  this.saleamt = saleamt;
}
public double getTotal()
{
  return saleamt;
}
}

BatchDecorator.java
package com.model;

public class BatchDecorator extends SaleDecorator {
  Sale sale;

  public BatchDecorator(Sale sale) {
    super();
    this.sale = sale;
  }

  @Override
  public double getTotal() {

    if (sale.getNop() 10)
      return sale.getTotal() 10;
    return sale.getTotal();
  }

  public int getNop() {
    return sale.getNop();
  }
}


SaleDecorator.java
package com.model;

public abstract class SaleDecorator extends Sale{
  public abstract double getTotal();
}


DiscountDecorator.java
package com.model;

public class DiscountDecorator extends SaleDecorator {

  Sale sale;

  public DiscountDecorator(Sale sale) {
    super();
    this.sale = sale;
  }

  @Override
  public double getTotal() {

    return sale.getTotal() (5.0 100.0 * sale.getTotal());
  }

  public int getNop() {
    return sale.getNop();
  }
}


Test.java
package com.tests;

import com.model.BatchDecorator;
import com.model.DiscountDecorator;
import com.model.Sale;

public class Test {
  public static void main(String ar[]) {
    Sale sale = new Sale(152000);

    sale = new DiscountDecorator(sale);

    sale = new BatchDecorator(sale);
    System.out.println(sale.getTotal());

  }
}

Output:-

Download Zip code

Example:-2

Widget.java
package com.decorator;

public interface Widget {
  void draw();
}


Decorator.java
package com.decorator;

abstract class Decorator implements Widget {
  private Widget wid;

  public Decorator(Widget w) {
    wid = w;
  }

  public void draw() {
    wid.draw();
  }
}



BorderDecorator.java
package com.decorator;

public class BorderDecorator extends Decorator {
  public BorderDecorator(Widget w) {
    super(w);
  }

  public void draw() {
    super.draw()
    System.out.println("  BorderDecorator");
  }
}


ScrollDecorator.java
package com.decorator;

public class ScrollDecorator extends Decorator {
    public ScrollDecoratorWidget w ) {
      super);
    }
    public void draw() {
      super.draw()
      System.out.println"  ScrollDecorator" );
    }
  }

TextField.java
package com.decorator;

public class TextField implements Widget {
  private int width, height;

  public TextField(int w, int h) {
    width = w;
    height = h;
  }

  public void draw() {
    System.out.println("TextField: " + width + ", " + height);
  }
}

DecoratorDemo.java
package com.decorator.client;

import com.decorator.*;

public class DecoratorDemo {
  public static void main(String[] args) {

    Widget aWidget = new BorderDecorator(new BorderDecorator(
        new ScrollDecorator(new TextField(8024))));
    aWidget.draw();
  }
}

Output:-


Download Zip code

Example:-3

IEmail.java
package com.decorator;

public interface IEmail
{
   public String getContents();
}

Email.java
package com.decorator;

//concrete component
public class Email implements IEmail
{
   private String content;
   
   public Email(String content)
   {
      this.content = content;
   }

   @Override
   public String getContents()
   {
      //general email stuff
      return content;

   }

}


ExternalEmailDecorator.java
package com.decorator;

//concrete decorator
public class ExternalEmailDecorator extends EmailDecorator
{
   private String content; 
   
   public ExternalEmailDecorator(IEmail basicEmail)
   {
      originalEmail = basicEmail;
   }
   
   @Override
   public String getContents()
   {
      
      //  secure original 
      content = addDisclaimer(originalEmail.getContents());
      return content;
   }
   
   
   private String addDisclaimer(String message)
   {
      //append company disclaimer to message
      return  message + "\n Company Disclaimer";
   }
   
}


EmailSender.java
package com.decorator;

public class EmailSender
{
   
   public void sendEmail(IEmail email)
   {
      //read the email to-address, to see
 if it's going outside of the company

      //if so decorate it 
      ExternalEmailDecorator external =
 
new ExternalEmailDecorator(email);
      System.out.println(external.getContents());
      //send 
      
   }
   
   public static void main(String[] args)
   {
    
     EmailSender emailSender  = new EmailSender();
     Email e=new Email("Hello Test");
     emailSender.sendEmail(e);
     
   }
   
}

Output:-

Download Zip code

Example:-4

ChaiDecorator.java
import java.util.ArrayList;
import java.util.ListIterator;

public class ChaiDecorator extends Tea {
    private Tea teaToMakeChai;
    private ArrayList chaiIngredients = new ArrayList();
    
    public ChaiDecorator(Tea teaToMakeChai) {
        this.addTea(teaToMakeChai);
        chaiIngredients.add("bay leaf");
        chaiIngredients.add("cinnamon stick");
        chaiIngredients.add("ginger");
        chaiIngredients.add("honey");
        chaiIngredients.add("soy milk");
        chaiIngredients.add("vanilla bean");
    }

    private void addTea(Tea teaToMakeChaiIn) {
        this.teaToMakeChai = teaToMakeChaiIn;
    }
    
    public void steepTea() {
        this.steepChai();
    }

    public void steepChai() {
        teaToMakeChai.steepTea();
        this.steepChaiIngredients();
        System.out.println("tea is steeping with chai");
    }    
    
    public void steepChaiIngredients() {
        ListIterator listIterator = chaiIngredients.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(((String)(listIterator.next())) 
                                         " is steeping");
        }
        System.out.println("chai ingredients are steeping");
    }      
}

Tea.java
public abstract class Tea {  
   boolean teaIsSteeped; 
   
   public abstract void steepTea();
}

TeaLeaves.java
public class TeaLeaves extends Tea {  
   public TeaLeaves() {
       teaIsSteeped = false;
   }
   
   public void steepTea() {
       teaIsSteeped = true;
       System.out.println("tea leaves are steeping");
   }
}


TestChaiDecorator.java
class TestChaiDecorator {            
    
   public static void main(String[] args) {
       Tea teaLeaves = new TeaLeaves();
       Tea chaiDecorator = new ChaiDecorator(teaLeaves);
       chaiDecorator.steepTea();
   }
}

Output:-

Download Zip code-