Discussion on design pattern of Java adorner _java

Source: Internet
Author: User
Tags stub

This essay mainly introduces the implementation of a simple decorator design pattern in Java:

Let's look at the class diagram of the adorner design pattern:


As you can see from the diagram, we can decorate any implementation class of the component interface, and these implementation classes also include the adorner itself, and the adorner itself can be decorated again.

The following is a simple decorator design pattern implemented in Java, which provides a starting point for adding coffee and adding milk, chocolate, and sugar to the adorner system.

Interface Component {void method ().} class Coffee implements Component {@Override public void method () {//
  TODO auto-generated Method stub System.out.println ("Pour coffee");
  } class Decorator implements Component {public Component comp;
  Public decorator (Component comp) {this.comp = comp;
  @Override public void Method () {//TODO auto-generated Method Stub Comp.method ();
  } class Concretedecoratea extends decorator {public Component comp;
    Public Concretedecoratea (Component comp) {super (comp);
  This.comp = comp;
  public void Method1 () {System.out.println ("pour milk");
  public void Method2 () {System.out.println ("Add Sugar");
    public void Method () {Super.method ();
    Method1 ();
  Method2 ();
  } class Concretedecorateb extends decorator {public Component comp;
    Public Concretedecorateb (Component comp) {super (comp);
  This.comp = comp; public void Method1 () {System.out.println ("Join ChuckForce ");
    public void Method () {Super.method ();
  Method1 ();
    } public class Testdecoratepattern {public static void main (string[] args) {Component comp = new Coffee ();
    Comp.method ();
    System.out.println ("--------------------------------------------------");
    Component comp1 = new Concretedecoratea (comp);
    Comp1.method ();
    System.out.println ("--------------------------------------------------");
    Component comp2 = new Concretedecorateb (COMP1);
    Comp2.method ();
    System.out.println ("--------------------------------------------------");
    Component Comp3 = new Concretedecorateb (new Concretedecoratea (New Coffee ()));
    Comp3.method ();
    System.out.println ("--------------------------------------------------");
    Component comp4 = new Concretedecoratea (new Concretedecorateb (New Coffee ()));
  Comp4.method ();

 }
}

Run Result:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.