Decorator mode in Design Mode

Source: Internet
Author: User

Decorator mode definition: dynamically attaches a responsibility to an object. To expand the functionality, the decoration provides an alternative solution that is more flexible than continuing.

Simple definition: Wrap an object to provide new behavior.

The annotator mode can effectively deal with the explosion-like problem.

OO principles:

It is open to extensions and closed to modifications.

Take StarbuzzCoffee in the book as an example:

Beverage. java (Beverage abstract class)

package headfirst.decorator.starbuzz;public abstract class Beverage {String description = "Unknown Beverage";  public String getDescription() {return description;} public abstract double cost();}

CondimentDecorator. java (seasoning abstract class)

package headfirst.decorator.starbuzz;public abstract class CondimentDecorator extends Beverage {public abstract String getDescription();}

DarkRoast. java (beverage class)

package headfirst.decorator.starbuzz;public class DarkRoast extends Beverage {public DarkRoast() {description = "Dark Roast Coffee";} public double cost() {return .99;}}


Espresso. java

package headfirst.decorator.starbuzz;public class Espresso extends Beverage {  public Espresso() {description = "Espresso";}  public double cost() {return 1.99;}}

HouseBlend. java

package headfirst.decorator.starbuzz;public class HouseBlend extends Beverage {public HouseBlend() {description = "House Blend Coffee";} public double cost() {return .89;}}

Mocha. java (Specific spices class)

package headfirst.decorator.starbuzz;public class Mocha extends CondimentDecorator {Beverage beverage; public Mocha(Beverage beverage) {this.beverage = beverage;} public String getDescription() {return beverage.getDescription() + ", Mocha";} public double cost() {return .20 + beverage.cost();}}

Soy. java

package headfirst.decorator.starbuzz;public class Soy extends CondimentDecorator {Beverage beverage;public Soy(Beverage beverage) {this.beverage = beverage;}public String getDescription() {return beverage.getDescription() + ", Soy";}public double cost() {return .15 + beverage.cost();}}

Whip. java

package headfirst.decorator.starbuzz; public class Whip extends CondimentDecorator {Beverage beverage; public Whip(Beverage beverage) {this.beverage = beverage;} public String getDescription() {return beverage.getDescription() + ", Whip";} public double cost() {return .10 + beverage.cost();}}

Test class: StarbuzzCoffee. java

package headfirst.decorator.starbuzz;public class StarbuzzCoffee { public static void main(String args[]) {Beverage beverage = new Espresso();System.out.println(beverage.getDescription() + " $" + beverage.cost()); Beverage beverage2 = new DarkRoast();beverage2 = new Mocha(beverage2);beverage2 = new Mocha(beverage2);beverage2 = new Whip(beverage2);System.out.println(beverage2.getDescription() + " $" + beverage2.cost()); Beverage beverage3 = new HouseBlend();beverage3 = new Soy(beverage3);beverage3 = new Mocha(beverage3);beverage3 = new Whip(beverage3);System.out.println(beverage3.getDescription() + " $" + beverage3.cost());}}


Output:

Espresso $1.99Dark Roast Coffee, Mocha, Mocha, Whip $1.49House Blend Coffee, Soy, Mocha, Whip $1.34


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.