Combination mode (composite): The whole and the part are processed in a unified way, and it is used for the processing of tree-structured data.
Divided into 3 parts
1. Containers
2. Leaves
3. Abstract components
//Abstract Components Public InterfaceAbstractfile {//Antivirus voidShadu ();}//Leaf ComponentsclassImageFileImplementsabstractfile{PrivateString name; PublicImageFile (String name) {Super(); This. Name =name; } Public voidShadu () {System.out.println ("Kill image File"); }; }//Container ComponentsclassRongqiImplementsabstractfile{PrivateString name; PrivateList<abstractfile> list=NewArraylist<abstractfile>(); PublicRongqi (String name) {Super(); This. Name =name; } Public voidAdd (abstractfile c) {List.add (c); } Public voidRemove (abstractfile c) {list.remove (c); } PublicAbstractfile Getchild (intindex) { returnList.get (index); } Public voidShadu () {System.out.println ("Folder Antivirus"); //Natural Recursion for(Abstractfile abstractfile:list) {abstractfile.shadu (); } } }
Public Static void Main (string[] args) { new imagefile ("Picture 1"); New ImageFile ("Picture 2"); New Rongqi ("Folder 1"); F0.add (F1); F0.add (F2); F0.shadu (); }
Java design pattern--Combined mode