Java Design Patterns – Composite and Observer Pattern Implementation

design-patternsjava

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);
        root.add(one);
        root.add(two);
        one.add(a);
        one.add(two);
        one.add(b);
        two.add(c);
        two.add(d);
        two.add(thr);
        thr.add(e);
    }

}

FileBrowser

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
        this.subject.attach(this);
    }

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


}

Directory

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) { 
        _children.add(obj); 
        notifyAllObservers();
    }


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

    public void attach(Observer observer){
          _observers.add(observer);     
    }

    public void detattach(Observer observer){
          _observers.remove(observer);      
    }

    public void notifyAllObservers(){
          for (Observer observer : _observers) {
             observer.update();
          }
    } 
}

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

Best Answer

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

Related Topic