I'm learning Composite and Observer design patterns and I have created a FileSystem class where I define Node, Folder and File as a composite relationship. Now I want to implement Observer pattern so when there is a FileBrowser/Finder/File Explorer Observers they can get updated when I add a File to a Folder. This is my relationship and code:

enter image description here

Im implementing the Subject Interface for Node (Component) and in Directory itself I implemented the Subject methods: attach, dettach and notifyObservers. Is this a good design? What If I want to get notifications for folder and subfolders? do I need to pass each Directory to my FileBrowser observer? If a File change we may need to duplicate the code meaning add attach, dettach and notifyObservers to my File class?.

public class SolutionMain {

    public static StringBuffer g_indent = new StringBuffer();

    public static void main(String[] args) {

        Node root = FileSystem.getFileSystem();
        Node one = new Directory("dir1");
        Node two = new Directory("dir2");
        Node thr = new Directory("dir3");
        Node a = new File("a", 100);
        Node b = new File("b", 200); 
        Node c = new File("c", 200); 
        Node d = new File("d", 400);
        Node e = new File("e", 10);

        new FileBrowser(root);



public class FileBrowser extends Observer {

    private static int observerIDTracker = 0;
    private int observerID;
    private Subject subject;

    public FileBrowser (Subject subject) {
        this.subject = subject;
        this.observerID = ++observerIDTracker;
        System.out.println("New observer "  + this.observerID);
        // Attach observer in this case FileBrowser

    public void update() {
        Node d = (Directory) subject;



public class Directory extends Node{

    private String _name;
    private ArrayList<Node> _children = new ArrayList<Node>();
    private ArrayList<Observer> _observers = new ArrayList<Observer>();

    public Directory(String name) { _name = name; }

    public void name(String name) { _name = name; }

    public String name() { return _name; }

    public void add(Node obj) { 

    public void display() { System.out.println("Directory: " + _name + " changed"); }

    public void attach(Observer observer){

    public void detattach(Observer observer){

    public void notifyAllObservers(){
          for (Observer observer : _observers) {

The notification works, but wondering if this is a good implementation of observer.

I found an example online where there is a use of Mediator pattern and there is an implementation of this class, but not sure what are the advantages.?

enter image description here

1 Answer 1


Was looking on google for some UML class diagrams for composite design pattern to use it in a paper and saw this thread of you. Unfortunately there is a mistake in your uml class diagram. Between 'Directory' and 'Node' you've an aggregation relationship, that's not correct though. It should be a composite relationship instead.

In an aggregation (what you've used between directory and node) relationship you have two objects without an existence dependency. Means that class A can last, despite class b being deleted.

On the contrary, the composite relationship is used for two objects having an existence dependency. So if using a composite between class A and class B with the composite symbol at class B, class A will get destroyed once destroying class B.

This means for your diagram, that a Node i.e. Directory or File will last even deleting the parent Directory. This makes no sense right? :D

  • It would depend on what a person is trying to achieve. Let's say we want something like POSIX implementation, where you can open a file, then delete it, but its node is still accessible until that particular process releases it. But maybe i'm overthinking it for this case.
    – v010dya
    Commented Feb 27, 2020 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.