We will summarize the ** Composite pattern ** in the GoF design pattern.
-The English word Composite means ** mixture ** or ** composite **. -The Composite pattern is a method that ** makes the container and contents the same and creates a recursive structure **. --It may be convenient to treat the container and contents as the same type, just as you treat directories and files as directory entries. For example, you can put the contents in the container, or you can put more containers. In this way, you can create a recursive structure. --The GoF design patterns are classified as ** structural design patterns **.
A program that displays a list of directories and files.
The base class for File and Directory.
Entry.java
public abstract class Entry {
public abstract String getName();
protected abstract void printList(String prefix);
public void printList() {
printList("");
}
}
A class that represents a file.
File.java
public class File extends Entry {
private String name;
public File(String name) {
this.name = name;
}
public String getName() {
return name;
}
protected void printList(String prefix) {
System.out.println(prefix + "/" + name);
}
}
A class that represents a directory.
Directory.java
import java.util.ArrayList;
import java.util.Iterator;
public class Directory extends Entry {
private String name;
private ArrayList<Entry> directory = new ArrayList<Entry>();
public Directory(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Entry add(Entry entry) {
directory.add(entry);
return this;
}
protected void printList(String prefix) {
System.out.println(prefix + "/" + name);
Iterator<Entry> it = directory.iterator();
while (it.hasNext()) {
Entry entry = (Entry) it.next();
entry.printList(prefix + "/" + name);
}
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
Directory workspaceDir = new Directory("workspace");
Directory compositeDir = new Directory("composite");
Directory testDir1 = new Directory("test1");
Directory testDir2 = new Directory("test2");
workspaceDir.add(compositeDir);
workspaceDir.add(testDir1);
workspaceDir.add(testDir2);
File directory = new File("Directory.java");
File entity = new File("Entity.java");
File file = new File("file.java");
File main = new File("main.java");
compositeDir.add(directory);
compositeDir.add(entity);
compositeDir.add(file);
compositeDir.add(main);
workspaceDir.printList();
}
}
/workspace
/workspace/composite
/workspace/composite/Directory.java
/workspace/composite/Entity.java
/workspace/composite/file.java
/workspace/composite/main.java
/workspace/test1
/workspace/test2
Since all objects (File, Directory) have a common abstract class, you can handle them uniformly without having to be aware of the contents of which is File or Directory from the client's point of view. .. Also, even if you add a new class (eg SymbolicLink), it will not affect the client processing unless the interface of the base class (Entry) changes.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts