Pattern - Composite
The Compositor pattern is a software design pattern that was first described in the book "Design Patterns: Elements of Reusable Object-Oriented Software" written by Erich Gamma, Richard Helm , Ralph Johnson and John Vlissides, better known as the "Gang of Four" (GoF).
The Composer pattern is a software design pattern that enables the creation of hierarchical object structures and provides an interface for working with those structures uniformly, regardless of the type of object they contain. It lets you create objects that make up other objects, forming a nested structure of objects.
The main idea of the Compositor pattern is to allow objects to be grouped into hierarchical structures, like a tree, where each node is a composite object of other objects. Each object has a common interface, allowing objects to be treated similarly regardless of their position in the hierarchy. This allows building complex structures using simple, easy-to-understand objects, making the code easier to maintain and extend.
Common examples of using the composer pattern include creating documents with text, images, and tables, creating graphical user interfaces with nested panels, and creating complex file structures with folders and files.
The Composer pattern can be used in many situations to solve common design problems, some examples include:
Below is a simple code example using the Composer pattern.
class FileSystemItem {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
getSize() {
return 0;
}
}
class File extends FileSystemItem {
constructor(name, size) {
super(name);
this.size = size;
}
getSize() {
return this.size;
}
}
class Directory extends FileSystemItem {
constructor(name) {
super(name);
this.items = [];
}
addItem(item) {
this.items.push(item);
}
removeItem(item) {
this.items = this.items.filter(i => i !== item);
}
getSize() {
return this.items.reduce((size, item) => size + item.getSize(), 0);
}
}
const home = new Directory('Home');
const documents = new Directory('Documents');
const resume = new File('resume.pdf', 1024);
const budget = new File('budget.xlsx', 2048);
documents.addItem(resume);
documents.addItem(budget);
home.addItem(documents);
console.log(home.getName()); // "Home"
console.log(home.getSize()); // 3072
The FileSystemItem class is the base class with a common interface for all file structure elements. It has a name and a getName() method that returns that name, and a getSize() method that returns 0 (because it's a base class and doesn't know the size of the item).
The File class extends FileSystemItem and has an additional "size" property. It also overrides the getSize() method to return the file size.
Recommended by LinkedIn
The Directory class also extends FileSystemItem and has an "items" property which is an array of file structure elements. It has addItem(item) and removeItem(item) methods for adding and removing elements, respectively. It also has a getSize() method that returns the total size of all its child elements using the reduce method.
Next, we create an instance of the Directory class called home and another one called documents. Then we create File instances called resume and budget. We add these files to the documents folder, and finally we add the documents folder to the home folder.
Finally, we print the name of the home folder and the total size of its elements.
Simple, right?
The Composer pattern is useful when we need to work with composite object structures, where an object can contain other objects and these objects can be both simple and composite. It allows you to work with these structures uniformly, regardless of whether your elements are simple or composite.
Examples of common uses of the Composer pattern include:
Conclusion
The Composer pattern is a design pattern that allows you to work with uniformly composed object structures, regardless of whether their elements are simple or composite. It allows the creation of hierarchical structures, where objects can contain other objects, and these objects can be both simple and composite.
Hope this helps, until next time.