define
By combining objects into a tree structure to represent a whole-part hierarchy, the composite pattern enables consistency in the use of individual objects and composite objects.
nature
Unify leaf objects and composite objects
His role
-
Leaf
A role that represents Content. No other role can be placed in this role.
-
Composite
Represents the role of the container in which you can put the Leaf role and Composite role.
-
Component
The role that makes the Leaf and Composite roles feel consistent is the parent of the Leaf and Composite roles.
-
Client
Roles that use composite mode.
The sample code
Public abstract class Dir {// Declare a List element to store all elements under the folder protected List<Dir> dirs = new ArrayList<>(); private String name; Public Dir(String name) {this.name = name; } public abstract void addDir(Dir Dir); Public abstract void removeDir(Dir Dir); Public abstract void clear(); Public abstract void print(); Public abstract List<Dir> getFiles(); Public String getName() {return name; Public class File extends Dir{public File(String name) {super(name); } @ Override public void addDir Dir (Dir) {throw new UnsupportedOperationException (" the file object does not support this operation "); } @ Override public void removeDir Dir (Dir) {throw new UnsupportedOperationException (" the file object does not support this operation "); {} @ Override public void the clear () throw new UnsupportedOperationException (" the file object does not support this operation "); } @Override public void print() { System.out.print(getName()); } @ Override public List < Dir > getFiles () {throw new UnsupportedOperationException (" the file object does not support this operation "); }} /** * public Class Folder extends Dir{public Folder(String name) {super(name); } @Override public void addDir(Dir dir) { dirs.add(dir); } @Override public void removeDir(Dir dir) { dirs.remove(dir); } @Override public void clear() { dirs.clear(); } @Override public void print() { System.out.print(getName() + "("); Iterator<Dir> iterator = dirs.iterator(); while (iterator.hasNext()){ Dir dir = iterator.next(); dir.print(); if(iterator.hasNext()){ System.out.print(","); } } System.out.print(")"); } @Override public List<Dir> getFiles() { return dirs; }} /** * test, Public class Client {public static void main(String[] args){// construct a directory object to represent the root directory of diskC Dir diskC = new The Folder (" C "); Diskc.adddir (new File("hello.txt")); Dir Windows = new Folder("Windows"); diskC.addDir(windows); Dir logs = new Folder("Logs"); diskC.addDir(logs); Dir users = new Folder("users"); diskC.addDir(users); Windows.adddir (new File("windows.txt")); // add File logs.addDir(new File("logs.txt")); // Add File users.adddir (new File("users.txt")); diskC.print(); }}Copy the code
The results
Disk C (hello. TXT, Windows (Windows. TXT), Logs (Logs. TXT), the users (users. TXT))Copy the code
function
Allows users to call in a unified way, rather than distinguishing leaf objects from composite objects.
advantages
- Defines a class hierarchy that contains both base and composite objects
- Unified composite objects and leaf objects
- Simplifies consumer calls
- Easier to extend, newly defined leaf objects and composite objects can be easily compatible with existing structures without requiring users to change in order to add new objects.
disadvantages
It is difficult to restrict component types within components.
When to use
- Represents an object-whole hierarchy of objects.
- A scenario in which some modules or functions can be separated from the whole.