Decoration Mode 1

Source: Internet
Author: User

[keywords]:java,design pattern, design pattern, Java and pattern learning, decorator, decoration mode

[Environment]:staruml5.0 + JDK6

[Author]:winty (wintys@gmail.com)

[Body]:

 package pattern.decorator; /** * Decoration Mode: Decorator pattern * @version 2009-6-5 * @author winty (wintys@gmail.com)/public class Decoratortest
        {public static void main (string[] args) {Component Component;
        component = new Concretecomponent ();
        Component.dosomething ();
        
        System.out.println ("");
        Transparent decorative mode Component decorator;
        Decorator = new Concretedecorator (component);
        Decorator.dosomething ();
        System.out.println ("");
        Translucent decorative pattern concretedecorator decorator2;
        Decorator2 = new Concretedecorator (component);
        Decorator2.dosomething ();
    Decorator2.dootherthings ();
  }/** * Abstract widget: Component * * Interface component{public abstract void dosomething (); /** * Concrete Widget: Concretecomponent/class Concretecomponent implements component{@Override public void Doso
    Mething () {System.out.println ("do something");
 }   }   /** * Decoration: Decorator */abstract class Decorator implements component{private Component comp;
    Public decorator (Component comp) {this.comp = comp;
    @Override public void DoSomething () {comp.dosomething (); }/** * Specific decoration: Concretedecorator */class Concretedecorator extends decorator{public concretedecorator (Comp
    Onent comp) {super (comp);
        @Override public void DoSomething () {super.dosomething ();
    Domorethings ();
    private void Domorethings () {System.out.println ("do more things.");
     /** * The new method is not part of the component interface, * so it cannot be accessed transparently through the component interface.
    */public void dootherthings () {System.out.println ("does other things."); }} 

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.