SHOW:
|
|
- or go back to the newest paste.
1 | public interface ComportamentoBotao { | |
2 | public void executarComportamento(); | |
3 | } | |
4 | ||
5 | public class Button { | |
6 | private ComportamentoBotao comportamento = null; | |
7 | public void setComportamento(ComportamentoBotao c) { | |
8 | comportamento = c; | |
9 | } | |
10 | ||
11 | //Método chamado pela interface gráfica quando o botão for clicado | |
12 | public void dispararEvento() { | |
13 | comportamento.executarComportamento(); | |
14 | } | |
15 | } | |
16 | ||
17 | - | //Na hora de usar você deveria declarar classes para cada um dos botões para depois usá-los |
17 | + | //Na hora de usar você precisa apenas declarar classes internas anonimas |
18 | - | public class ComportamentoBotao1 implements ComportamentoBotao { |
18 | + | |
19 | Button b1 = new Button(); | |
20 | b1.setComportamento(new ComportamentoBotao1(){ | |
21 | public void executarComportamento() { | |
22 | //comportamento do botão | |
23 | } | |
24 | - | public class ComportamentoBotao2 implements ComportamentoBotao { |
24 | + | }); |
25 | ||
26 | Button b2 = new Button(); | |
27 | b2.setComportamento(new ComportamentoBotao2() { | |
28 | public void executarComportamento() { | |
29 | //comportamento do botão 2 | |
30 | - | public class ComportamentoBotao3 implements ComportamentoBotao { |
30 | + | |
31 | }); | |
32 | ||
33 | Button b3 = new Button(); | |
34 | b3.setComportamento(new ComportamentoBotao3() { | |
35 | public void executarComportamento() { | |
36 | - | public class ComportamentoBotao4 implements ComportamentoBotao { |
36 | + | |
37 | } | |
38 | }); | |
39 | ||
40 | Button b4 = new Button(); | |
41 | b4.setComportamento(new ComportamentoBotao4(){ | |
42 | public void executarComportamento() { | |
43 | - | b1.setComportamento(new ComportamentoBotao1()); |
43 | + | |
44 | } | |
45 | }); |