Design pattern ~ Composite ~

1.First of all

We will summarize the ** Composite pattern ** in the GoF design pattern.

2. What is the Composite 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 **.

3. Sample class diagram

Composite.PNG

4. Sample program

A program that displays a list of directories and files.

4-1. Entry class

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("");
	}
}

4-2. File class

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);
	}
}

4-3. Directory class

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);
		}
	}
}

4-4. Main class

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();
	}
}

4-5. Execution result

/workspace
/workspace/composite
/workspace/composite/Directory.java
/workspace/composite/Entity.java
/workspace/composite/file.java
/workspace/composite/main.java
/workspace/test1
/workspace/test2

5. Benefits

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.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Composite ~
Composite pattern
Composite Pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Introduction to Design Patterns (Composite)
[Design pattern] Java core library
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Mediator pattern
Observer Pattern
Builder Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -