Composite Pattern
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
Composite became a pretty popular solution for the most problems that require building a tree structure. Composite’s great feature is the ability to run methods recursively over the whole tree structure and sum up the results.
Golang Implementation
Example: Folders and Files
Let’s try to understand the Composite pattern with an example of an operating system’s file system. In the file system, there are two types of objects: files and folders. There are cases when files and folders should be treated to be the same way. This is where the Composite pattern comes in handy.
Imagine that you need to run a search for a particular keyword in your file system. This search operation applies to both files and folders. For a file, it will just look into the contents of the file; for a folder, it will go through all files of that folder to find that keyword.
component.go
package main
type Component interface {
search(string)
}
folder.go
package main
import "fmt"
type Folder struct {
components []Component
name string
}
func (f *Folder) search(keyword string) {
fmt.Printf("Serching recursively for keyword %s in folder %s\n", keyword, f.name)
for _, composite := range f.components {
composite.search(keyword)
}
}
func (f *Folder) add(c Component) {
f.components = append(f.components, c)
}
file.go
package main
import "fmt"
type File struct {
name string
}
func (f *File) search(keyword string) {
fmt.Printf("Searching for keyword %s in file %s\n", keyword, f.name)
}
func (f *File) getName() string {
return f.name
}
main.go
package main
func main() {
file1 := &File{name: "File1"}
file2 := &File{name: "File2"}
file3 := &File{name: "File3"}
folder1 := &Folder{
name: "Folder1",
}
folder1.add(file1)
folder2 := &Folder{
name: "Folder2",
}
folder2.add(file2)
folder2.add(file3)
folder2.add(folder1)
folder2.search("rose")
}
// Serching recursively for keyword rose in folder Folder2
// Searching for keyword rose in file File2
// Searching for keyword rose in file File3
// Serching recursively for keyword rose in folder Folder1
// Searching for keyword rose in file File1
Rust Implementation
Example: Folder and File
File and Directory are both of the trait Component with a single search method. For a file, it will just look into the contents of the file; for a folder, it will go through all files of that folder to find that keyword.
fs/mod.rs
mod file;
mod folder;
pub use file::File;
pub use folder::Folder;
pub trait Component {
fn search(&self, keyword: &str);
}
fs/file.rs
use super::Component;
pub struct File {
name: &'static str,
}
impl File {
pub fn new(name: &'static str) -> Self {
Self { name }
}
}
impl Component for File {
fn search(&self, keyword: &str) {
println!("Searching for keyword {} in file {}", keyword, self.name);
}
}
fs/folder.rs
use super::Component;
pub struct Folder {
name: &'static str,
components: Vec<Box<dyn Component>>,
}
impl Folder {
pub fn new(name: &'static str) -> Self {
Self {
name,
components: vec![],
}
}
pub fn add(&mut self, component: impl Component + 'static) {
self.components.push(Box::new(component));
}
}
impl Component for Folder {
fn search(&self, keyword: &str) {
println!(
"Searching recursively for keyword {} in folder {}",
keyword, self.name
);
for component in self.components.iter() {
component.search(keyword);
}
}
}
main.rs
mod fs;
use fs::{Component, File, Folder};
fn main() {
let file1 = File::new("File 1");
let file2 = File::new("File 2");
let file3 = File::new("File 3");
let mut folder1 = Folder::new("Folder 1");
folder1.add(file1);
let mut folder2 = Folder::new("Folder 2");
folder2.add(file2);
folder2.add(file3);
folder2.add(folder1);
folder2.search("rose"); // Understand: the search sequence depends on how the structure looks like
}
// Searching recursively for keyword rose in folder Folder 2
// Searching for keyword rose in file File 2
// Searching for keyword rose in file File 3
// Searching recursively for keyword rose in folder Folder 1
// Searching for keyword rose in file File 1