Intent
Attach additional responsibilities to an object dynamically. Decorators provide
a flexible alternative to subclassing for extending functionality.
a flexible alternative to subclassing for extending functionality.
Also Known As
Wrapper
Applicability
Use Decorator
- 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.
Structure
Program
====================Interface class=======================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public interface IceCream {
public double getCost();
public String getDiscription();
}
/**
*
* @author Sonu
*/
public interface IceCream {
public double getCost();
public String getDiscription();
}
==================== Abstract Class =======================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public abstract class Decoretor implements IceCream{
IceCream i;
public Decoretor(IceCream i1) {
i=i1;
}
@Override
public double getCost() {
return i.getCost();
}
@Override
public String getDiscription() {
return i.getDiscription();
}
}
/**
*
* @author Sonu
*/
public abstract class Decoretor implements IceCream{
IceCream i;
public Decoretor(IceCream i1) {
i=i1;
}
@Override
public double getCost() {
return i.getCost();
}
@Override
public String getDiscription() {
return i.getDiscription();
}
}
================concreate class =======================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public class Chocklate extends Decoretor{
public Chocklate(IceCream i1) {
super(i1);
}
public double getCost()
{
return super.getCost()+10;
}
public String getDiscription()
{
return super.getDiscription()+"With Chocklate";
}
}
/**
*
* @author Sonu
*/
public class Chocklate extends Decoretor{
public Chocklate(IceCream i1) {
super(i1);
}
public double getCost()
{
return super.getCost()+10;
}
public String getDiscription()
{
return super.getDiscription()+"With Chocklate";
}
}
====================concreate class ====================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public class Nutes extends Decoretor{
public Nutes(IceCream i1) {
super(i1);
}
@Override
public double getCost()
{
return super.getCost()+20;
}
@Override
public String getDiscription()
{
return super.getDiscription()+"With Nutes";
}
}
/**
*
* @author Sonu
*/
public class Nutes extends Decoretor{
public Nutes(IceCream i1) {
super(i1);
}
@Override
public double getCost()
{
return super.getCost()+20;
}
@Override
public String getDiscription()
{
return super.getDiscription()+"With Nutes";
}
}
=====================Concreate Class======================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public class StrawbarryIceCream implements IceCream{
@Override
public double getCost() {
return 15;
}
@Override
public String getDiscription() {
return "Add Strawbarry IceCream";
}
}
/**
*
* @author Sonu
*/
public class StrawbarryIceCream implements IceCream{
@Override
public double getCost() {
return 15;
}
@Override
public String getDiscription() {
return "Add Strawbarry IceCream";
}
}
===================Concreate Class========================
package DecoratorPattern;
/**
*
* @author Sonu
*/
public class VanillaIceCream implements IceCream{
@Override
public double getCost() {
return 5.5;
}
@Override
public String getDiscription() {
return "This is Vanilla IceCreame.";
}
}
/**
*
* @author Sonu
*/
public class VanillaIceCream implements IceCream{
@Override
public double getCost() {
return 5.5;
}
@Override
public String getDiscription() {
return "This is Vanilla IceCreame.";
}
}
=================The main class =======================
package DecoratorPattern;
import java.util.Scanner;
/**
*
* @author Sonu
*/
public class Main {
public static void main(String[] args) {
System.out.println("Enter your Choise:");
System.out.println("1.Vanilla IceCream.");
System.out.println("2.Strawbarry IceCream");
System.out.println("3.Vanilla IceCream with Chockalte.");
System.out.println("4.Vanilla IceCream with Nutes.");
System.out.println("5.Strawbarry IceCream with Chockalte.");
System.out.println("6.Strawbarry IceCream with Nutes.");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
switch(i)
{
case 1:
IceCream ic = new VanillaIceCream();
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 2:
ic = new StrawbarryIceCream();
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 3:
ic = new Chocklate((IceCream)new VanillaIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 4:
ic = new Nutes((IceCream)new VanillaIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 5:
ic = new Chocklate((IceCream)new StrawbarryIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 6:
ic = new Nutes((IceCream)new StrawbarryIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
default:
System.out.println("You Chose Wrong Option.");
}
}
}
import java.util.Scanner;
/**
*
* @author Sonu
*/
public class Main {
public static void main(String[] args) {
System.out.println("Enter your Choise:");
System.out.println("1.Vanilla IceCream.");
System.out.println("2.Strawbarry IceCream");
System.out.println("3.Vanilla IceCream with Chockalte.");
System.out.println("4.Vanilla IceCream with Nutes.");
System.out.println("5.Strawbarry IceCream with Chockalte.");
System.out.println("6.Strawbarry IceCream with Nutes.");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
switch(i)
{
case 1:
IceCream ic = new VanillaIceCream();
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 2:
ic = new StrawbarryIceCream();
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 3:
ic = new Chocklate((IceCream)new VanillaIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 4:
ic = new Nutes((IceCream)new VanillaIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 5:
ic = new Chocklate((IceCream)new StrawbarryIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
case 6:
ic = new Nutes((IceCream)new StrawbarryIceCream());
System.out.println("Total Cost:"+ic.getCost());
System.out.println(""+ic.getDiscription());
break;
default:
System.out.println("You Chose Wrong Option.");
}
}
}
======================== output =====================








0 comments:
Post a Comment