Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  The main idea I didn't get from the Sun tutorial.  (Read 6058 times)
0 Members and 1 Guest are viewing this topic.
Offline ewjordan

Junior Member





« Reply #30 - Posted 2008-08-25 21:46:12 »

To me it seems that Riven is saying that the "Inheritance is Evil" rule should apply here.  That rule says that whenever you have a choice, you should prefer composition to inheritance (which means adding functionality by calling out to members rather than extending classes).  That's especially true when you're not actually extending the functionality of a class, you're just using it.

It's a bit tough to justify the rule in a simple example, but suffice it to say that the rule tends to show up pretty high on the OO-advice list of most experienced designers (in any OO language, not just Java - the C++ guys have been screaming about this for decades).  The gist of it is that IS-A is a much tighter coupling between classes than HAS-A, so changes in one class are far more likely to have side effects outside of that file, which makes maintenance that much more of a bitch.  Even Gosling is often quoted (usually by functional programmers that think objects are ruining Real Programming) as saying that the "extends" keyword is one of the worst things about Java - programming to an interface and using composition leads to more maintainable systems in most situations.

In this case it's not a huge deal, since JPanel is for all intents and purposes frozen in time because Sun doesn't want to piss off its entire user base (so you're not likely to have surprise name clashes or anything like that), but it's not gaining you anything to extend it, and it's a bad habit to get into.  The fact that all the Java tutorials use JPanels by extending them is particularly annoying, since it implies that there's some reason you need to extend them in order to use them, which is just not true, and it's very confusing to newcomers.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #31 - Posted 2008-08-26 00:13:35 »

Take a look at the average "hello world" introduction to Swing (or worse: introduction to Java!)



How many years does it take one to fully grasp this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public class MyFrame extends JFrame implements ActionListener
{
   public static void main(String[] args)
   {
      new MyFrame("Hello World");
   }

   public MyFrame()
   {
      JButton b = new JButton("click me");
      b.addActionListener(this); // BLACK MAGIC for n00bs

      // what is 'this' ?

      this.setLayout(new BorderLayout());
      this.add(b, BorderLayout.NORTH);
      this.pack();
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // SAY WHAT?
  }

   public void actionPerformed(ActionEvent event)
   {
      System.out.println("you clicked me");
   }
}



And why this code crashes or just hangs every once in a while, as we are manipulating Swing from the main thread!




This is very much like how my classmates learned to write their first few lines of Java, when wondering what the difference between an Object, a primitive and a Class was - not to mention Interfaces like ActionListener would be coded 'because the sample code had them'. Anyway... just to show how completely f*cked up most of us (?) got introduced to the wonderful world of programming.


[size=8pt]
Our teacher was to bad, that he had code like MyFrame, MyButton, MyPanel, MyList - just for implementing the event listeners. And when there was nothing to listen for, I stumbled across:

public class MyCheckbox extends Checkbox {
   public MyCheckbox() {
       super();
   }
}

and

public class MyMap extends HashMap {
   public MyMap() {
      super();
   }

   public void myPut(Object key, Object value) {
     this.put(key, val);
   }
}

Okay... that's enough offtopic stuff for today.
[/size]

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!