Composite mode: Combines multiple objects into a tree structure to represent a whole-part relationship hierarchy. Composite mode allows clients to treat single objects and composite objects uniformly

Component (Abstract: container) : It can be an interface or an abstract class that declares interfaces for leaf build objects and child container build objects. In this role, it can contain implementations and declarations of behavior common to all subclasses. In the abstract construction, the methods of accessing and managing its children are defined, such as adding children, deleting children, obtaining children, and so on. Leaf builds: Leaf builds are all kinds of files! Leaf builds have no child artifacts. It implements the behavior defined in the abstract build. For those accessing, deleting, or adding child containers, an error is reported. Compsite (Child container Build) : This represents container node objects in composite mode. Container nodes are child nodes, which can be child containers or leaf builds, and provide a collection to store child nodes.

Simple implementation of composite patterns

component

Public Abstract Class Component{// This is the abstract class of the container class, defines the behavior, defines the method that creates and removes the child container abstract. public abstract void addComposite(Component c); Public abstract void removeComposite(Component c); Public abstract Component getComposite(int I); Public abstract void operation(); // business method}Copy the code

leaf

Public class Leaf extends Component{public void Operation(){system.out.print (" business method ");  } public void addComponent(Component c){ System.out.print(" Not a child container "); } public void removeComponent(Component c){// An error message is displayed. System.out.print(" Not a child container "); } public Component addComponent(int c){ System.out.print(" Not a child container "); return null; }}Copy the code

composite

Public class Composite extends Component{private ArrayList<Component> list = new ArrayList<Component>; public void addComponent(Component c){ list.add(c); } public void removeComponent(Component c){ list.remove(c); } public Component getComponent(int c){ Component c1 =list.get(c); return c1; } public void operation(){ for(Object obj:list){ ((Component)obj).operation(); }}}Copy the code

A formal example of a composite pattern

Antivirus software, the software can be a folder antivirus, can also be specified for certain files antivirus.

AbstractFile: AbstractFile class that acts as an abstract build

public abstract class AbstractFiles {
  public abstract void add(AbstractFiles af);
  public abstract void remove(AbstractFiles af);
  public abstract AbstractFiles get(int  i);
  public abstract void killVirus();
}
Copy the code

Leaf node: file type, one of which is written

public class ImageFile extends AbstractFiles { private String name; public ImageFile(String name) { this.name=name; } @override public void add(AbstractFiles af) {// TODO auto-generated method stub system.out.println (" this method is not supported "); } @override public void remove(AbstractFiles af) {// TODO auto-generated method stub system.out.println (" this method is not supported "); } @override public AbstractFiles get(int I) {// TODO auto-generated method stub system.out.println (" this method is not supported "); return null; } @override public void killVirus() {// TODO auto-generated method stub system.out.println (" --"+name+"-- "-- "); }}Copy the code

File type:

Public Class Folder extends AbstractFiles {public Class Folder extends AbstractFiles { Private ArrayList<AbstractFiles> list = new ArrayList<AbstractFiles>(); private String name; public Folder(String name) { this.name=name; } @Override public void add(AbstractFiles af) { list.add(af); System.out.println(" add to dog "); } @Override public void remove(AbstractFiles af) { // TODO Auto-generated method stub if(list.remove(af)) { System.out.println(" delete succeeded "); } else {system.out.println (" delete failed "); } } @Override public AbstractFiles get(int i) { // TODO Auto-generated method stub return list.get(i); } @override public void killVirus() {// TODO auto-generated method stub system.out.println (" +name+"); For (Object o:list) {((AbstractFiles)o).killvirus (); }}}Copy the code

Testing:

Public class Client {public static void main(String[] args) {// Create a file type AbstractFiles f1 = new Folder(" main Folder "); AbstractFiles file1= new ImageFile(" sun Goks.png "); AbstractFiles file2= new ImageFile(".jpg"); AbstractFiles file3= new ImageFile(" ImageFile "); f1.add(file1); f1.add(file2); f1.add(file3); f1.killVirus(); file1.killVirus(); }}Copy the code

reference

Blog.csdn.net/qq_40709468…