Synthetic mode-Transparent

Source: Internet
Author: User

[keywords]:java,design pattern, design pattern, Java and Schema learning, composite, synthetic mode

[Environment]:staruml5.0 + JDK6

[Author]:winty (wintys@gmail.com) http://www.blogjava.net/wintys/

[Body]:

Transparent synthesis mode

Package pattern.composite.transparent;
Import java.util.*; /** * Transparent synthesis mode: Composite pattern * @version 2009-6-3 * @author winty (wintys@gmail.com)/public class Transparentcompo
        sitetest{public static void Main (string[] args) {Graphics G1, G2;

        Graphics P1, p2;
        G1 = new Line ();
        G2 = new Circle ();
        P1 = new picture ();

        P2 = new picture ();
        G1.draw ();
        G2.draw ();

        System.out.println ("");
        P1.add (G1);
        P1.add (G2);
        P1.draw ();

        System.out.println ("");
        P2.add (G1);
        P2.add (G2);
        P2.add (p1);
    P2.draw ();

    }/** * Abstract component role/interface graphics{void Draw ();
    void Add (Graphics g);
    void Remove (int i);
Graphics getchild (int i); /** * Leaf Role */class line implements graphics{@Override public void Draw () {System.out.println ("dra
    W a line "); @Override public void Add (Graphics g) {} @OverridE public void Remove (int i) {} @Override public Graphics getchild (int i) {return null; }/** * Leaf role */class Circle implements graphics{@Override public void Draw () {System.out.println (
    "Draw a Circle"); 
    @Override public void Add (Graphics g) {} @Override public void remove (int i) {} @Override
    Public Graphics getchild (int i) {return null;

    }/** * Composite role/class picture implements graphics{private list<graphics> Graphics;
    Public picture () {graphics = new arraylist<graphics> ();
        @Override public void Draw () {iterator<graphics> it = graphics.iterator ();
            while (It.hasnext ()) {Graphics g = it.next ();
        G.draw ();
    @Override public void Add (Graphics g) {Graphics.add (g);
    @Override public void Remove (int i) {graphics.remove (i); } @OvErride public Graphics getchild (int i) {return graphics.get (i); }
}

Run Result:

Draw a line

Draw a Circle

Draw a line

Draw a Circle

Draw a line

Draw a Circle

Draw a line

Draw a Circle

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.