3

In a comment reply to one of my answers, Hovercraft said

No, your suggestion will make things worse really, since by using this as the ActionListener, you're suggesting that he make his View also a Control, something that shouldn't be done in anything other than "toy" programs.

As a newbie, I have always been using 'this' and I am unclear as to why this is so discouraged. Could someone could explain/elaborate Hovercraft's answer or give another?

Comment-- https://stackoverflow.com/questions/18021509/how-can-i-call-method-with-a-string/18021674#18021674

Community
  • 1
  • 1
discipliuned
  • 916
  • 7
  • 13
  • 8
    Instead of linking to the user can you link to the post or comment in question? Also perhaps you should try commenting to him first. – nanofarad Aug 09 '13 at 15:52
  • 2
    It sounds like he is advising seperation of concerns, and not make your class deal with more than it has to – Karthik T Aug 09 '13 at 15:54
  • 1
    Ah sorry, I thought more people would want to know too, that's why I asked – discipliuned Aug 09 '13 at 15:56
  • `this` in constructors: http://nat.truemesh.com/archives/000222.html can be bad. – zapl Aug 09 '13 at 15:59
  • This is in reference to the MVC design philosophy. You can read more about it here: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – Martin Wickham Aug 09 '13 at 16:11
  • If your application requires that you add action listeners to several of your components, then you also end up with a big chain of if statements trying to determine which component was the source of the event. An alternative is to use an anonymous inner class, and invoke another, more specific method (like what was probably done in the OP's post you're referring to). – sgbj Aug 09 '13 at 16:12
  • Please have a look at this [example](https://www.dropbox.com/s/nfyly2anqmo7v87/Skeleton.java), [example](https://www.dropbox.com/s/3cso39hh1efpcls/Skeleton1.java), [example](https://www.dropbox.com/s/3dhwdbfxmcvpsng/Skeleton2.java) and this [example](https://www.dropbox.com/s/671hqa629pbi95u/Skeleton3.java), every option has its own pros and cons associated with itself, it depends on the situation, which one is beneficial in the given senario. – nIcE cOw Aug 09 '13 at 16:12
  • Hope this link, [A Generic MVC Model in Java](http://www.onjava.com/pub/a/onjava/2004/07/07/genericmvc.html), will help you understand another way of achieving the same thingy, with a nice example :-) I wanted to put this as an answer, but I think this will make my answer too huge. – nIcE cOw Aug 09 '13 at 16:19
  • See also this [Q&A](http://stackoverflow.com/q/11662858/230513) about leaking `this` from a constructor. – trashgod Aug 09 '13 at 18:14

1 Answers1

4

It's cause Swing follows MVC pattern. If you delegate the Controller and View to one class then something wrong. Also read about Single Responsability Principle , a class only should be responsible to one thing , if not it's seems that your class is like a God that do everything.

Instead of

public class MyJFrame extends JFrame implements KeyListener{
     MyJFrame(){
       this.addKeyListener(this); // sounds awful           
     }
}

Use something like this:

public class MyFrameView {

private JFrame frame;

     MyFrameView(){
       frame = new JFrame();
       frame.addKeyListener(new MyKeyListener());
     }

}

public class MyKeyListener implements KeyListener{

}
nachokk
  • 14,363
  • 4
  • 24
  • 53