Java and mode 26-8th-merging mode

Source: Internet
Author: User
Document directory
  • Source code
  • Source code

The merging mode belongs to the object structure mode, and is sometimes called the "part-whole" mode. The merging mode organizes objects into the tree structure and can be used to describe the relationship between the whole and the part. The merging mode allows the client to view elements as compound elements.

Merging Mode

  The merging mode uses the tree structure to represent the relationship between the part and the whole. The merging mode allows the client to view individual component objects in the same way as the compositing objects composed of them.

For example, a file system is a typical synthesis mode system. Is part of a common XP file system.

It can be seen that the file system is a tree structure with nodes on the tree. There are two types of Tree nodes. One is a tree branch node, that is, a directory with an internal tree structure, which is colored in the figure; the other is a file, that is, a leaf node without an internal tree structure.

Obviously, directories and files can be treated and processed as the same object, which is the application of the merging mode.

The merging mode does not provide a parent object management method. However, the merging mode must provide sub-object management methods, such as add (), remove (), and getchild (), where appropriate ().

The implementation of the merging mode is divided into two forms based on the differences between the Implemented interfaces.SecurityAndTransparent.

Structure of safe synthesis Mode

The merging mode of the security mode requires that the management clustering method only appears in the tree component class, but not in the leaf component class.

This form involves three roles:

●Abstract component role:This is an abstract role that defines public interfaces and default behaviors for the objects in the combination and can be used to manage all sub-objects. A merging Object usually treats its sub-objects as component objects. In a safe synthesis mode, the component role does not define a method for managing sub-objects. This definition is provided by the Branch component object.

●Leaf component (leaf) role:A leaf object is an object without sub-objects. It defines the behavior of the original object that participates in the combination.

●Composite role:Indicates the object with sub-objects in the combination. The branches component class provides all methods for managing sub-objects, such as add (), remove (), and getchild ().

Source code

Abstract component role

Package COM. bankht. composite;/*** @ Author: -AK47 * @ Creation Time: 09:56:57, December 25, ** @ Class description: abstract component Angle class */public interface component {/*** output the name of the component */Public void printstruct (string prestr );}

 

Tree Branches

