lunes, 30 de abril de 2018

Observer pattern Java

Observer Pattern en Java


Subject.java class
 
package ObserverPatt2;

import java.util.Collection;

public abstract class Subject {
    Collection observers;
    abstract void registerObserver(Observer obs);
    abstract void unregisterObserver(Observer obs);
    abstract void notifyObservers();
}

Observer.java class

package ObserverPatt2;

public abstract class Observer {
    public abstract void update();
}

PostOffice.java class

public class PostOffice extends Subject {
    private String address;

    public PostOffice(String address) {
        observers = new ArrayList();
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override    void registerObserver(Observer obs) {
        observers.add(obs);
    }

    @Override    void unregisterObserver(Observer obs) {
        observers.remove(obs);
    }

    @Override    void notifyObservers() {
        Iterator iterator = observers.iterator();
        while(iterator.hasNext()){
            ((ObserverPatt2.MailBox)iterator.next()).update();
        }
    }
}

MailBox.java class

package ObserverPatt2;

public class MailBox extends Observer {
    String Id;

    public MailBox(String id) {
        Id = id;
    }

    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    @Override    public void update() {
        System.out.println(Id +  "- Tiene Nuevo email");
    }
}

MainClass.java

package ObserverPatt2;

public class MainClass {
    public static void main(String... args){
        MailBox mailBox1 = new MailBox("Paseo de la Reforma 123");
        MailBox mailBox2 = new MailBox("Insurgentes Sur 123");
        PostOffice postOffice = new PostOffice("Central de Correos");

        System.out.println("registrando observador " + mailBox1.getId() + " en " + postOffice.getAddress() );
        postOffice.registerObserver( mailBox1);
        System.out.println( postOffice.getAddress() +  ": notificando a observadores...");
        postOffice.notifyObservers();
        System.out.println("registrando observador " + mailBox2.getId() + " en " + postOffice.getAddress() );
        postOffice.registerObserver( mailBox2);
        System.out.println( postOffice.getAddress() +  ": notificando a observadores...");
        postOffice.notifyObservers();
        System.out.println("Desregistrando observador " + mailBox1.getId() + " de " + postOffice.getAddress() );
        postOffice.unregisterObserver( mailBox1);
        System.out.println( postOffice.getAddress() +  ": notificando a observadores...");
        postOffice.notifyObservers();
    }
}

La corrida quedaría de la siguiente manera:

registrando observador Paseo de la Reforma 123 en Central de Correos
Central de Correos: notificando a observadores...
Paseo de la Reforma 123- Tiene Nuevo email
registrando observador Insurgentes Sur 123 en Central de Correos
Central de Correos: notificando a observadores...
Paseo de la Reforma 123- Tiene Nuevo email
Insurgentes Sur 123- Tiene Nuevo email
Desregistrando observador Paseo de la Reforma 123 de Central de Correos
Central de Correos: notificando a observadores...
Insurgentes Sur 123- Tiene Nuevo email

fin texto.

No hay comentarios:

Publicar un comentario