Common design patterns-decorative patterns]

Source: Internet
Author: User

Decoration mode: as its name implies, it adds layers of coats with him, or adds more adjectives to an object.

Please refer to the demo to show you at a glance.

Package decorator;/*** @ description: decoration mode * @ author Potter * @ date 05:35:07 * @ version V1.0 */public class app {/*** @ Param ARGs */public static void main (string [] ARGs) {coffee = New Tang (New NaI (New Natie (); system. out. println ("coffee's cost =" + coffee. getcost (); system. out. println ("coffee's message =" + coffee. getmessage ());}}

Coffee class:

Package decorator;/*** @ Description: Coffee class * @ author Potter * @ date 05:21:35 * @ version V1.0 */public interface coffee {public float getcost (); public String getmessage ();}

Decoration core class:

Package decorator;/*** @ Description: decoration mode core * @ author Potter * @ date 05:21:14 * @ version V1.0 */public class decorator implements coffee {protected coffee; public decorator (coffee) {This. coffee = coffee ;}@ overridepublic float getcost () {return coffee. getcost () ;}@ overridepublic string getmessage () {return coffee. getmessage ();}}

Physical Blue Mountains coffee:

Package decorator;/*** @ Description: blue coffee * @ author Potter * @ date 05:25:46 * @ version V1.0 */public class lanshan implements coffee {@ overridepublic float getcost () {return 20f ;}@ overridepublic string getmessage () {return "lanshan coffee ";}}

Entity latte coffee:

Package decorator;/*** @ Description: Latte * @ author Potter * @ date 2012-9-10 05:23:53 * @ version V1.0 */public class Natie implements coffee {@ overridepublic float getcost () {return 10f ;}@ overridepublic string getmessage () {return "latte ";}}

Decorative Milk:

Package decorator;/*** @ Description: Milk * @ author Potter * @ date 05:33:13 * @ version V1.0 */public class Nai extends decorator {public NaI (coffee) {super (coffee) ;}@ overridepublic float getcost () {return Super. getcost () + 5 ;}@ overridepublic string getmessage () {return "Milky" + super. getmessage ();}}

Decorative SUGAR:

Package decorator;/*** @ Description: sugar * @ author Potter * @ date 05:30:49 * @ version V1.0 */public class tang extends decorator {public Tang (coffee) {super (coffee) ;}@ overridepublic string getmessage () {return "SWEETENED" + super. getmessage ();}}

Running result:

Coffee's cost = 15.0
Coffee's message = latte with sugar and milk

Haha ~ It's easy to understand!

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.