Search⌘ K
AI Features

Challenge: Composite Pattern

Explore how to apply the composite design pattern to implement a directory system in JavaScript. This lesson helps you understand the structure of components, leaves, and composites through a hands-on challenge, enabling you to model files and folders with properties such as name, size, and last modification time.

Problem statement

In this challenge, you have to implement a directory system using the composite pattern.

You have been given the following skeleton code:

Node.js
class Directory{
constructor(name,lastModified,size){
this.name = name
this.lastModified = lastModified
this.size = size
}
getLastmodified(){}
getSize(){}
getName(){}
}
class File extends Directory{
}
class Folder extends Directory{
}

It’s up to you to figure out which class will be the component, leaf subclass, and composite subclass.

A Directory has the following properties:

  • name: the name of the file/folder ...