Decorator Mode (decorator pattern): dynamically attaches responsibility to an object. To extend functionality, adorners provide a more flexible alternative than inheritance.
How to use:
1. First create the component (Component) parent class , all classes, specific components (concrete Component) and adorners (decorator) belong to this type and can be extended;
Can be an abstract class (abstract Class) or an interface (interface);
Code:
/**
* @time May 23, 2014 * * *
package decorator;
/**
* @author C.l.wang * */public
abstract class Beverage {
String description = "Unkown Beverage ";
Public String GetDescription () {return
description;
}
public abstract double ();
}
2. Decorator (Decorator) class parent class, Inherit component (component) parent class , but do not Implement interface function, by its inheriting class (specific adorner) to implement ,
Can be an abstract class (abstract Class) or an interface (interface);
Code:
/**
* @time May 23, 2014 * * *
package decorator;
/**
* @author C.l.wang * */public
abstract class Condimentdecorator extends beverage {
public Abstract String getdescription ();
}
3. specific components (concrete Component), that is, the decorator (decorator) need to decorate the basis of the inheritance component (Component) parent class;
Code:
/** * @time May 23, 2014 * * * Package decorator; /** * @author C.l.wang * */public class Darkroast extends beverage {public darkroast () {description = "Dark Roas
T "; }/* (non-javadoc) * @see decorator.
Beverage#cost () */@Override public double () {//TODO auto-generated method stub return 0.99;
}/** * @time May 23, 2014 * * * Package decorator;
/** * @author C.l.wang * */public class Decat extends beverage {public Decat () {description = "Decat"; }/* (non-javadoc) * @see decorator.
Beverage#cost () */@Override public double () {//TODO auto-generated method stub return 1.05;
}/** * @time May 23, 2014 * * * Package decorator;
/** * @author C.l.wang * */public class Espresso extends beverage {public Espresso () {description = "Espresso"; }/* (non-javadoc) * @see decorator.
Beverage#cost () */@Override public double () {//TODO auto-generated method stub return 1.99; }/** * @time May 2014 23rd * * * Package decorator; /** * @author C.l.wang * */public class Houseblend extends beverage {public houseblend () {description = ' house B
Lend Coffee "; }/* (non-javadoc) * @see decorator.
Beverage#cost () */@Override public double () {//TODO auto-generated method stub return 89;
}
}