Java design pattern (3) decoration pattern

Source: Internet
Author: User

Decoration mode: dynamically attaches the responsibility to the object. To expand the function, the decoration provides another option that is different from inheritance. 1. Create a top-level class [html] package com. pattern. decorate; public abstract class Beverage {String description = "Unknown Beverage"; public String getDescription () {return description;} public abstract double cost ();} 2, create a product class [html] package com. pattern. decorate; public class Espresso extends Beverage {public Espresso () {description = "Espresso" ;}/ *** @ see com. pattern. decorate. beverage # cost () */@ Override publ Ic double cost () {return 1.99 ;}} [html] package com. pattern. decorate; public class HouseBlend extends Beverage {public HouseBlend () {description = "House Blend Coffee";}/*** @ see com. pattern. decorate. beverage # cost () */@ Override public double cost () {return. 89 ;}} [html] package com. pattern. decorate; public class DarkRoast extends Beverage {public DarkRoast () {description = "DarkRoast "; }/*** @ See com. pattern. decorate. beverage # cost () */@ Override public double cost () {return 2.99 ;}} 3. Create a decoration class [html] package com. pattern. decorate; public abstract class CondimentDecorator extends Beverage {public abstract String getDescription ();} 4. Create a decoration class specific implementation class [html] package com. pattern. decorate; public class Whip extends CondimentDecorator {public Beverage beverage; public Whip (Beverage beverag E) {this. beverage = beverage;}/*** @ see com. pattern. decorate. condimentDecorator # getDescription () */@ Override public String getDescription () {return beverage. getDescription () + ", Whip";}/*** @ see com. pattern. decorate. beverage # cost () */@ Override public double cost () {return. 30 + beverage. cost () ;}} [html] package com. pattern. decorate; public class Mocha extends CondimentDecorator {Beverage Beverage; public Mocha (Beverage beverage) {this. beverage = beverage;}/*** @ see com. pattern. decorate. condimentDecorator # getDescription () */@ Override public String getDescription () {return beverage. getDescription () + ", Mocha";}/*** @ see com. pattern. decorate. beverage # cost () */@ Override public double cost () {return. 20 + beverage. cost () ;}5, test [html] package com. pattern. decorate; public clas S 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 ();} The decoration class has a common parent class with the decorated class. In fact, it does not have the same parent class!

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.