Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall
synthesis (composite) mode is a very important design pattern, which organizes objects into trees to describe the relationship of trees. First, the schematic diagram is visible from the schematic diagram, File, the folder all can treat the reed ifile Equally, provides the great convenience for the object management. Of course, the concept of a tree is not just a hierarchy of file folders, just because of this very image, there are many trees in the actual concept, such as organizational structure, classification level, and so on, are logical concepts, whether physical or logical, in Java are the same processing. Second, the example of a logical tree below, based on the schematic diagram above, to see how to implement and how to use the tree, this structure is very simple, but how to use the tree, traversing the tree, for my use or have a certain degree of difficulty. It is quite difficult to use the recursive traversal of a tree, how to recursively, how to control the traversal level, and how to convert a logical relationship to a (similar) physical relationship. Don't talk nonsense, look at the know. /**
* Created by the IntelliJ idea.
* User:leizhimin
* Date:2008-8-2 16:13:59
* Abstract File role
*/
public interface IFile {
//Return your own instance
ifile getcomposite ();
//A business method
void Sampleoperation ();
//Get depth
int getdeep ();
//Set Depth
void Setdeep (int x);
}import Java.util.Vector;
/**
* Created by the IntelliJ idea.
* User:leizhimin
* Date:2008-8-2 16:15:03
* Folder role
*/
public class Folder implements IFile {
private String name; File name
private int deep; Level depth, root depth is 0
private Vector<ifile> componentvector = new vector<ifile> ();
public Folder (String name) {
this.name = name;
}
//Return your own instance
public ifile Getcomposite () {
return to this;
}
//A business method
public void Sampleoperation () {
System.out.println ("Execute a Business Method!") ");
}
//Add a file or folder
public void Add (IFile ifile) {
componentvector.addelement (ifile);
Ifile.setdeep (this.deep 1);
}
//Delete a file or folder
public void Remove (IFile ifile) {
componentvector.removeelement (ifile);
}
//Return Direct sub-file (folder) collection
public Vector getallcomponent () {
return componentvector;
}
public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
public int Getdeep () {
return deep;
}
public void Setdeep (int deep) {
this.deep = deep;
}
}/**
* Created by the IntelliJ idea.
* User:leizhimin
* Date:2008-8-2 16:27:15
* File
*/
public class File implements IFile {
private String name; File name
private int deep; Level depth
public File (String name) {
this.name = name;
}
//Return your own instance
public ifile Getcomposite () {
return to this;
}
//A business method
public void Sampleoperation () {
System.out.println ("Execute a Business Method!") ");
}
public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
public int Getdeep () {
return deep;
}
public void Setdeep (int deep) {
this.deep = deep;
}
}import Java.util.Iterator;
import Java.util.Vector;
/**
* Created by IntelliJ idea.
* User:leizhimin
* Date:2008-8-2 16:35:25
* A test for traversing the tree
*/
public class Client {
public static String IndentChar = "\ t"; File-level indent character
public static void Main (String args]) {
new Client (). Test ();
}
/**
* Client Test method
*/
public void Test () {
//root files and folders
folder root = new folder ("root");
Folder B1_1 = new folder ("1_ branch 1");
Folder B1_2 = new folder ("1_ branch 2");
Folder B1_3 = new folder ("1_ Branch 3");
file L1_1 = new file ("1_ leaf 1");
file L1_2 = new file ("1_ leaf 2");
file L1_3 = new file ("1_ leaf 3");
//b1_2 files and folders
Folder B2_1 = new folder ("2_ branch 1");
Folder B2_2 = new Folder ("2_ 2");
file L2_1 = new file ("2_ leaf 1");
//Tree-building hierarchy (simple test, no repetitive controls)
Root.add (b1_1);
Root.add (b1_2);
Root.add (l1_1);
Root.add (l1_2);
B1_2.add (b2_1);
B1_2.add (b2_2);
B1_2.add (l2_1);
Root.add (l1_3);
Root.add (b1_3);
//console print tree hierarchy
Outtree (root);
}
public void Outtree (folder folder) {
System.out.println (Folder.getname ());
Iteratetree (folder);
}
/**
* Traverse folder, input file tree
*
* @param folder
*/
public void Iteratetree (folder folder) {
vector<ifile> clist = Folder.getallcomponent ();
//todo: CList can be sorted before traversal, which is not a priority
for (iterator<ifile> it = Clist.iterator (); It.hasnext ();) {
ifile em = It.next ();
if (em instanceof Folder) {
Folder cm = (folder) em;
System.out.println (Getindents (Em.getdeep ()) cm.getname ());
iteratetree (cm);
} else {
System.out.println (Getindents (Em.getdeep ()) ((File) EM). GetName ());
}
}
}
/**
* File-level indent string
*
* @param x indent number of characters
* @return Indent string
*/
public static String getindents (int x) {
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < x; i) {
sb.append (IndentChar);
}
return sb.tostring ();
}
} Three, run test console output as follows:
It is obvious that the tree logical relationship has been successfully demonstrated. Iv. Summary 1, the composite model used above is a safe synthesis model, and the so-called security refers to the different methods in file and folder. Folder has management of the clustered object, file not. 2, the synthesis pattern in the program design has the widespread application, for instance dom4j, the resource manager, the Java GUI container hierarchy diagram and so on are the synthesis pattern application model. 3, a lot of synthetic mode is needed to analyze and think to identify, such as to do a complex mathematical expression calculator, there are four kinds of operational symbols. The analysis found that there are two kinds of computation, one is the number, the other is a number of expressions, but the expression is also composed of numbers, so that the number and expression can be abstracted as an operand. The expression to be calculated is then expressed. Problem solved.