Learn a little design pattern every day-decorator mode

Source: Internet
Author: User
Tags abstract stub
Decorator Mode


English name


Decorator Pattern


definition


Dynamically attaches responsibility to objects, and to extend functionality, decorators provide a more resilient alternative than inheritance


Principles


1. Classes should be open to extensions, closed for modifications

2. This is the first model to conform to the "open close" principle


Understanding


1. To use decorators, first find the component that defines the decorator, defined as an abstract class, meaning an abstract interface.

2. Define an adorner component, also defined as an abstract class. An abstract component that is intended to be an adorner, but inherits the abstract interface defined earlier.

3. Implement specific components for an abstract interface.

4. Implement the specific decorator, each time the implementation is to pass the abstract interface object. This makes it convenient for the decorator to call the decorator's method.

5. The relationship between the component and the decorator is 1:n


Code


Component Abstraction Interface (decorator)


Public abstract class Beverage {
	String description = "Unknow beverage";
	
	Public String getdescription () {
		return description;
	}
	
	public abstract double Cost ();
}

Decorator Abstract Interface


Public abstract class Condimentdecorator extends beverage {public

	abstract String getdescription ();


Component-Specific implementation class


public class Darkroast extends beverage {public

	darkroast () {
		description = "Dark roast";
	}
	@Override public
	double Cost () {
		//TODO auto-generated method stub
		return 1.11;
	}

}

public class Decat extends beverage {

	/* (non-javadoc)
	 * @see com.sprd.pattern.decorator.beverage#cost ()
	 * /
	@Override public
	double Cost () {
		//TODO auto-generated method stub
		return 2.3;
	}

}

public class Espresso extends beverage {public
	Espresso () {
		Description = ' house Blend Coffee ';
	}

	@Override public
	double Cost () {
		return 1.99;
	}

}

public class Houseblend extends beverage {public
	houseblend () {
		Description = ' house Blend ';
	}

	@Override public
	double Cost () {
		//TODO auto-generated method Stub
		return.



Decorator Concrete Implementation


public class Mocha extends Condimentdecorator {
	beverage beverage;

	Public Mocha (Beverage beverage) {
		this.beverage = beverage;
	}

	@Override public
	String getdescription () {
		return beverage.getdescription () + ", Mocha";
	}

	
	@Override public
	double Cost () {
		return 0.2 + beverage.cost ();
	}

}

public class Soy extends Condimentdecorator {
	beverage beverage;
	Public Soy (Beverage beverage) {
		this.beverage = beverage;
	}

	@Override public
	String getdescription () {
		return beverage.getdescription () + ", Soy";
	}

	@Override public
	double Cost () {
		//TODO auto-generated method Stub
		return. 3 + beverage.cost ();
	}

}

public class Whip extends Condimentdecorator {
	beverage beverage;

	Public Whip (Beverage beverage) {
		this.beverage = beverage;
	}

	@Override public
	String getdescription () {
		return beverage.getdescription () + ", Whip";
	}

	@Override public
	double Cost () {
		return. 2 + beverage.cost ();
	}

}


Test program


public class Starbuzzcoffee {public

	static void Main (string[] args) {
		//dot a cup of espresso, nothing added
		beverage Espresso = NE W Espresso ();
		System.out.println (String.Format ("%s &%s", Espresso.getdescription (), Espresso.cost ()));
		Order a cup of deep roasted coffee
		beverage darkroast = new Darkroast ();
		Gamoca
		darkroast = new Mocha (darkroast);
		Double Mocha
		darkroast = new Mocha (darkroast);
		Add milk bubble
		darkroast = new Whip (darkroast);
		
		System.out.println (String.Format ("%s &%s", Darkroast.getdescription (), Darkroast.cost ()));
		
		Beverage houseblend = new Houseblend ();
		Add soy milk
		houseblend = new Soy (houseblend);
		Gamoca
		houseblend = new Mocha (houseblend);
		Add milk bubble
		houseblend = new Whip (houseblend);
		System.out.println (String.Format ("%s &%.2f", Houseblend.getdescription (), Houseblend.cost ()));
	}

}

Output results


House Blend Coffee & 1.99
Dark Roast, Mocha, mocha,whip & 1.71 House
Blend,soy, Mocha,whip & 1.59


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.