First, the concept
Combined mode allows you to combine objects into a tree structure to represent a "whole/part" hierarchy. The combination enables the customer to handle individual objects and object combinations in a consistent manner.
Second, UML diagram
1.Component (object interface) that defines what an object can do.
2.Leaf (leaf node object)
3.Composite (Other node objects, including other nodes or leaves).
Iii. examples
Courier companies generally have hierarchical structure
/*** Shun Fung Company Abstract class * Defines what the company can do *@authorEason **/ Public Abstract classSfcompany {//Company Name protectedString name; //company Level protected intgrade; PublicSfcompany (String name,intgrade) { This. Name =name; This. grade =grade; } //Add a subordinate company Public Abstract voidAdd (sfcompany SF); //Delete a subordinate company Public Abstract voidRemove (sfcompany SF); //Print Yourself Public voiddisplay () { for(intI=1; i<=grade; i++) {System.out.print ("-----"); } System.out.println (name); } }/*** The lowest-level company *@authorEason **/ Public classSftailcompanyextendssfcompany{ PublicSftailcompany (String name,intgrade) { Super(name, grade); } @Override Public voidAdd (sfcompany sf) {Throw Newunsupportedoperationexception (); } @Override Public voidRemove (sfcompany sf) {Throw Newunsupportedoperationexception (); } //already mentioned in the parent class//@Override//Public void Display () {//for (int i=1; i<=grade; i++) {//System.out.print ("-----");// }//System.out.println (name);// }}/*** Shun Fung Company with subordinate company *@authorEason **/ Public classSfheadcompanyextendssfcompany{//Save subordinate company PrivateList<sfcompany> Sfcompanys =NewArraylist<sfcompany>(); PublicSfheadcompany (String name,intgrade) { Super(name, grade); } @Override Public voidAdd (sfcompany sf) {sfcompanys.add (SF); } @Override Public voidRemove (sfcompany sf) {sfcompanys.remove (SF); } //Print yourself first, print your own subordinates@Override Public voiddisplay () {//already mentioned in the parent class, use Super.display ();//for (int i=1; i<=grade; i++) {//System.out.print ("-----");// }//System.out.println (name); Super. display (); for(Sfcompany sf:sfcompanys) {sf.display (); } }}/*** Test class *@authorEason **/ Public classTestcompostite { Public Static voidMain (string[] args) {Sfcompany head=NewSfheadcompany ("head Office", 1); Sfcompany Shenzhen=NewSfheadcompany ("Shenzhen branch", 2); Sfcompany Wuhan=NewSfheadcompany ("Wuhan branch", 2); Sfcompany Beijing=NewSfheadcompany ("Beijing branch", 2); Sfcompany Wuchang=NewSfheadcompany ("Wuchang branch", 3); Sfcompany Baoan=NewSfheadcompany ("Baoan branch", 3); Sfcompany Luohu=NewSfheadcompany ("Lo Wu Branch", 3); Sfcompany Nanshan=NewSfheadcompany ("Nanshan Branch", 3); Head.add (Shenzhen); Head.add (Wuhan); Head.add (Beijing); Wuhan.add (Wuchang); Shenzhen.add (Baoan); Shenzhen.add (Luohu); Shenzhen.add (Nanshan); //removing Wuhan//Head.remove (Wuhan);Head.display (); }}
Iv. use of the scene
1. The need to reflect the overall and part of the hierarchical relationship, and users want to ignore the whole and part of the difference, unified use of the whole object and part of the object, you should consider the combination mode.
2. Combination mode allows you to optimize the processing of recursive or hierarchical data structures. such as the file system structure.
Design mode--combined mode composite (structural type)