Created
January 12, 2022 17:27
-
-
Save walteralleyz/769b397bde3b45f6c42463d63c5afe88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RevistaInformatica extends Observable { | |
private int edicao; | |
public void setNovaEdicao(int novaEdicao) { | |
this.edicao = novaEdicao; | |
setChanged(); | |
notifyObservers(); | |
} | |
public int getEdicao() { | |
return this.edicao; | |
} | |
public static void main(String[] args) { | |
//poderia receber a nova edicao atraves de um recurso externo | |
int novaEdicao = 3; | |
RevistaInformatica revistaInformatica = new RevistaInformatica(); | |
Assinante1 assinante1 = new Assinante1(revistaInformatica); | |
revistaInformatica.setNovaEdicao(novaEdicao); | |
} | |
} | |
class Assinante1 implements Observer { | |
Observable revistaInformatica; | |
int edicaoNovaRevista; | |
public Assinante1(Observable revistaInformatica) { | |
this.revistaInformatica = revistaInformatica; | |
revistaInformatica.addObserver(this); | |
} | |
@Override | |
public void update(Observable revistaInfSubject, Object arg1) { | |
if (revistaInfSubject instanceof RevistaInformatica) { | |
RevistaInformatica revistaInformatica = (RevistaInformatica) revistaInfSubject; | |
edicaoNovaRevista = revistaInformatica.getEdicao(); | |
System.out.println("Atenção, já chegou a mais uma edição da Revista Informatica. " + | |
"Esta é a sua edição número: " + edicaoNovaRevista); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment