Java files and io--decorator mode

Source: Internet
Author: User

Intention:

Add some extra responsibilities to an object dynamically. For added functionality, the decorator mode is more flexible than generating subclasses. This mode extends the functionality of the object in a transparent manner to the client.

Applicable environment

Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.

Handle the duties that can be revoked.

When a method of generating subclasses cannot be used for expansion. One scenario is that there may be a large number of independent extensions, in order to support each combination will produce a large number of subclasses, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

Involved roles:

Abstract component: Defines an abstract interface to standardize the classes that prepare additional functionality.

Specific component: The class that will be attached to the function, implementing the abstract component role interface.

Abstract Decorator: holds a reference to a specific component role and defines an interface that is consistent with the abstract component role.

Specific decoration: implements the abstract decorator role, which is responsible for adding additional functionality to specific artifacts

Code Implementation :

Drink.java interface of the adorner object

Soyabeanmilk.java Concrete Decorator Objects

Eggdecorator.java Concrete Decorator Object

Sugerdecorator.java Concrete Decorator Object

Blackbeandecorator.java Concrete Decorator Object

Decorator.java Decorator base class

Test.java Test

 package   Com.vince.decorator;  /**   * abstract Adorner interface *   @author   Administrator *  */ public  interface   Drink {  String description ();  public  float   cost ();}  
 package   Com.vince.decorator;  /**   * concrete Adorner class *   @author   Administrator *  */ public  class  Soyabeanmilk implements   drink{ public   String description () { return
     "Soy milk" < span> public  float   cost () { return   3f; }    }
 PackageCom.vince.decorator;/*** Specific Decorator Object *@authorAdministrator **/ Public classSugerdecoratorextendsDecorator { Publicsugerdecorator (Drink Drink) {Super(drink); //TODO Auto-generated constructor stub} @Override PublicString Description () {//TODO Auto-generated method stubs        return Super. Description () + "+ Sugar"; } @Override Public floatCost () {//TODO Auto-generated method stubs        return Super. Cost () +0.5f; }}
 PackageCom.vince.decorator;/*** Specific Decorator Object *@authorAdministrator **/ Public classEggdecoratorextendsDecorator { Publiceggdecorator (Drink Drink) {Super(drink); //TODO Auto-generated constructor stub    }     PublicString Description () {return Super. Description () + "+ eggs"; }     Public floatCost () {return Super. Cost () +3f; }    }
 PackageCom.vince.decorator;/*** Specific Decorator Object *@authorAdministrator **/ Public classBlackbeandecoratorextendsDecorator { Publicblackbeandecorator (Drink Drink) {Super(drink); //TODO Auto-generated constructor stub} @Override PublicString Description () {//TODO Auto-generated method stubs        return Super. Description () + "+ Black Beans"; } @Override Public floatCost () {//TODO Auto-generated method stubs        return Super. Cost () +2f; }}
 PackageCom.vince.decorator; Public classTest {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO Auto-generated method stubsDrink drink=NewSoyabeanmilk (); Sugerdecorator Suger=Newsugerdecorator (drink); Blackbeandecorator Blackbean=NewBlackbeandecorator (Suger); Eggdecorator Egg=NewEggdecorator (Blackbean); System.out.println ("A bit of breakfast" +egg.description ()); System.out.println ("Spend money together" +egg.cost ()); }}
 PackageCom.vince.decorator;/*** Decorator Abstract class *@authorAdministrator **/ Public Abstract classDecoratorImplementsDrink {PrivateDrink Drink;//the object to decorate      PublicDecorator (Drink Drink) { This. drink=drink; } @Override PublicString Description () {//TODO Auto-generated method stubs        returndrink.description (); } @Override Public floatCost () {//TODO Auto-generated method stubs        returnDrink.cost (); }    }

Java files and io--decorator mode

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.