Security Mode and transparent mode of Composite

Source: Internet
Author: User

Composite): Combine objects into a tree structure to represent the "part-whole" hierarchy, so that users can use a single object and a composite object in a consistent manner.

Use Cases:

1. It is used for the part of an object-the overall hierarchy, such as the tree menu, folder menu, and department organizational structure;

2. Hide the differences between a composite object and a single object so that you can use all objects in the composite structure in a unified manner.

Generic class diagram:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/14205L239-0.jpg "/>

 

Speaking of trees, the course "Data Structure" has been learned, and tree traversal is the most important. The following is a simple code implementation based on the class diagram.

// Abstract Component class and node class abstract class Component {public String name; public Component (String name) {this. name = name;} // public Operation public void getName () {System. out. println (this. name );}}


// Class Composite extends Component {private Component list <Component> children; public Composite (String name) {super (name); this. children = new Component list <Component> ();} // add a node, which may be a branch or leaf public void add (Component child) {this. children. add (child) ;}// delete a node, which may be a branch or leaf public void remove (String child) {this. children. remove (child) ;}// obtain the public writable list of the subtree <Component> getChildren () {return this. children ;}}


// Leaf Component class Leaf extends Component {public Leaf (String name) {super (name );}}


// Test class, responsible for building the entire tree public class Client {public static void main (String [] args) {Composite root = new Composite ("root "); composite branch01 = new Composite ("branches 01"); Composite branch02 = new Composite ("branches 02"); root. add (branch01); root. add (branch02); Component leaf01 = new Leaf ("Leaf 01"); Component leaf02 = new Leaf ("Leaf 02 "); component leaf03 = new Leaf ("Leaf 03"); Component leaf04 = new Leaf ("Leaf 04"); Component leaf05 = new Leaf ("Leaf 05 "); branch01.add (leaf01); branch01.add (leaf02); branch02.add (leaf03); branch02.add (leaf04); branch02.add (leaf05); displayTree (root );} // recursively traverse the entire tree public static void displayTree (Composite root) {Component list <Component> children = root. getChildren (); for (Component c: children) {if (c instanceof Leaf) {System. out. print ("\ t"); c. getName ();} else {c. getName (); // recursive displayTree (Composite) c );}}}}


Test results:

Branches 01 leaves 01 leaves 02 branches 02 leaves 03 leaves 04 leaves 05


The above Client-class tree building code is annoying. If the entire tree has hundreds of nodes, the efficiency would be too bad. In fact, in practical applications, it is not like this to manually build a complex tree. We should alreadyStore the node content and logical relationships of the entire tree in the database table.More importantly, this input should not be done by our developers. Because each node record in the table stores some of its own information, including whether it is a leaf or a parent node, what developers need is to make the programReads records from tables in the database to build the entire tree..

In addition, the Code above can only traverse from the root node, but cannot traverse from a node. To solve this problem, you can add a parent attribute to the Component class of the abstract Component class, add the setParent () and getParent () methods. The specific implementation of different traversal methods is complete.

The class diagram above belongs to the safe mode, because the Leaf class does not have add, remove, and other methods. These methods are placed to the Composite class node class).

To implement the transparent mode, the class diagram is as follows:

650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/14205KK9-1.jpg "/>

 

The difference is that the methods such as add and remove are added to the Component of the abstract Component class. In this case, the Leaf class must assign the inherited add, remove, and other unavailable and illogical methodsAnnotationDeprecated and throw an exception. It seems that this transparent mode is more troublesome and there is nothing to do with it. In fact, in this modeWhen traversing the entire tree, no forced type conversion is allowed.. Let's take a look at the displayTree () method above, in which Composite) c is used for forced type conversion using recursive time.

The Leaf class code is as follows:

// Leaf Component class Leaf extends Component {public Leaf (String name) {super (name );}@Deprecated// Throw an unsupported operation exception public void add (Component child) throws UnsupportedOperationException {throws new UnsupportedOperationException ();}@DeprecatedPublic void remove (String child) throws UnsupportedOperationException {throws new UnsupportedOperationException ();}@DeprecatedPublic parameter list <Component> getChildren () throws UnsupportedOperationException {throws new UnsupportedOperationException ();}}


For details about how to use the security mode or transparent mode, see the specific situation.

This article is from the "ant" blog, please be sure to keep this source http://haolloyin.blog.51cto.com/1177454/347308

Related Article

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.