Observer Design Pattern on Android

Asked

Viewed 746 times

1

Someone would have how to show me an Observer structure within android?

I was trying to build one like the java, but I was unsuccessful.

My test was done as follows:

  • I created a class Banco and a class ClienteObservable;
  • At the bank I have the methods of notification, and adicionarObserver (that I pass the ball to clienteObservable);
  • In the class ClienteObservable I have the method update, who will carry out an action upon being notified.

This is the logic I have about Project Observer Standard, but I don’t know how to structure it in , someone could develop something very basic to show me?

It follows below the Code:

Na Class Banco inserir a descrição da imagem aqui

In Class clienteObservable inserir a descrição da imagem aqui

  • 2

    Could you post the code you implemented in java? Taking advantage you can leave the classes with the default Java with the first letter uppercase.

  • I added the image, and saw that the method notify this without any action with the observer, I figured it might be another strength to use the Observer

  • What error is having on android?

  • No error, just not executed anything, I put for every time the observer receives a notification, it displays a Toast, and it does not run, like and I flirted, my method notify Observeer is not running anything(I think), I will test when I get from the service.

  • 1

    You need to explicitly call notifyObservers every time you change your object. the same way I put in my class in the answer below.

1 answer

1

I believe it’s the same as java.

public class Cliente {
}
public interface ClienteObservable {
    public void update();
}
public class Banco {
    private List<ClienteObservable> observables = new ArrayList<ClienteObservable>();
    public void adicionarObserver(ClienteObservable obs){
       this.observables.add(obs);
    }
}

But there is a conceptual error here. It should actually be like this:

public interface ClienteObserver {
    public void update(Cliente cliente);
}
public class Banco implements ClienteObserver {
    @Override
    public void update(Cliente cliente) {
        //faz algum calculo para o cliente  ..
    }
}
public class Cliente {
    private String nome;
    private List<ClienteObserver> observers = new ArrayList<ClienteObserver>();

    public void adicionarObserver(ClienteObserver obs) {
        this.observers.add(obs);
    }
    public void setNome(String nome) {
        this.nome = nome;
        this.notifyObservers();
    }
    private void notifyObservers() {
        for (ClienteObserver observer : this.observers) {
            observer.update(this);
        }
    }
}

For who you are observing is the Customer object. When it updates its status it should communicate the observers about this modification.

  • I tested what you sent me, really it works in java , but on android no, I still believe that android has to be a different structure.

  • 1

    As this example is isolated it should work just like the desktop. Can give more details of your problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.