In the book "Java and Patterns" of Dr. Shanhong, this describes the composition (Composite) pattern:
The composition pattern belongs to the structure pattern of the object, sometimes called the "partial-whole" pattern. Compositing mode organizes objects into a tree structure that can be used to describe the relationship between the whole and the part. Compositing mode allows the client to treat simple elements in the same way as composite elements.
compositing mode
The composition mode represents the relationship of the part and the whole with the tree structure. The compositing mode allows the client to treat a single constituent object in the same way as a composite object composed of them.
For example, a file system is a typical synthetic mode system. is part of the common computer XP file system.
As you can see, the file system is a tree structure with nodes on the tree. There are two nodes of a tree, one is a branch node, that is, a directory, there is an internal tree structure, color is painted in the picture, and the other is the file, that is, the leaf node, there is no internal tree structure.
Obviously, directories and files can be treated and treated as the same object, which is the application of synthetic patterns.
The composition mode may not provide the management method of the parent object, but the composition mode must provide the child object's management method in the appropriate place, such as: Add (), remove (), and Getchild () and so on.
The implementation of the synthetic mode is divided into two forms according to the interface of the implementation, which is called the safe and transparent type respectively.
Structure of the Safe synthesis mode
The synthetic mode of Safe mode requires that the method of managing aggregation only appears in the tree component class, not in the leaf component class.
This is a form that involves three roles:
abstract Component (Component) Role: This is an abstract role that defines a common interface and its default behavior for participating objects, which can be used to manage all child objects. A composition object typically takes a sub-object it contains as an object of type component. In a secure synthetic mode, the component role does not define a method for managing sub-objects, which is given by the tree member object.
Leaf component (leaf) role: A Leaf object is an object that has no subordinate sub-objects and defines the behavior of the original object that participates in the composition.
Branch Component (Composite) Role: represents an object that participates in a group of subordinate child objects. The branch component class gives all the methods for managing sub-objects, such as Add (), remove (), and Getchild ().
Source
Abstract Component Role Classes
Public interface Component { /** * Output The name of the build itself * /public void printstruct (String prestr);}
Branch component Role Class
public class Composite implements Component {/** * is used to store sub-component objects contained in a composite object */Private list<component> Child components = new arraylist<component> (); /** * Combination Object name */private String name; /** * Construction method, the name of the combined object is passed into the name of the * @param name of the combination object */public Composite (String name) {this.name = name; }/** * Aggregation management method, add a sub-artifact object * @param Child Widget Object */public void AddChild (Component child) {Childco Mponents.add (child); }/** * Aggregation management method, delete a sub-artifact Object * @param index sub-widget object subscript */public void removechild (int index) {Childcomp Onents.remove (index); }/** * Aggregation management method, return all child artifact objects */public list<component> Getchild () {return childcomponents; }/** * Output object's own structure * @param prestr prefix, mainly in accordance with the level of stitching space, to achieve the backward indent */@Override public void printstruct (String p RESTR) {//First output System.out.println (prestr + "+" + this.name); If a subassembly is also included, output the sub-component objects if (thIs.childcomponents! = null) {//Add two spaces, which means to indent two spaces backwards prestr + = ""; Outputs the current object's sub-object for (Component c:childcomponents) {//recursively outputs each sub-object C.printstruct (prestr ); } } }}
Leaf component Role Class
public class Leaf implements Component { /** * The name of the leaf object */ private String name; /** * Construction method, name of the incoming leaf object * @param name of the leaf object * /Public leaf (String name) { this.name = name; } /** * The 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, mainly in accordance with the level of stitching space, to achieve backward indentation */ @ Override public void Printstruct (String prestr) { //TODO auto-generated method stub System.out.println (Prestr + "-" + Name);} }
Client class
public class Client {public static void Main (String[]args) { Composite root = new Composite ("costume"); Composite C1 = new Composite ("mens"); Composite C2 = new Composite ("ladies"); 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 ("");} }
As can be seen, the Branch component Class (Composite) gives the declaration and implementation of methods such as Addchild (), RemoveChild () and Getchild (), while leaf component classes do not give declarations or implementations of these methods. This is a safe practice, and due to this feature, it is not possible for a client application to mistakenly invoke the clustering method of the leaf component because the leaf component does not have these methods and the call causes a compilation error.
The disadvantage of the secure compositing mode is that it is not transparent enough because the leaf and tree classes will have different interfaces.
Structure of transparent synthesis mode
Unlike the safe synthetic mode, the transparent synthesis mode requires that all concrete component classes, whether branch members or leaf members, conform to a fixed interface.
Source
Abstract Component Role Classes
Public abstract class Component { /** * Output The name of the build itself * /public abstract void Printstruct (String prestr); c5/>/** * Aggregation Management method, add a sub-artifact object * @param child sub-widget Object * /public void AddChild (Component child) { /** * Default implementation, throws an exception, because the leaf object does not have this function * or sub-component does not implement this function */ throw new Unsupportedoperationexception ("object does not support this feature" ); } /** * Aggregation Management method, delete a sub-artifact object * @param index sub-component Object Subscript * /public void removechild (int index) { /** * Default implementation, throws an exception, because the leaf object does not have this function * or sub-component does not implement this function */ throw new Unsupportedoperationexception ("object does not support this feature" ); } /** * Aggregation Management method, returns all child artifact objects * /Public list<component> getchild () { /** * Default implementation, throws an exception, Because the leaf object does not have this function * or the subassembly does not implement this function */ throw new Unsupportedoperationexception ("object does not support this feature");} }
The branch component role class, which changes the implements Conponent to extends conponent and no other places.
public class Composite extends Component {/** * is used to store sub-component objects contained in a composite object */Private list<component> childcom ponents = new arraylist<component> (); /** * Combination Object name */private String name; /** * Construction method, the name of the combined object is passed into the name of the * @param name of the combination object */public Composite (String name) {this.name = name; }/** * Aggregation management method, add a sub-artifact object * @param Child Widget Object */public void AddChild (Component child) {Childco Mponents.add (child); }/** * Aggregation management method, delete a sub-artifact Object * @param index sub-widget object subscript */public void removechild (int index) {Childcomp Onents.remove (index); }/** * Aggregation management method, return all child artifact objects */public list<component> Getchild () {return childcomponents; }/** * Output object's own structure * @param prestr prefix, mainly in accordance with the level of stitching space, to achieve the backward indent */@Override public void printstruct (String p RESTR) {//First output System.out.println (prestr + "+" + this.name); If a subassembly is also included, output the sub-component objects if (this.Childcomponents! = null) {//Add two spaces, which means to indent two spaces backwards prestr + = ""; Outputs the current object's sub-object for (Component c:childcomponents) {//recursively outputs each sub-object C.printstruct (prestr ); } } }}
Leaf component Role class, this class will change implements Conponent to extends Conponent, no change elsewhere.
public class Leaf extends Component { /** * The name of the leaf object */ private String name; /** * Construction method, name of the incoming leaf object * @param name of the leaf object * /Public leaf (String name) { this.name = name; } /** * The 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, mainly in accordance with the level of stitching space, to achieve backward indentation */ @ Override public void Printstruct (String prestr) { //TODO auto-generated method stub System.out.println (Prestr + "-" + Name);} }
The main change in the client class is to no longer differentiate between composite objects and leaf objects.
public class Client {public static void Main (String[]args) { Component root = new Composite ("costume"); Component c1 = new Composite ("mens"); Component c2 = new Composite ("ladies"); 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 ("");} }
As you can see, the client does not need to differentiate between the Branch object (Composite) or the leaf object (leaf); For the client, the Component object is the operation.
Choice of two implementation methods
The security compositing pattern referred to here refers to whether it is more secure from the client using synthetic mode, and if it is safe, there is no possibility of misoperation, and the methods that can be accessed are supported.
the transparency synthesis pattern described here refers to whether the "tree object" or "leaf object" needs to be differentiated from the client using the compositing mode. If it is transparent, it does not have to distinguish between, for the customer, is the Compoent object, the specific type is transparent to the client, is not concerned about.
For compositing mode, transparency is more important in terms of security and transparency, after all, the purpose of compositing mode is to make the client no longer distinguish between tree objects and leaf objects, but rather operate in a uniform way.
And for security implementations, it is necessary to distinguish between tree and leaf objects. Sometimes, you need to convert the object type, but found that the type information is lost, forced to convert, this type conversion is necessarily not safe.
Therefore, in the use of synthetic mode, it is recommended to adopt transparent implementation.
The synthetic mode of Java and pattern