JAVA Implementation of the composite mode [00 original]

Source: Internet
Author: User

1. Class Diagram

2. Java implementation code package cn.edu. ynu. Sei. composite;

/**
* Abstract component role
*
* @ Author 88250
* @ Version 1.0.0, 2007-9-4
*/
Public interface component
{
/**
* Return your instance
* @ Return
*/
Public composite getcomposite ();

/**
* A Business Method
*/
Public void sampleoperation ();
}

Package cn.edu. ynu. Sei. composite;

Import java. util. enumeration;
Import java. util. vector;

/**
* Branching role
*
* @ Author 88250
* @ Version 1.0.0, 2007-9-4
*/
Public class composite implements component
{
Private vector <component> componentvector = new vector <component> ();

@ Override
Public composite getcomposite ()
{
Return this;
}

@ Override
Public void sampleoperation ()
{
Enumeration <component> enumeration = components ();

While (enumeration. hasmoreelements ())
{
Enumeration. nextelement (). sampleoperation ();
}
}

/**
* Adds a self-component object to the aggregation management method.
* @ Param component
*/
Public void add (Component component)
{
Componentvector. addelement (component );
}

/**
* The aggregation management method deletes a sub-component object.
* @ Param component
*/
Public void remove (Component component)
{
Componentvector. removeelement (component );
}

/**
* The aggregation management method returns the aggregated enumeration object.
* @ Return
*/
Private enumeration <component> components ()
{
Return componentvector. Elements ();
}

}

Package cn.edu. ynu. Sei. composite;

/**
* Leaf role
*
* @ Author 88250
* @ Version 1.0.0, 2007-9-4
*/
Public class leaf implements component
{

@ Override
Public composite getcomposite ()
{
// Todo auto-generated method stub
Return NULL;
}

@ Override
Public void sampleoperation ()
{
// Todo auto-generated method stub

}

}

3. Summary

This is the security form of the merging (combination) mode. Note that the merging mode can be considered only when the "part-whole" relationship is established.

4. References

Design Patterns and Java and Pattern

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.