23 design patterns (10) - combined patterns

introduce

The combination pattern is also called the part whole pattern. It makes the concept of simple element and complex element fuzzy in our tree structure problem. The client program can deal with complex element just as it deals with simple element, so that the internal structure of client program and complex element can be decoupled

Combinatorial pattern can optimize recursive or hierarchical data structure. There are many examples about hierarchical data structure, which makes combinatorial pattern very useful

Class diagram

 

component:
Component: declare a public interface for the objects participating in composition, whether it is a combination or a leaf node
Leaf: represents the leaf node object in the combination. The leaf node has no child nodes
Composite: refers to the object with sub objects participating in the combination, and gives the behavior of branch purchase

 

Example

FolderComponent

public abstract class FolderComponent {
 private String name;

 public String getName() {
   return name;
 }

 public void setName(final String name) {
   this.name = name;
 }

 public FolderComponent() {
 }

 public FolderComponent(final String name) {
   this.name = name;
 }

 public abstract void add(FolderComponent component);

 public abstract void remove(FolderComponent component);

 public abstract void display();
}

 

FileLeaf

public class FileLeaf extends FolderComponent {
 public FileLeaf(final String name) {
   super(name);
 }

 @Override
 public void add(final FolderComponent component) {
   // ...
 }

 @Override
 public void remove(final FolderComponent component) {
   // ...
 }

 @Override
 public void display() {
   System.out.println("FileLeaf:" + this.getName());
 }
}

 

FolderComposite

public class FolderComposite extends FolderComponent {
 private final List<FolderComponent> components;

 public FolderComposite(final String name) {
   super(name);
   this.components = new ArrayList<FolderComponent>();
 }

 public FolderComposite() {
   this.components = new ArrayList<FolderComponent>();
 }

 @Override
 public void add(final FolderComponent component) {
   this.components.add(component);
 }

 @Override
 public void remove(final FolderComponent component) {
   this.components.remove(component);
 }

 @Override
 public void display() {
   System.out.println("FolderComposite---name:" + this.getName());
   for (final FolderComponent component : components) {
     System.out.println("FolderComposite---component-name:" + component.getName());
   }
 }
}

 

Client

public class Client
{
   public static void main(final String[] args)
   {
       final FolderComponent leaf = new FileLeaf("runnable file");
       leaf.display();

       final FolderComponent folder = new FolderComposite("new folder");
       folder.add(new FileLeaf("content1 in new folder"));
       folder.add(new FileLeaf("content2 in new folder"));
       folder.display();
   }
}

 

Output results:

FileLeaf:runnable file
FolderComposite---name:new folder
FolderComposite---component-name:content1 in new folder
FolderComposite---component-name:content2 in new folder

 

Usage scenarios

Composite mode applies when:

1. You want to represent the part whole hierarchy of the object

2. You want the user to ignore the difference between composite objects and single objects, and the user will use all objects in the composite structure uniformly.

 

summary

The composition pattern decouples the internal structure of the client and the complex elements, so that the client can deal with the complex elements as well as the simple elements.

If you want to create a hierarchy in which you can treat all elements in the same way, a composite pattern is the best choice. A document is used in this chapter

An example of the system illustrates the use of composite patterns. In this case, both files and directories perform the same interface, which is the key to the composite pattern. By implementing the same interface, you can treat files and directories in the same way, thus saving files or directories as child elements of directories.

37 original articles published, praised 1, 970 visitors
Private letter follow

Posted on Mon, 09 Mar 2020 06:35:17 -0400 by aditya2071990