Decorator mode (decorator pattern) detailed

Source: Internet
Author: User
Tags abstract inheritance stub

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.