
This blog post explores the Composite Design Pattern, illustrating its application through two examples: designing a file system and creating a calculator. The pattern allows for the creation of tree-like structures where objects can contain other objects, simplifying the management of complex hierarchies.
In this blog post, we will delve into the Composite Design Pattern, a structural design pattern that allows you to compose objects into tree-like structures. This pattern is particularly useful when dealing with hierarchies of objects, where individual objects and compositions of objects can be treated uniformly. We will explore this concept through two practical examples: designing a file system and creating a calculator.
The Composite Design Pattern is centered around the idea of objects containing other objects. This means that you can create complex structures where a single object can be composed of multiple objects, forming a tree-like hierarchy. For instance, consider a tree structure where each node can be a leaf (an end object) or a composite (an object that contains other objects).
Let’s consider the problem of designing a file system. In a file system, you can have files and directories. A directory can contain files or other directories, creating a hierarchical structure.
To design a file system, we need to represent:
File Class:
class File {
private String name;
public File(String name) {
this.name = name;
}
public void ls() {
System.out.println(name);
}
}
Directory Class:
class Directory {
private String name;
private List<FileSystem> fileSystemList;
public Directory(String name) {
this.name = name;
this.fileSystemList = new ArrayList<>();
}
public void ls() {
System.out.println(name);
for (FileSystem fs : fileSystemList) {
fs.ls();
}
}
}
When calling the ls method on a directory, it iterates through its list of file system objects and calls their respective ls methods. This eliminates the need for type-checking (using instanceof) since both files and directories implement the same interface.
Directory movie = new Directory("Movies");
File border = new File("Border Movie");
Directory comedyMovies = new Directory("Comedy Movies");
comedyMovies.add(new File("Hulchul"));
movie.add(border);
movie.add(comedyMovies);
movie.ls();
The second example involves creating a calculator that can evaluate arithmetic expressions. This can also be represented as a tree structure where each node is either a number or an operation.
To build a calculator, we need to represent:
evaluate method.evaluate method.Number Class:
class Number implements ArithmeticExpression {
private int value;
public Number(int value) {
this.value = value;
}
public int evaluate() {
return value;
}
}
Expression Class:
class Expression implements ArithmeticExpression {
private ArithmeticExpression left;
private ArithmeticExpression right;
private Operation operation;
public Expression(ArithmeticExpression left, ArithmeticExpression right, Operation operation) {
this.left = left;
this.right = right;
this.operation = operation;
}
public int evaluate() {
switch (operation) {
case ADD:
return left.evaluate() + right.evaluate();
case SUBTRACT:
return left.evaluate() - right.evaluate();
// Other operations...
}
}
}
When evaluating an expression, the evaluate method checks the operation and recursively evaluates the left and right expressions. This allows for complex expressions to be evaluated seamlessly.
ArithmeticExpression expression = new Expression(
new Number(2),
new Expression(new Number(1), new Number(7), Operation.ADD),
Operation.MULTIPLY
);
int result = expression.evaluate(); // Evaluates to 16
The Composite Design Pattern is a powerful tool for managing tree-like structures in programming. By using this pattern, we can simplify the design of complex systems such as file systems and calculators. Understanding when and how to apply this pattern can significantly enhance your ability to solve design problems effectively.
For further exploration, consider implementing these examples in your preferred programming language and experimenting with different structures and operations. Happy coding!
Paste a YouTube link and let Magica create the key takeaways.
Summarize another video