title: Writing an elegant Click Handler
body: <p>We usually write a click handler on a button (in GWT or Swing or Android) in the following way (<strong>way 1</strong>).</p>
  
  <pre><code>class A {
  
      public void myMethod() {
  
          int count = 0 ;
  
          Button myButton = new Button("x");
          myButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                count = 1 ;
            }
  
          });
  
      }
  }
  </code></pre>
  
  <p>However, we can also write a different class for the click handler and implement it like the following (<strong>way 2</strong>) :</p>
  
  <pre><code>class A implements ButtonClickInterface {
  
      int count ;
      public void myMethod() {
  
          count = 0 ;
  
          Button myButton = new Button("x");
          myButton.addClickHandler(new MyClickHander(this));
  
      }
  
      @Override
      public void buttonClicked() {
          count = 1 ;        
      }
  }
  
  interface ButtonClickInterface {
      public void buttonClicked() ;
  }
  
  class MyClickHandler implements ClickHandler {
  
  
      ButtonClickInterface buttonClickInterface ;
  
      public MyClickHandler(ButtonClickInterface buttonClickInterface) {
          this.buttonClickInterface = buttonClickInterface ;
      }
  
      @Override
      public void onClick(ClickEvent event) {
          buttonClickInterface.buttonClicked() ;         
      }
  
  }
  </code></pre>
  
  <p>Here I have to make <code>count</code> as a class variable. Can this have any drawbacks? Also can the above way of implementation have any other drawbacks? How would the two ways in which I have implemented the ClickHandler face off against each other in terms of complexity, coupling, number of objects created and code readability ?</p>