First, Mode analysis
Combines objects into a tree structure to represent a hierarchical structure of "partial whole." The combined mode makes the user consistent with individual objects and usage.
The main points of the combo mode are:
1. The whole object is displayed in a tree-shaped hierarchy structure.
2, the root node of the tree and child nodes can be added to delete child nodes, leaf nodes No child nodes
3, can call through the action of the node and show all his own point of action
4, in order to facilitate the expansion of unlimited nodes, we usually define a common interface, all child nodes and leaf nodes implement this interface
Second, the mode code
1, public interface, for all child nodes and leaf node implementation
PackageComponent.patten; Public Abstract classComponent {protectedString name; PublicComponent (String name) { This. name=name; } Public Abstract voidAdd (Component Component); Public Abstract voidRemove (Component Component); Public Abstract voidDisplayintdeepth); }
2. Leaf node
PackageComponent.patten;Importjava.util.ArrayList;Importjava.util.List; Public classCompositeextendsComponent { PublicComposite (String name) {Super(name); } List<Component> list=NewArraylist<component>(); @Override Public voidAdd (Component Component) {list.add (Component); } @Override Public voidRemove (Component Component) {list.remove (Component); } @Override Public voidDisplayintdeepth) {System.out.println ( This. Name); //the method of the leaf node can invoke methods that show all of his child nodes for(Component component:list) {StringBuffer buffer=NewStringBuffer (); for(inti = 0; i < deepth; i++) {buffer.append ("--"); } System.out.print (buffer); Component.display (deepth+1); } }}
3, leaf node, leaf node does not implement add and delete methods, but because the integration of the interface, so there must be
PackageComponent.patten; Public classLeafextendsComponent { PublicLeaf (String name) {Super(name); } @Override Public voidAdd (Component Component) {//TODO auto-generated Method Stub} @Override Public voidRemove (Component Component) {//TODO auto-generated Method Stub} @Override Public voidDisplayintdeepth) {System.out.println ("--"+ This. Name); }}
4. Client code
PackageComponent.patten; Public classClient { Public Static voidMain (string[] args) {Component root=NewComposite ("root"); Component COM1=NewComposite ("COM1"); Root.add (COM1); Component LEAF1=NewLeaf ("LEAF1"); Component leaf2=NewLeaf ("Leaf2"); Component leaf3=NewLeaf ("Leaf3"); Com1.add (LEAF1); Com1.add (LEAF2); Com1.add (LEAF3); Component COM2=NewComposite ("COM2"); Root.add (COM2); Component LEAF4=NewLeaf ("Leaf2"); Com2.add (LEAF4); Root.display (1); }}
5. Implementation results
Root--com1------LEAF1------leaf2------leaf3--com2------ Leaf2
Third, the application scenario
The combination mode mainly solves the problem of tree structure, such as group and Molecule Company, folder and file system of computer system, multi-level menu in System, list in Java collection frame, and so on, the main point is to add the hierarchical relation and display the subset information, and use the folder system for example.
Four, Scene code
1. Define the node of the file
Packagecomponent.example;Importjava.util.ArrayList;Importjava.util.List; Public Abstract classNode {protectedString name; protectedList<node> list=NewArraylist<node>(); PublicNode (String name) { This. name=name; } Public Abstract voidAdd (Node node)throwsException; Public Abstract voidRemove (node node)throwsException; Public Abstract voiddisplay (String path);
2. Define Folders
Packagecomponent.example;/*** Folder *@authorZJL * @time 2016-2-2 **/ Public classFolderextendsNode { PublicFolder (String name) {Super(name); } @Override Public voidAdd (Node node) {List.add (node); } @Override Public voidRemove (node node) {List.remove (node); } @Override Public voiddisplay (String path) {path+="/"+ This. Name; for(Node node:list) {node.display (path); } }}
3. definition file
Packagecomponent.example; Public classFileextendsNode { PublicFile (String name) {Super(name); } @Override Public voidAdd (Node node)throwsException {Throw NewException ("This method is not supported"); } @Override Public voidRemove (node node)throwsException {Throw NewException ("This method is not supported"); } @Override Public voiddisplay (String path) {System.out.println (path+"/"+ This. Name); }}
4, the Client
Packagecomponent.example; Public classClient { Public Static voidMain (string[] args) {Folder document=NewFolder ("My Profile");//My Profile folderFile book =NewFile ("Java Programming ideas. pdf");//Document FilesFolder music =NewFolder ("My Music");//My Music folderFile Music1 =NewFile ("You Are My eyes. mp3");//music files 1File Music2 =NewFile ("Without You.mp3");//Music files 2//Determine tree structure relationshipsdocument.add (book); Document.add (music); Music.add (MUSIC1); Music.add (MUSIC2); Document.display (""); }}
5. Operation Result
/My Profile/Java programming ideas. PDF/My profile/My music/ you Are my eyes. mp3/My profile/My music/without You.mp3
[Working design pattern] combination mode compnent