<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>Blog of Meng Yan</title>
	<link>http://mengyan.org/english</link>
	<description>Life is a sweet but sometime a bitter melody, and I love every note.</description>
	<pubDate>Mon, 29 May 2006 09:17:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0</generator>
	<language>en</language>
			<item>
		<title>Why use do {} while(0) in macro?</title>
		<link>http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/</link>
		<comments>http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/#comments</comments>
		<pubDate>Mon, 29 May 2006 09:15:44 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Others</category>
		<guid isPermaLink="false">http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/</guid>
		<description><![CDATA[Morgan wrote a good article which discussed how debugger works. You can check
this entry here(This article is in Chinese  ).
I noticed one code in Morgan&#8217;s comments:
#ifdef _DEBUG
#define LOG_INFO(format, str) do { printf(format, str); } while (0)
#else
#define LOG_INFO(format, str)
#endif
So, do you know why he use &#8220;do {} while (0)&#8221; instead of &#8220;{}&#8221; ?　
Here&#8217;s the explanation:
do..while(0) [...]]]></description>
			<content:encoded><![CDATA[<p>Morgan wrote a good article which discussed how debugger works. You can check<br />
this entry<a href="http://spaces.msn.com/morganchengmo/blog/cns!9950CE918939932E!561.entry?_c=BlogPart#permalink"> here</a>(This article is in Chinese <img src='http://mengyan.org/english/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ).</p>
<p>I noticed one code in Morgan&#8217;s comments:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline">#ifdef _DEBUG</li>
<li>#define LOG_INFO(format, str) do { printf(format, str); } while (0)</li>
<li>#else</li>
<li>#define LOG_INFO(format, str)</li>
<li>#endif</li></ol></div>
<p>So, do you know why he use &#8220;do {} while (0)&#8221; instead of &#8220;{}&#8221; ?　</p>
<p>Here&#8217;s the explanation:</p>
<p><a href="http://mkseo.pe.kr/blog/?p=28">do..while(0) in macro of LINUX kernel</a></p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2006. |
      <a href="http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/">Permalink</a> |
      <a href="http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/&amp;title=Why use do {} while(0) in macro?">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/others/" title="View all posts in Others" rel="category tag">Others</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2006/05/29/why-use-do-while0-in-macro/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Singleton &#038; Lazy Initialization</title>
		<link>http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/</link>
		<comments>http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/#comments</comments>
		<pubDate>Sun, 19 Mar 2006 16:16:23 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/</guid>
		<description><![CDATA[Do you know how to use lazy initialization in Java? There&#8217;s already many thread discussing this problem in the comunity, just to conclude here.
Due to the JVM memory model, Double-Checking Locking (DCL) will not work in Java, I&#8217;d like to recommend two articles talking about this.
The &#34;Double-Checked Locking is Broken&#34; Declaration
Double-checked locking and the Singleton [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know how to use lazy initialization in Java? There&#8217;s already many thread discussing this problem in the comunity, just to conclude here.</p>
<p>Due to the JVM memory model, Double-Checking Locking (DCL) will not work in Java, I&#8217;d like to recommend two articles talking about this.</p>
<p><a href="http://www.cs.umd.edu/%7Epugh/java/memoryModel/DoubleCheckedLocking.html">The &quot;Double-Checked Locking is Broken&quot; Declaration</a></p>
<p><a href="http://www-128.ibm.com/developerworks/library/j-dcl.html">Double-checked locking and the Singleton pattern </a></p>
<p>There&#8217;s another article tell you exactly what synchronized really means, very helpful </p>
<p> <a href="http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html">Double-checked locking: Clever, but broken</a></p>
<p>One possible way to use lazy initialization in Java:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">import</span><span style="color: Gray;"> </span><span style="color: Blue;">java</span><span style="color: Gray;">.</span><span style="color: Blue;">io</span><span style="color: Gray;">.*;</span></li>
<li><span style="color: Gray;">&nbsp; </span><span style="color: Green;">import</span><span style="color: Gray;"> </span><span style="color: Blue;">java</span><span style="color: Gray;">.</span><span style="color: Blue;">lang</span><span style="color: Gray;">.*;</span></li>
<li><span style="color: Gray;">&nbsp; </span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Singleton</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; </span><span style="color: #ffa500;">//&nbsp; &nbsp; private static final Singleton _INSTANCE = new Singleton();</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;"> </span><span style="color: Blue;">Singleton</span><span style="color: Olive;">()</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">System</span><span style="color: Gray;">.</span><span style="color: Blue;">out</span><span style="color: Gray;">.</span><span style="color: Blue;">println</span><span style="color: Gray;"> </span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">Construct Singleton Instance now</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span style="color: Blue;">Singleton</span><span style="color: Gray;"> </span><span style="color: Blue;">getInstance</span><span style="color: Olive;">()</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">/*</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(_INSTANCE == null)</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _INSTANCE = new Singleton();</span></li>
<li><span style="color: #ffa500;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span><span style="color: Gray;">&nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">return</span><span style="color: Gray;"> </span><span style="color: Blue;">SingletonHolder</span><span style="color: Gray;">.</span><span style="color: Blue;">INSTANCE</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">say</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">message</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">System</span><span style="color: Gray;">.</span><span style="color: Blue;">out</span><span style="color: Gray;">.</span><span style="color: Blue;">println</span><span style="color: Olive;">(</span><span style="color: Blue;">message</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">private</span><span style="color: Gray;"> </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">SingletonHolder</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span style="color: Blue;">Singleton</span><span style="color: Gray;"> </span><span style="color: Blue;">INSTANCE</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Singleton</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">main</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">args</span><span style="color: Olive;">[])</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">System</span><span style="color: Gray;">.</span><span style="color: Blue;">out</span><span style="color: Gray;">.</span><span style="color: Blue;">println</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">Hello world!</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">Singleton</span><span style="color: Gray;"> </span><span style="color: Blue;">instance</span><span style="color: Gray;"> = </span><span style="color: Blue;">Singleton</span><span style="color: Gray;">.</span><span style="color: Blue;">getInstance</span><span style="color: Olive;">()</span><span style="color: Gray;">;&nbsp; &nbsp; &nbsp; &nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; </span><span style="color: Olive;">}</span></li></ol></div>
<p>In the end, attach two articles about Singleton in C# in MSDN, there&#8217;s no problem using DCL in C#.</p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/impsingletonincsharp.asp"> Implementing Singleton in C#</a></p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp">Exploring the Singleton Design Pattern</a></p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2006. |
      <a href="http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/">Permalink</a> |
      <a href="http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/&amp;title=Singleton & Lazy Initialization">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2006/03/20/singleton-lazy-initialization/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Really! Simple Sharing!</title>
		<link>http://mengyan.org/english/2005/11/25/really-simple-sharing/</link>
		<comments>http://mengyan.org/english/2005/11/25/really-simple-sharing/#comments</comments>
		<pubDate>Fri, 25 Nov 2005 10:30:05 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Others</category>
		<guid isPermaLink="false">http://mengyan.org/english/2005/11/25/really-simple-sharing/</guid>
		<description><![CDATA[Just read Ray Ozzie&#8217;s post of &#8220;Really Simple Sharing&#8221; and many comments, mark it here.
&#8220;Microsoft XML News&#8221; - Tim Bray
&#8220;Sharing at so many levels!&#8221; - Dave Winer
&#8220;Microsoft proposal: Simple Sharing Extensions for RSS and OPML&#8221; - Alex Barnett
&#8220;RSS + OPML/ SSE = Really Simple Synchronization&#8221;
And here&#8217;s the SSE spec
Simple Sharing Extensions for RSS and OPML 

 [...]]]></description>
			<content:encoded><![CDATA[<p>Just read Ray Ozzie&#8217;s post of &#8220;<a href="http://spaces.msn.com/members/rayozzie/Blog/cns!1pyct_cYtbBtOBPDVAumMEdw!175.entry" title="Really Simple Sharing">Really Simple Sharing</a>&#8221; and many comments, mark it here.</p>
<p>&#8220;<a href="http://www.tbray.org/ongoing/When/200x/2005/11/21/Microsoft-XML-News">Microsoft XML News</a>&#8221; - Tim Bray</p>
<p>&#8220;<a href="http://www.scripting.com/2005/11/21.html#sharingAtSoManyLevels">Sharing at so many levels!</a>&#8221; - Dave Winer</p>
<p>&#8220;<a href="http://blogs.msdn.com/alexbarn/archive/2005/11/21/495253.aspx">Microsoft proposal: Simple Sharing Extensions for RSS and OPML</a>&#8221; - Alex Barnett</p>
<p>&#8220;<a href="http://blogs.zdnet.com/BTL/index.php?p=2187">RSS + OPML/ SSE = Really Simple Synchronization</a>&#8221;</p>
<p>And here&#8217;s the SSE spec</p>
<p><a href="http://msdn.microsoft.com/xml/rss/sse/">Simple Sharing Extensions for RSS and OPML </a>
</p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/11/25/really-simple-sharing/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/11/25/really-simple-sharing/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/11/25/really-simple-sharing/&amp;title=Really! Simple Sharing!">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/11/25/really-simple-sharing/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/others/" title="View all posts in Others" rel="category tag">Others</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/11/25/really-simple-sharing/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Method signatures of delegate and interface in C#</title>
		<link>http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/</link>
		<comments>http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/#comments</comments>
		<pubDate>Fri, 18 Nov 2005 06:53:49 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/</guid>
		<description><![CDATA[I ever write an article about the  event handling of Java and C#, just want to add some of C#&#8217;s delegate.
Delegate has no semantics carried, so does interface, it only provide method signatures.
Let&#8217;s look at the following example:
class Person
{
&#160; &#160; public interface Runnable
&#160; &#160; {
&#160; &#160; &#160; &#160; void run();
&#160; &#160; }
&#160;
&#160; &#160; public [...]]]></description>
			<content:encoded><![CDATA[<p>I ever write an article about the <a href="http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/"> event handling of Java and C#</a>, just want to add some of C#&#8217;s delegate.</p>
<p>Delegate has no semantics carried, so does interface, it only provide method signatures.</p>
<p>Let&#8217;s look at the following example:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Person</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">interface</span><span style="color: Gray;"> </span><span style="color: Blue;">Runnable</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">run</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">execute</span><span style="color: Olive;">(</span><span style="color: Blue;">Person</span><span style="color: Gray;">.</span><span style="color: Blue;">Runnable</span><span style="color: Gray;"> </span><span style="color: Blue;">runnable</span><span style="color: Olive;">)</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">{</span><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Gym</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">interface</span><span style="color: Gray;"> </span><span style="color: Blue;">Runnable</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">run</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
<p>Class Person&#8217;s execute method will not accept the instance that implements the Gym&#8217;s Runnable interface.</p>
<p>But in this case,</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Person</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Blue;">delegate</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">Runnable</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">execute</span><span style="color: Olive;">(</span><span style="color: Blue;">Person</span><span style="color: Gray;">.</span><span style="color: Blue;">Runnable</span><span style="color: Gray;"> </span><span style="color: Blue;">runnable</span><span style="color: Olive;">)</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">{</span><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Gym</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Blue;">delegate</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">Runnable</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
<p>It&#8217;s no problem for Person&#8217;s execute method to accept the Gym&#8217;s Runnable delegate.</p>
<p>So, delegate defines a single method signature to be wrapped by a delegate instance, but interface defines a set of method signatures to be implemented by a class.</p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/&amp;title=Method signatures of delegate and interface in C#">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/11/18/method-signatures-of-delegate-and-interface-in-c/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Farewell, Sun</title>
		<link>http://mengyan.org/english/2005/11/14/farewell-sun/</link>
		<comments>http://mengyan.org/english/2005/11/14/farewell-sun/#comments</comments>
		<pubDate>Mon, 14 Nov 2005 10:59:23 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Life</category>
		<guid isPermaLink="false">http://mengyan.org/english/2005/11/14/farewell-sun/</guid>
		<description><![CDATA[

    
    
    &#169; Meng Yan for Blog of Meng Yan, 2005. &#124;
      Permalink &#124;
      No comment
    Add to del.icio.us
    Search blogs linking this post with Technorati
    [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mengyan.org/english/wp-content/resources/Farewellletter.png"><img src="http://mengyan.org/english/wp-content/resources/Farewellletter.png" alt="Farewell Sun" /></a>
</p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/11/14/farewell-sun/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/11/14/farewell-sun/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/11/14/farewell-sun/&amp;title=Farewell, Sun">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/11/14/farewell-sun/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/life/" title="View all posts in Life" rel="category tag">Life</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/11/14/farewell-sun/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Get the Stack trace information in Java SE 5.0</title>
		<link>http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/</link>
		<comments>http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/#comments</comments>
		<pubDate>Fri, 21 Oct 2005 03:15:22 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/</guid>
		<description><![CDATA[JAVA SE 5.0 provides two very useful APIs to help us debug our program, All this two are all class &#34;Thread&#34;&#8217;s method:
public StackTraceElement[] getStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread.  This method will return a zero-length array if this thread has not started or has terminated. If [...]]]></description>
			<content:encoded><![CDATA[<p>JAVA SE 5.0 provides two very useful APIs to help us debug our program, All this two are all class &quot;Thread&quot;&#8217;s method:</p>
<p>public StackTraceElement[] getStackTrace()</p>
<blockquote>
<p>Returns an array of stack trace elements representing the stack dump of this thread.  This method will return a zero-length array if this thread has not started or has terminated. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence.  The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.  </p>
</blockquote>
<p>public static Map&lt;Thread, StackTraceElement[]&gt; getAllStackTraces()</p>
<blockquote>
<p>Returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.</p>
</blockquote>
<p>We can use this two APIs to get the thread&#8217;s stack trace information and help us debug or log. I will give a example that use this to write log.</p>
<p>If we are going to add log before, we need to add a &quot;java.util.logging.Logger&quot; to our class and use it to log. It&#8217;s ok and works pretty well, the following code just wanna to give you an example of the new exposed API &quot;getStackTrace&quot;. </p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">log</span><span style="color: Olive;">(</span><span style="color: Blue;">Level</span><span style="color: Gray;"> </span><span style="color: Blue;">level</span><span style="color: Gray;">, </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">message</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">Thread</span><span style="color: Gray;"> </span><span style="color: Blue;">currentThread</span><span style="color: Gray;"> = </span><span style="color: Blue;">Thread</span><span style="color: Gray;">.</span><span style="color: Blue;">currentThread</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">StackTraceElement</span><span style="color: Olive;">[]</span><span style="color: Gray;"> </span><span style="color: Blue;">sts</span><span style="color: Gray;"> = </span><span style="color: Blue;">currentThread</span><span style="color: Gray;">.</span><span style="color: Blue;">getStackTrace</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">if</span><span style="color: Gray;"> </span><span style="color: Olive;">(</span><span style="color: Blue;">sts</span><span style="color: Gray;"> != </span><span style="color: Green;">null</span><span style="color: Gray;"> &amp;&amp; </span><span style="color: Blue;">sts</span><span style="color: Gray;">.</span><span style="color: Blue;">length</span><span style="color: Gray;"> &gt; </span><span style="color: Maroon;">3</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">StackTraceElement</span><span style="color: Gray;"> </span><span style="color: Blue;">ste</span><span style="color: Gray;"> = </span><span style="color: Blue;">sts</span><span style="color: Olive;">[</span><span style="color: Maroon;">3</span><span style="color: Olive;">]</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">fileName</span><span style="color: Gray;"> = </span><span style="color: Blue;">ste</span><span style="color: Gray;">.</span><span style="color: Blue;">getFileName</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">className</span><span style="color: Gray;"> = </span><span style="color: Blue;">ste</span><span style="color: Gray;">.</span><span style="color: Blue;">getClassName</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">methodName</span><span style="color: Gray;"> = </span><span style="color: Blue;">ste</span><span style="color: Gray;">.</span><span style="color: Blue;">getMethodName</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="hl-types">int</span><span style="color: Gray;"> </span><span style="color: Blue;">lineNum</span><span style="color: Gray;"> = </span><span style="color: Blue;">ste</span><span style="color: Gray;">.</span><span style="color: Blue;">getLineNumber</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">LogRecord</span><span style="color: Gray;"> </span><span style="color: Blue;">record</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">LogRecord</span><span style="color: Olive;">(</span><span style="color: Blue;">level</span><span style="color: Gray;">, </span><span style="color: Blue;">message</span><span style="color: Gray;"> + </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">At line </span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> + </span><span style="color: Blue;">lineNum</span><span style="color: Gray;"> + </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;"> in file </span><span style="color: #8b0000;">&quot;</span><span style="color: Gray;"> + </span><span style="color: Blue;">fileName</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">record</span><span style="color: Gray;">.</span><span style="color: Blue;">setSourceClassName</span><span style="color: Olive;">(</span><span style="color: Blue;">className</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">record</span><span style="color: Gray;">.</span><span style="color: Blue;">setThreadID</span><span style="color: Olive;">((</span><span class="hl-types">int</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Blue;">currentThread</span><span style="color: Gray;">.</span><span style="color: Blue;">getId</span><span style="color: Olive;">())</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">record</span><span style="color: Gray;">.</span><span style="color: Blue;">setSourceMethodName</span><span style="color: Olive;">(</span><span style="color: Blue;">methodName</span><span style="color: Olive;">)</span><span style="color: Gray;">;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">logger_</span><span style="color: Gray;">.</span><span style="color: Blue;">log</span><span style="color: Olive;">(</span><span style="color: Blue;">record</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"> </span><span style="color: Green;">else</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">logger_</span><span style="color: Gray;">.</span><span style="color: Blue;">log</span><span style="color: Olive;">(</span><span style="color: Blue;">level</span><span style="color: Gray;">, </span><span style="color: Blue;">message</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span></li></ol></div>
<p>Caught  the meaning? Yes, it&#8217;s pretty simple.</p>
<p>sts[0] = java.lang.Thread :: dumpThreads</p>
<p>sts[1] = java.lang.Thread :: getStackTrace </p>
<p>sts[2] = LogTest :: log ( This function ) </p>
<p>sts[3] = Where you called the log function</p>
<p>And you can use this stack trace information to finish other features. </p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/&amp;title=Get the Stack trace information in Java SE 5.0 ">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/10/21/get-the-stack-trace-information-in-java-se-50/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>My English Blog</title>
		<link>http://mengyan.org/english/2005/10/15/my-english-blog/</link>
		<comments>http://mengyan.org/english/2005/10/15/my-english-blog/#comments</comments>
		<pubDate>Sat, 15 Oct 2005 06:07:27 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Life</category>
		<guid isPermaLink="false">http://mengyan.org/english/2005/10/15/my-english-blog/</guid>
		<description><![CDATA[I have not updated my english blog for a long time, I decided to start a new one here. I will backup all my english posts here, sorry for the incovenience and hope these will do you some help. 
BTW: I also write chinese blog, if you are interested, you can access here. 
  [...]]]></description>
			<content:encoded><![CDATA[<p>I have not updated my <a href="http://blogs.sun.com/dreamwords">english blog</a> for a long time, I decided to start a new one <a href="http://www.mengyan.org/english/">here</a>. I will backup all my english posts here, sorry for the incovenience and hope these will do you some help. </p>
<p>BTW: I also write chinese blog, if you are interested, you can access <a href="http://www.mengyan.org/blog/">here</a>. </p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/10/15/my-english-blog/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/10/15/my-english-blog/#comments">One comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/10/15/my-english-blog/&amp;title=My English Blog">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/10/15/my-english-blog/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/life/" title="View all posts in Life" rel="category tag">Life</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/10/15/my-english-blog/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>How to offset Lilina&#8217;s display timezone?</title>
		<link>http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/</link>
		<comments>http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/#comments</comments>
		<pubDate>Mon, 22 Aug 2005 04:04:12 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/?p=6</guid>
		<description><![CDATA[One of my US friends ask me how to offset the timestamp of Lilina RSS aggregator. He lives in the east coast but his web host is in the west. 
In fact, it&#8217;s very easy to do that, and I do think Lilina should add such support.
1) Add the following line in &#34;conf.php&#34;
&#60; ?php
$TIMEZONE_OFFSET = [...]]]></description>
			<content:encoded><![CDATA[<p>One of my US friends ask me how to offset the timestamp of <a href="http://lilina.sourceforge.net/">Lilina</a> RSS aggregator. He lives in the east coast but his web host is in the west. </p>
<p>In fact, it&#8217;s very easy to do that, and I do think Lilina should add such support.</p>
<p>1) Add the following line in &quot;conf.php&quot;</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Gray;">&lt; ?php</span></li>
<li><span style="color: Gray;">$TIMEZONE_OFFSET = 12;</span></li>
<li><span style="color: Gray;">?&gt;</span></li></ol></div>
<p>The value is the timezone offset between your host and your home. (In my case, it&#8217;s 12 hours)</p>
<p>2) In &quot;index.php&quot;</p>
<p>Find the following lines:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Gray;">&lt; ?php</span></li>
<li><span style="color: Gray;">if ($x['date_timestamp'] == '') </span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; $x['date_timestamp'] = create_time($x['title'] . $x['link']);</span></li>
<li><span style="color: Gray;">?&gt;</span></li></ol></div>
<p>Add this line:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Gray;">&lt; ?php</span></li>
<li><span style="color: Gray;">else</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; $x['date_timestamp'] += $TIMEZONE_OFFSET * 60 * 60;</span></li>
<li><span style="color: Gray;">?&gt;</span></li></ol></div>
<p>3) In &quot;lib.php&quot;</p>
<p>Find the function &quot;create_time&quot;, then change the line</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Gray;">&lt; ?php</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; if ($time_table[$md5] &lt;= 0) $time_table[$md5] = time()</span></li>
<li><span style="color: Gray;">?&gt;</span></li></ol></div>
<p>to</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Gray;">&lt; ?php</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; if ($time_table[$md5] &lt;= 0) $time_table[$md5] = time() + $TIMEZONE_OFFSET * 60 * 60;</span></li>
<li><span style="color: Gray;">?&gt;</span></li></ol></div>
<p>Upload all these three files to your server and test it, it should work well now.</p>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/&amp;title=How to offset Lilina's display timezone?">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/08/22/how-to-offset-lilinas-display-timezone/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Scripting for Java</title>
		<link>http://mengyan.org/english/2005/08/11/scripting-for-java/</link>
		<comments>http://mengyan.org/english/2005/08/11/scripting-for-java/#comments</comments>
		<pubDate>Thu, 11 Aug 2005 02:00:24 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/?p=7</guid>
		<description><![CDATA[JSR223 &#34;describe mechanisms allowing scripting language programs to access information developed in the Java Platform and allowing scripting language pages to be used in Java Server-side Applications.&#34;

This defines a framework to allow scripting language programs to access information developed in the Java platform. We currently plan to integrate this into Mustang for b40. Aside from [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jcp.org/en/jsr/detail?id=223">JSR223</a> &quot;describe mechanisms allowing scripting language programs to access information developed in the Java Platform and allowing scripting language pages to be used in Java Server-side Applications.&quot;</p>
<blockquote>
<p>This defines a framework to allow scripting language programs to access information developed in the Java platform. We currently plan to integrate this into Mustang for b40. Aside from the framework, we will also include a JavaScript engine based on the <a href="http://www.mozilla.org/rhino/" target="_blank">Mozilla Rhino</a> implementation. Later, we hope to include a scripting shell that is script language independent. This will be a very cool way to create a prototype, do some exploratory coding, and learn new APIs. </p>
</blockquote>
<p>The latest release of <a href="https://mustang.dev.java.net/">Mustang</a> has already included this feature. You can check out the latest release of JavaSE to have a try.</p>
<p><a id="more-7"></a></p>
<p>There are 3 blog entries to talk about this:</p>
<p><a href="http://www.davidflanagan.com/blog/2005_08.html#000074">Example: using JavaScript in Java </a></p>
<p><a href="http://blogs.sun.com/roller/page/sundararajan?entry=simple_javascripting_from_java">Simple JavaScripting from Java</a> </p>
<p><a href="http://blogs.sun.com/roller/page/roumen?entry=scripting_support_in_mustang_an">Scripting Support in Mustang - An Example You Can Try </a></p>
<p>Then why adding scripting languages to Java? </p>
<blockquote>
<p>two ways of using Java aware scripting languages: exploratory programming and testing, and user level scripting of applications. - <a href="http://www.ociweb.com/jnb/archive/jnbMar2001.html">Scripting Languages For Java</a> </p>
</blockquote>
<p>What interested me most is that it is <strong>a framework but not a simple implementation</strong>. So, any other scripting languages (such as PHP, Ruby &#8230; )can be added to Java if someone develop a module implementation following the framework standard. So, it&#8217;s &quot;<a href="http://www.mengyan.org/blog/archives/2005/08/09/45.html">An architecture of participation</a>&quot;.</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: #ffa500;">//import package</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">import</span><span style="color: Gray;"> </span><span style="color: Blue;">javax</span><span style="color: Gray;">.</span><span style="color: Blue;">script</span><span style="color: Gray;">.*; </span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Main</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">static</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">main</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Olive;">[]</span><span style="color: Gray;"> </span><span style="color: Blue;">args</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">try</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// create a script engine manager</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">ScriptEngineManager</span><span style="color: Gray;"> </span><span style="color: Blue;">manager</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">ScriptEngineManager</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// create script engine for JavaScript</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">ScriptEngine</span><span style="color: Gray;"> </span><span style="color: Blue;">jsengine</span><span style="color: Gray;"> = </span><span style="color: Blue;">manager</span><span style="color: Gray;">.</span><span style="color: Blue;">getEngineByName</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">js</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// evaluate JavaScript code from String</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">jsengine</span><span style="color: Gray;">.</span><span style="color: Blue;">eval</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">print('hello world')</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"> </span><span style="color: Green;">catch</span><span style="color: Gray;"> </span><span style="color: Olive;">(</span><span style="color: Blue;">ScriptException</span><span style="color: Gray;"> </span><span style="color: Blue;">se</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// Handle script exception here..</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #ffa500;">// FIXME: do a better job here!</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">se</span><span style="color: Gray;">.</span><span style="color: Blue;">printStackTrace</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/08/11/scripting-for-java/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/08/11/scripting-for-java/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/08/11/scripting-for-java/&amp;title=Scripting for Java">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/08/11/scripting-for-java/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/08/11/scripting-for-java/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Events Handling in Java and C#</title>
		<link>http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/</link>
		<comments>http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/#comments</comments>
		<pubDate>Fri, 22 Jul 2005 11:42:33 +0000</pubDate>
		<dc:creator>Meng Yan</dc:creator>
		
	<category>Tech</category>
		<guid isPermaLink="false">http://mengyan.org/english/?p=4</guid>
		<description><![CDATA[Actually, this article is posted about one year before, at here. Today, I happened to see this article, interesting. So, I found my old entry and post here. Also, I post all the comments at the end, it&#8217;s really valuable I think. 
Recently(2004.08.06), a friend of mine discussed the Event handle issues in C# and [...]]]></description>
			<content:encoded><![CDATA[<p>Actually, this article is posted about one year before, at <a href="http://blogs.sun.com/roller/page/dreamwords/Weblog/events_handling_in_java_and?catname=">here</a>. Today, I happened to see <a href="http://www.duduwolf.com/post/192.asp">this</a> article, interesting. So, I found my old entry and post here. Also, I post all the comments at the end, it&#8217;s really valuable I think. </p>
<p>Recently(2004.08.06), a friend of mine discussed the Event handle issues in C# and Java with me, sounds interesting. I would like to blog the discussion and my investigation here.</p>
<p>C# (pronounced C Sharp) is a programming language developed by MS as part of its .NET platform. When I saw it the first time, I ask myseft:&quot;is it another version of Java?&quot;. Just a joke, C# is extremely similar to Java: GC, VM, single inheritance, interfaces, packages(use &quot;using&quot; instead of &quot;import&quot; <img src='http://mengyan.org/english/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ), pure OO etc. However, C# also invent many new features, delegate is a famous one.</p>
<p>Ok, let&#8217;s talk about the event-handle issues. In Java, we use the well-known Observer Pattern (Gamma et, Design Patterns) to implmented the events. For example, if we wanna to handle the Button&#8217;s Click event, we simply add a ActionListener to this button:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Blue;">JButton</span><span style="color: Gray;"> </span><span style="color: Blue;">button</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">JButton</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">ok</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">button</span><span style="color: Gray;">.</span><span style="color: Blue;">setActionCommand</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">ok</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">button</span><span style="color: Gray;">.</span><span style="color: Blue;">addActionListener</span><span style="color: Olive;">(</span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Handler</span><span style="color: Olive;">())</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">......</span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Handler</span><span style="color: Gray;"> </span><span style="color: Green;">implements</span><span style="color: Gray;"> </span><span style="color: Blue;">ActionListener</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">actionPerformed</span><span style="color: Olive;">(</span><span style="color: Blue;">ActionEvent</span><span style="color: Gray;"> </span><span style="color: Blue;">ev</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">command</span><span style="color: Gray;"> = </span><span style="color: Blue;">ev</span><span style="color: Gray;">.</span><span style="color: Blue;">getActionCommand</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">if</span><span style="color: Gray;"> </span><span style="color: Olive;">(</span><span style="color: Blue;">command</span><span style="color: Gray;"> == </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">OK</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">onOK</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; ......</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
<p>Or we can even add listener in following way:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Blue;">button</span><span style="color: Gray;">.</span><span style="color: Blue;">addActionListener</span><span style="color: Olive;">(</span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">ActionListener</span><span style="color: Olive;">()</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">actionPerformed</span><span style="color: Olive;">(</span><span style="color: Blue;">ActionEvent</span><span style="color: Gray;"> </span><span style="color: Blue;">ev</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">command</span><span style="color: Gray;"> = </span><span style="color: Blue;">ev</span><span style="color: Gray;">.</span><span style="color: Blue;">getActionCommand</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Green;">if</span><span style="color: Gray;"> </span><span style="color: Olive;">(</span><span style="color: Blue;">command</span><span style="color: Gray;"> == </span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">OK</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Blue;">onOK</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; &nbsp; &nbsp; ......</span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">})</span><span style="color: Gray;">;</span></li></ol></div>
<p>In C#, Anders Hejlsberg introduced the new keyword &quot;delegate&quot;, I think it&#8217;s actually like the type-safe function pointers. Anders really love the function pointers, he bring this to Delphi &amp; Visual J++ too. Let&#8217;s first take a look at how this keyword works:</p>
<p>In Java, Since the listener rely on the interfaces, so, we can not call the methods without knowledge of the target object. For example:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Class1</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">show</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">s</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{}</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Green;">class</span><span style="color: Gray;"> </span><span style="color: Blue;">Class2</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; </span><span style="color: Green;">public</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">show</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">s</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{}</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
<p>A more complex example is the method whose name can be variant, you can find detail information in <a href="http://www.onjava.com/pub/a/onjava/2003/05/21/delegates.html">this</a> article.</p>
<p>Although these two classes have common method, but because they do not share a common interface, so, we can not call them in a uniform way. How to solve the problem? In Java, we can use the Proxy Pattern(Gamma et, Design Patterns) and Adapter Pattern(Gamma et, Design Patterns) or use the reflection mechanism to solve the problem. In C#, ok, it&#8217;s delegate&#8217;s work.</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Green;">public</span><span style="color: Gray;"> </span><span style="color: Blue;">delegate</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">show</span><span style="color: Olive;">(</span><span style="color: Blue;">String</span><span style="color: Gray;"> </span><span style="color: Blue;">s</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">show</span><span style="color: Gray;"> </span><span style="color: Blue;">s1</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">show</span><span style="color: Olive;">(</span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Class1</span><span style="color: Olive;">()</span><span style="color: Gray;">.</span><span style="color: Blue;">show</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">s1</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">Test</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">s1</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">show</span><span style="color: Olive;">(</span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Class2</span><span style="color: Olive;">()</span><span style="color: Gray;">.</span><span style="color: Blue;">show</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">s1</span><span style="color: Olive;">(</span><span style="color: #8b0000;">&quot;</span><span style="color: Red;">Test</span><span style="color: #8b0000;">&quot;</span><span style="color: Olive;">)</span><span style="color: Gray;">;</span></li></ol></div>
<p>The most important usage of delegates is for event handling:</p>
<div class="hl-surround"><ol class="hl-main"><li class="hl-firstline"><span style="color: Blue;">Button</span><span style="color: Gray;"> </span><span style="color: Blue;">button</span><span style="color: Gray;"> = </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">Button</span><span style="color: Olive;">()</span><span style="color: Gray;">;</span></li>
<li><span style="color: Gray;"></span><span style="color: Blue;">button</span><span style="color: Gray;">.</span><span style="color: Blue;">Click</span><span style="color: Gray;"> += </span><span style="color: Green;">new</span><span style="color: Gray;"> </span><span style="color: Blue;">EventHandler</span><span style="color: Olive;">(</span><span style="color: Blue;">onOK</span><span style="color: Olive;">)</span><span style="color: Gray;">; </span><span style="color: #ffa500;">// BTW: we can see, method is the first-class object in C#.</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp;</span></li>
<li><span style="color: Gray;"></span><span style="color: Green;">private</span><span style="color: Gray;"> </span><span class="hl-types">void</span><span style="color: Gray;"> </span><span style="color: Blue;">onOK</span><span style="color: Olive;">(</span><span style="color: Gray;">...</span><span style="color: Olive;">)</span><span style="color: Gray;"> </span><span style="color: Olive;">{</span><span style="color: Gray;"></span></li>
<li><span style="color: Gray;">&nbsp; &nbsp; ......</span></li>
<li><span style="color: Gray;"></span><span style="color: Olive;">}</span></li></ol></div>
<p>Ok, that is the mechanism C# handle events. How about it? I think everyone has his cents. In my opnion, at the first look, it&#8217;s very attractive to me, things get easy now. But soon I feel uncomfortable. I don&#8217;t think this function pointer-like mechanism should exist in a pure OO languages <img src='http://mengyan.org/english/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . So, I would rather to use patterns to keep the code clean.</p>
<p>To summarize:<br />
  At the above situation:<br />
  1) If I can controll all the code, then I would rather retrofit a common interface that has &quot;show&quot; method;<br />
  2) If things are not that easy, for example: all classes are library codes, or method has different name, I&#8217;d like to use adapter and proxy pattern to solve this problems.</p>
<p>Just my cents. <img src='http://mengyan.org/english/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<blockquote>
<p>Woty - not bad. delegate is some how usefule. but i think method should not be the first-class object. In common OO theory: Class := data + operation on data. if member operation is first-class, how about member varible?</p>
</blockquote>
<blockquote>
<p>Mijia - I think the delegate is just the wrapper for the function pointer or callback function in C/C++, which cannot be appeared in Java or C#, because they have their restrict security architecture. As well as first class object discussed, I do not know the exact ly defined concept, so I could not tell which idea would be correct. But I would like to regard it like that callback function or function pointer seems very dangerous in C/C++, but it is a fantastic mechanism which could provide flexibility for the software design. And I think Anders Hejlsberg from the C# loved it the most, so he cannot resist to add such mechanism into C#. But in Java, for the consideration of security, the function pointer is strictly forbidden, but Java could use the Reflect to emulate the behavior of the callback function or function pointers. And it is simple and flexible as well. So I think perhaps &quot;function as first class&quot; may be not pretty in the view of grammar or sementics, and in C# it is just one&#8217;s idea, some camouflage beneath the security flag.</p>
</blockquote>
    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; Meng Yan for <a href="http://mengyan.org/english">Blog of Meng Yan</a>, 2005. |
      <a href="http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/">Permalink</a> |
      <a href="http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/#comments">No comment</a></p>
    <p>Add to <a href="http://del.icio.us/post?url=http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/&amp;title=Events Handling in Java and C#">del.icio.us</a></p>
    <p>Search blogs linking this post with <a href="http://www.technorati.com/search/http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/" title="Search on Technorati">Technorati</a></p>
    <p>Want more on these topics ? Browse the archive of posts filed under <a href="http://mengyan.org/english/category/tech/" title="View all posts in Tech" rel="category tag">Tech</a>.</p>]]></content:encoded>
			<wfw:commentRSS>http://mengyan.org/english/2005/07/22/events-handling-in-java-and-c/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
