Composite mode

Combination mode

The relationship between the part and the whole is represented by a tree structure, so that the client can handle the part object and the whole object in a unified way.

role

Abstract Component: define common points of leaf and container components
Leaf: no child nodes
Container component: it has container features and can contain child nodes

Specifically, the characteristics of container components are to add and remove child nodes, as well as index functions.

public interface Component {
	void operation();
}

interface Leaf extends Component {}

interface Composite extends Component {
	void add(Component c);
	void remove(Component c);
	Component getChild(int index);
}

for instance:
A file anti-virus system, for each different type of single file, belongs to the leaf component, because it is impossible to have more detailed segmentation A container component is a folder. A container can contain a container or a leaf.
Realization:

// Abstract component, with only one antivirus function
public interface AbstractFile {
	void killVirus(); //Anti-Virus
}

// Define three types of leaf nodes, representing image, text and video files respectively
class ImageFile implements AbstractFile {

	private String name;
	
	public ImageFile(String name) {
		super();
		this.name = name;
	}
	
	@Override
	public void killVirus() {
		System.out.println("---Image file:" + name + ",Carry out the investigation!");
	}
}

class TextFile implements AbstractFile {

	private String name;
	
	public TextFile(String name) {
		super();
		this.name = name;
	}

	@Override
	public void killVirus() {
		System.out.println("---Text file:" + name + ",Carry out the investigation!");	
	}
	
}

class VideoFile implements AbstractFile {

	private String name;
	
	public VideoFile(String name) {
		super();
		this.name = name;
	}

	@Override
	public void killVirus() {
		System.out.println("---Video file:" + name + ",Carry out the investigation!");	
	}

}

//Define container components, folders,
//Contains a List of other nodes (folders, or individual files) that implement the AbstractFile type

class Folder implements AbstractFile {
	private String name;
	// Define the container to store the child nodes under the container component
	private List<AbstractFile> list = new ArrayList<AbstractFile>();

	public Folder(String name) {
		super();
		this.name = name;
	}

	@Override
	public void killVirus() {
		System.out.println("---Folder:" + name + ",Carry out the investigation!");
		
		// If there are subfolders, recursively call itself
		for(AbstractFile file : list) {
			file.killVirus();
		}
		
	}
	
	public void add(AbstractFile file) {
		list.add(file);
	}
	
	public void remove(AbstractFile file) {
		list.remove(file);
	}
	
	public AbstractFile getChild(int index) {
		return list.get(index);
	

Client: killVirus method can be called directly whether it is to kill files or folders.

public class Client {

	public static void main(String[] args) {
		AbstractFile f2,f3,f4,f5;
		
		Folder f1 = new Folder("My collection");
		f2 = new ImageFile("pic.jpg");
		f3 = new TextFile("Hello.txt");
		f1.add(f2);
		f1.add(f3);
		
		Folder f11 = new Folder("Film");
		f4 = new VideoFile("Laughing and fighting.avi");
		f5 = new VideoFile("Divine eagle.avi");
		f11.add(f4);
		f11.add(f5);
		
		f1.add(f11);
		
		f1.killVirus();
		
	}
}

Operation result:

Application scenarios in development

Operating System Explorer
Container hierarchy in GUI
XML file parsing
Junit unit test framework

Tags: xml Junit

Posted on Sat, 09 Nov 2019 09:38:29 -0800 by bruceleejr