Combination mode, combining objects into a tree structure to represent a "part-total" hierarchy. Combination mode enables users to use individual objects and combined objects consistently. Combination mode allows clients to complete functions that originally required process control statements as simply as modifying configuration files.
Features: For recursive or tree-like hierarchical data structures, they can be processed in the simplest way.
Applications in enterprise-level development and commonly used frameworks: system directory structure and website navigation structure
Here is an example of the directory structure:
Scenario: Suppose we now have a directory, and there are subdirectories and files under the directory. Now we want to view the entire directory and all files and creation time in the directory.
The specific code is as follows:
package com.test.composite;import java.util.ArrayList;import java.util.Date;import java.util.List;public class Demo { public static void main(String[] args) { Date d = new Date(); Dir f1 = new Dir("My Collection", d); d.setYear(2012); Dir f2 = new Dir("Picture", d); Dir f3 = new Dir("Music", d); d.setYear(2013); ActualFile f4 = new ActualFile("Xi Yangyang and Big Big Wolf.avi", d); f1.add(f4); ActualFile f5 = new ActualFile("taiyanghua.jpg", d); ActualFile f6 = new ActualFile("Deformed Stainless Steel.jpg", d); f2.add(f5); f2.add(f6); f1.add(f2); f1.add(f3); f1.showFile(); }}/** * First of all, the directory and the file belong to the file, so we can abstract an abstract file*/interface AbstractFile { /** * Display file method*/ public void showFile();}/** * Real file*/class ActualFile implements AbstractFile { private String name; private Date createDate; public ActualFile(String name, Date createDate) { this.name = name; this.createDate = createDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } /** * Implement the display file method of abstract file class*/ public void showFile() { System.out.println("File name: "+this.name+"--Create time: "+this.createDate.getTime()); }}/** * Directory file*/class Dir implements AbstractFile { private String name; private Date createDate; /** * As a directory file, there will be an additional subfile list*/ private List<AbstractFile> list = new ArrayList<>(); public Dir(String name, Date createDate) { super(); this.name = name; this.createDate = createDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } /** * Add a directory file, add a subfile or subdirectory to a directory */ public void add(AbstractFile f){ this.list.add(f); } /** * Delete operation of directory file, delete subfile or subdirectory */ public void remove(AbstractFile f){ this.list.remove(f); } /** * Get operation of directory file, obtain subfile or subdirectory below the directory */ public AbstractFile getIndex(int index){ return this.list.get(index); } public void showFile() { System.out.println("Directory name: "+this.name+"--Create time: "+this.createDate.getTime()); for(AbstractFile f:list){ f.showFile(); } }}Combination mode is more like a traversal method, but this method also has some limitations, such as only targeting data similar to a tree structure.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.