Package COM. bankht. composite; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 09:57:27, December 25, ** @ Class description: tree Branches component Angle class */public class composite implements component {/*** used to store the child component objects contained in the composite object */private list <component> childcomponents = new arraylist <component> (); /*** name of the composite object */private string name;/*** constructor, input the name of the composite object ** @ Param name * Name of the composite object */Public composite (string n AME) {This. name = Name;}/*** aggregation management method. Add a sub-component object ** @ Param child * sub-component object */Public void addchild (Component child) {childcomponents. add (child);}/*** aggregation management method, delete a sub-component object ** @ Param Index * sub-component object subscript */Public void removechild (INT index) {childcomponents. remove (INDEX);}/*** aggregation management method, returns all child component objects */public list <component> getchild () {return childcomponents ;} /***** the prefix of the output object's own structure ** @ Param prestr *. It mainly concatenates spaces by level to implement backward Indent */@ overridepublic void printstruct (string prestr) {// output system first. out. println (prestr + "+" + this. name); // if the child widget contains child widgets, the object if (this. childcomponents! = NULL) {// Add two spaces, indicating to indent two spaces backward: prestr + = ""; // output the sub-object of the current object for (component C: childcomponents) {// recursively output each sub-Object C. printstruct (prestr );}}}}

 

Role color of leaf Components

Package COM. bankht. composite;/*** @ Author: -AK47 * @ Creation Time: 09:58:17, December 25, ** @ Class description: leaf component Angle class */public class leaf implements component {/*** name of the leaf object */private string name;/*** constructor, input the name of the leaf object ** @ Param name * Name of the leaf object */Public leaf (string name) {This. name = Name;}/*** structure of the output leaf object. The leaf object has no sub-object, that is, the name of the output leaf object ** @ Param prestr * prefix, it is mainly based on Spaces spliced by layers to achieve backward indent */@ overridepublic void printstruct (string prestr) {system. out. println (prestr + "-" + name );}}

Client type

Package COM. bankht. composite;/*** @ Author: -AK47 * @ Creation Time: 09:58:50, December 25, ** @ Class description: client class */public class client {public static void main (string [] ARGs) {composite root = new composite ("clothing "); composite C1 = new composite ("men's wear"); composite C2 = new composite ("Women's Wear"); leaf leaf1 = new leaf ("shirt "); leaf leaf2 = new leaf ("jacket"); leaf leaf3 = new leaf ("skirt"); leaf leaf4 = new leaf ("set"); root. addchild (C1); root. addchild (C2); c1.addchild (leaf1); c1.addchild (leaf2); c2.addchild (leaf3); c2.addchild (leaf4); root. printstruct ("");}}

Running result:

+ Clothing + men's wear-shirts-jackets + Women's Wear-skirts-Suits

 

It can be seen that the composite class provides descriptions and implementations of addchild (), removechild (), and getchild () methods, the leaf component class does not provide the declaration or implementation of these methods. This approach is safe. Due to this feature, client applications cannot mistakenly call the clustering methods of leaf components, because leaf components do not have these methods, calling will lead to compilation errors.

The disadvantage of the Safe synthesis mode is that it is not transparent, because the leaf and branch classes will have different interfaces.

Structure of transparent synthesis Mode

Different from the safe synthesis mode, the transparent synthesis mode requires that all specific component classes, regardless of the branches or leaves, comply with a fixed interface.

Source code

Abstract component role

Package COM. bankht. composite. transparent; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 10:10:55, December 25, ** @ Class description: abstract component role class */public abstract class component {/*** output form own name */public abstract void printstruct (string prestr);/*** aggregation management method, add a sub-component object ** @ Param child * sub-component object */Public void addchild (Component child) {/*** default implementation and throw an exception, because the leaf object does not have this function or the child component does not implement this function */throw new unsupportedoperationexception ("the object does not support this function");}/*** clustering management method, delete a sub-component object ** @ Param Index * subcomponent object subscript */Public void removechild (INT index) {/*** default implementation, throwing an exception, because the leaf object does not have this function or the child component does not implement this function */throw new unsupportedoperationexception ("the object does not support this function");}/*** clustering management method, returns the default implementation of all sub-component objects */public list <component> getchild () {/*** and throws an exception, because the leaf object does not have this function or the child component does not implement this function */throw new unsupportedoperationexception ("the object does not support this function ");}}

 

Tree branches, which change implements conponent to extends conponent.

Package COM. bankht. composite. transparent; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 10:11:20, December 25, ** @ Class description: corner class of the tree branches component. This class changes implements conponent to extends conponent, there is no change elsewhere. */Public class composite extends component {/*** is used to store the child component objects contained in the composite objects */private list <component> childcomponents = new arraylist <component> (); /*** name of the composite object */private string name;/*** constructor, input the name of the composite object ** @ Param name * Name of the composite object */Public composite (string name) {This. name = Name;}/*** aggregation management method. Add a sub-component object ** @ Param child * sub-component object */Public void addchild (Component child) {childcomponents. add (child);}/*** aggregation Management Method to delete the subcomponent object ** @ Param Index * subcomponent object subscript */Public void removechild (INT index) {childcomponents. remove (INDEX);}/*** aggregation management method, returns all child component objects */public list <component> getchild () {return childcomponents ;} /*** the prefix of the output object's own structure ** @ Param prestr *. It is mainly based on hierarchical space splicing to achieve backward indent */@ overridepublic void printstruct (string prestr) {// output system first. out. println (prestr + "+" + this. name); // if the child widget contains child widgets, the object if (this. childcomp Onents! = NULL) {// Add two spaces, indicating to indent two spaces backward: prestr + = ""; // output the sub-object of the current object for (component C: childcomponents) {// recursively output each sub-Object C. printstruct (prestr );}}}}

 

The role color of the leaf component. This class changes implements conponent to extends conponent, which remains unchanged elsewhere.

Package COM. bankht. composite. transparent;/*** @ Author: -AK47 * @ Creation Time: 10:11:56, December 25, ** @ Class description: corner class of leaf component. This class changes implements conponent to extends conponent, there is no change elsewhere. */Public class leaf extends component {/*** name of the leaf object */private string name;/*** constructor, input the name of the leaf object ** @ Param name * Name of the leaf object */Public leaf (string name) {This. name = Name;}/*** structure of the output leaf object. The leaf object has no sub-object, that is, the name of the output leaf object ** @ Param prestr * prefix, it is mainly based on Spaces spliced by layers to achieve backward indent */@ overridepublic void printstruct (string prestr) {// todo auto-generated method stubsystem. out. println (prestr + "-" + name );}}

 

The main change in client classes is that composite objects and leaf objects are no longer distinguished.

Package COM. bankht. composite. transparent;/*** @ Author: -AK47 * @ Creation Time: 10:12:22, December 25, ** @ Class description: The main change of the client class is that the composite object and leaf object are no longer distinguished. */Public class client {public static void main (string [] ARGs) {component root = new composite ("clothing"); component C1 = new composite ("men's wear "); component C2 = new composite ("Women's Wear"); component leaf1 = new leaf ("shirt"); component leaf2 = new leaf ("jacket "); component leaf3 = new leaf ("skirt"); component leaf4 = new leaf ("set"); root. addchild (C1); root. addchild (C2); c1.addchild (leaf1); c1.addchild (leaf2); c2.addchild (leaf3); c2.addchild (leaf4); root. printstruct ("");}}

 

It can be seen that the client does not need to distinguish whether the operation is a composite or leaf. for the client, the operation is a component object.

Selection of two implementation methods

  The security synthesis mode mentioned here refers:Check whether the client uses the merging mode to ensure security. If the mode is secure, no misoperation may occur. All accessible methods are supported.

  The transparent synthesis mode here refers:Whether the client uses the merging mode to identify whether it is a "tree object" or a "leaf object ". If it is transparent, there is no need to distinguish between them. For customers, they are all compoent objects. The specific types are transparent to the client and do not need to be concerned.

In terms of security and transparency,More emphasis on transparencyAfter all, the purpose of the merging mode is to make the client no longer differentiate whether the operation is a tree object or a leaf object, but operate in a unified way.

In addition, for security implementation, it is necessary to distinguish whether it is a tree object or a leaf object. Sometimes, you need to convert the object type, but find that the type information is lost, so you have to forcibly convert the object. This type conversion is not safe enough.

Therefore, when using the merging mode, we recommend that you use more transparent implementation methods.

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.