Skip to main content
added 5 characters in body
Source Link
gogasca
  • 193
  • 9

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

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

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.?

Source Link
gogasca
  • 193
  • 9

Composite and Observer pattern implementation

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 how the advantages.

enter image description here