<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Victor Farazdagi &#187; Java</title>
	<atom:link href="http://www.phpmag.ru/category/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmag.ru</link>
	<description>Phrozn, Phing &#38; Zend Framework Musings</description>
	<lastBuildDate>Fri, 19 Aug 2011 08:47:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>3 ways to initialize class attributes in Java</title>
		<link>http://www.phpmag.ru/2009/07/22/3-ways-to-initialize-class-attributes-in-java/</link>
		<comments>http://www.phpmag.ru/2009/07/22/3-ways-to-initialize-class-attributes-in-java/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 23:22:48 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java tips]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=695</guid>
		<description><![CDATA[While skimming through amazing Core Java book earlier on today, I re-discovered the initialization blocks theme, that can be used to setup your class data fields. There are two common ways to initialize class attributes: Provide value in declaration, i.e. you set the value as you introduce data field Set value in constructor public class [...]]]></description>
			<content:encoded><![CDATA[<p>While skimming through amazing <a href="http://www.amazon.com/Core-Java-I-Fundamentals-8th-Sun/dp/0132354764/ref=sr_1_3?ie=UTF8&amp;s=books&amp;qid=1248217134&amp;sr=1-3">Core Java</a> book earlier on today, I re-discovered the initialization blocks theme, that can be used to setup your class data fields. </p>
<p>There are two common ways to initialize class attributes:</p>
<ol>
<li>Provide value in declaration, i.e. you set the value as you introduce data field</li>
<li>Set value in constructor</li>
</ol>
<pre name="code" class="java">
public class Testy {
    private int a = 12; // you set value in declaration
    private int b;
    private int c;

    public Testy()
    {
        this.b = 21;      // you set value in constructor
    }
}
</pre>
<p>However, there&#8217;s actually a third way:</p>
<ol start="3">
<li>Use initialization block</li>
</ol>
<pre name ="code" class="java">
public class Testy {
    private int a = 12; // you set value in declaration
    private int b;
    private int c;

    public Testy()
    {
        this.b = 21;      // you set value in constructor
    }

    // object initialization block
    // executed every time object of the class is constructed
    {
        this.c = 44;
    }
}
</pre>
<p>What benefit this structure has over other methods? You can use this to abstract common construction logic (when you have several constructors relying on some common initialization). But, the same effect could be achieved by calling common constructor via this() as the first statement to every constructor requiring prior call to common constructor. Overall, I can hardly think about any use case that initialization block could resolve better than using constructors directly. It might be issue of preference.</p>
<p>However, initialization blocks could still be very useful. When? In situations where constructors do not provide a solution: yes I am talking about static fields. If you need some complex logic initializing your static fields, static initialization block resolves the issue neatly:</p>
<pre name="code" class="java">
public class Testy {
    protected static int x;

    // static initialization block
    static   // Note the word "static" before initialization block curly braces.
    {
        Random generator = new Random();
        Testy.x = generator.nextInt(100);
    }
}
</pre>
<p> NB:<br />
Initialization blocks are executed before constructors and after fields are initialized to their default values (which are either values provided in field declaration or one of <em>true</em>, <em>0</em>, <em>null</em> depending on type). Static initialization takes place only once &#8211; when class is loade</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/07/22/3-ways-to-initialize-class-attributes-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Aggregation vs Composition</title>
		<link>http://www.phpmag.ru/2009/02/11/aggregation-vs-composition/</link>
		<comments>http://www.phpmag.ru/2009/02/11/aggregation-vs-composition/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 15:28:32 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[Object-Oriented]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=383</guid>
		<description><![CDATA[As I got into Java seriously, I decided to give several Design Patterns books I own yet another try &#8211; I have skimmed through GoF&#8217;s master-piece number of times, but being a PHP programmer in PHP4 age, I just had no incentive to study patterns any deeper, as there clearly was no option to apply [...]]]></description>
			<content:encoded><![CDATA[<p>As I got into Java seriously, I decided to give several Design Patterns books I own yet another try &#8211; I have skimmed through GoF&#8217;s master-piece number of times, but being a PHP programmer in PHP4 age, I just had no incentive to study patterns any deeper, as there clearly was no option to apply them. PHP5 is better wrt OO-tion but still looks like a toy.</p>
<p>Overall, I don&#8217;t understand the whole idea of implementing simple and easy-to-learn language, get popular, and then redesign the language to include all the goodies from &#8220;real languages like C/C++, Java&#8221;. But that&#8217;s topic for another post, I guess <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So, now when I have a little more time to experiment with Java, and as it is as object-oriented as one might dream, I wanted to study patterns in details. I plan to post number of articles later this month, but for now, I wanted to capture one (important if you are planning to use UML and Patterns) simple concept &#8211; that&#8217;s difference between Aggregation and Composition. As simple as it might seem, there quite a lot of people who don&#8217;t understand it.<br />
<span id="more-383"></span><br />
First of all, both Aggregation and Composition are Associations, and actually share characteristics, so I wouldn&#8217;t bother you with &#8220;has a&#8221;, &#8220;is a&#8221;, &#8220;is part of&#8221; cliches, which aren&#8217;t as helpful as it&#8217;s believed.</p>
<p>Very good <a href="http://www.bletchleypark.net/algorithms/software/oop.html">definition</a> of Aggregation, goes as following:</p>
<p><em>Aggregation is a whole/part association where a client object contains one or more server objects, but the client object can be defined with or without the creation of the server object. For instance, a car can contain golf clubs in its back trunk but the car is still a car with or without the golf clubs.<br />
</em></p>
<p>Let me translate. Aggregation is a &#8220;whole/part&#8221; association, and is used to describe object relationship where object A (client, aggregate, whole) <strong>contains</strong> object(s) B (server, component, part). Here, important word is &#8220;contains&#8221; &#8211; client literally contains the server object(s), it&#8217;s not composed of them. </p>
<p>Actually, that&#8217;s the sole difference between Aggregation and Composition, where later is a special case of former.</p>
<p>Before I provide some code examples, here is similar definition of Composition (do compare it with definition of Aggregation):</p>
<p><em>Composition is a stronger whole/part association where a client object is composed of one or more server objects, such that without the creation of the one or more server objects the client object could not be created. For instance, a car&#8217;s composition consists of an engine among other objects. However, the car would not be considered a car if it did not have an engine.</em></p>
<p>So, Aggregation vs. Composition is really a matter of control over life-cycle of parts within the whole. With Aggregation you can attach parts to whole at any time &#8211; for the whole to be initialized presence of parts that the whole might contain is not required. With Composition you have another story: since parts constitute the logical whole with the main object i.e. object is considered to be the sum of all its parts, you have to initialize parts at a time the container object is initialized. </p>
<p>Let&#8217;s have a look at examples.</p>
<p>As Aggregation refers to containment, and in Java code might look as following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Aggregation <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Player Kaka <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Player<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Team team <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Team<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        team.<span style="color: #006633;">addPlayer</span><span style="color: #009900;">&#40;</span>Kaka<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Kaka is now contained!</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Client, Aggregate, Whole
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Team <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ArrayList</span> players<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Team<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// player list is empty, we can attach players via addPlayer(), so</span>
        <span style="color: #666666; font-style: italic;">// that team CONTAINS players, however our team object is not dependant</span>
        <span style="color: #666666; font-style: italic;">// on players for initialization - we can initialize Team objects even</span>
        <span style="color: #666666; font-style: italic;">// if we have no intention to put players into them</span>
        players <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addPlayer<span style="color: #009900;">&#40;</span>Player p<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        players.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Dump Part class
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you see Team object can exist without any players, and we can attach Player objects at any point after Team&#8217;s initialization.</p>
<p>Now, lets turn our attention to Composition example. Here parts should be initialized (generally within constructor of container class) prior to full initialization of container. Do understand that decision on which association is more appropriate depends on our aims, and we can re-write the Team/Player example to express composition if we <em>believe</em> that Team is <strong>composed</strong> of Players (so no entity could be called Team if it hasn&#8217;t got players):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Composition <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// player objects life-cycle is totally under container class controll</span>
        Team team <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Team<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Client, Aggregate, Whole
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Team <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ArrayList</span> players<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Team<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        players <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        players.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Player<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Dump Part class
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Player <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you see the difference is more conceptual. Indeed, when it comes to code, we can use the same constructs (member variables in this case) quite differently. If we have Aggregation in mind, then our container class can hold/contain components, while it doesn&#8217;t interested in initialization of those components (and destruction &#8211; which is not an issue in Java, due to garbage collection, but should be taken into consideration for, say, C++). If what we mean is Composition, then our container object is literally composed of those parts, and as such initializes them at construction time.</strong></strong></p>
<p>What I want to stress: despite the fact that difference between Aggregation and Composition might be academical at times, their conceptual difference is not that small. And on conceptual level you should understand what you are dealing with. You might be wrong thinking that Aggregation is a better abstraction for a Team/Player concept, but you should understand what aggregation means.</p>
<p>Sorry for such a lengthy explanation of what in essence is quite a simple concept.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/02/11/aggregation-vs-composition/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Java: Изменение внешнего вида программ.</title>
		<link>http://www.phpmag.ru/2009/01/19/java-switching-look-and-feels/</link>
		<comments>http://www.phpmag.ru/2009/01/19/java-switching-look-and-feels/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 22:45:53 +0000</pubDate>
		<dc:creator>Victor Farazdagi</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java tips]]></category>

		<guid isPermaLink="false">http://www.phpmag.ru/?p=296</guid>
		<description><![CDATA[На Java изменить внешний вид программы (так называемые LaF &#8211; Look&#8217;n'Feel), до крайнего просто. Достаточно задать имя установленного интерфеса в UIManager.setLookAndFeel(): String laf = &#34;com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel&#34;; // определяем новый LaF UIManager.setLookAndFeel&#40;laf&#41;; // устанавливаем новый LaF SwingUtilities.updateComponentTreeUI&#40;this&#41;; // обновляем вн. вид компонентов Для того чтобы посмотреть какие LaF у вас имеются в наличии, делаем следующее: UIManager.LookAndFeelInfo&#91;&#93; lafs [...]]]></description>
			<content:encoded><![CDATA[<p>На Java изменить внешний вид программы (так называемые LaF &#8211; Look&#8217;n'Feel), до крайнего просто. Достаточно задать имя установленного интерфеса в UIManager.setLookAndFeel():</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> laf <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// определяем новый LaF</span>
<span style="color: #003399;">UIManager</span>.<span style="color: #006633;">setLookAndFeel</span><span style="color: #009900;">&#40;</span>laf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// устанавливаем новый LaF</span>
<span style="color: #003399;">SwingUtilities</span>.<span style="color: #006633;">updateComponentTreeUI</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// обновляем вн. вид компонентов</span></pre></div></div>

<p>Для того чтобы посмотреть какие LaF у вас имеются в наличии, делаем следующее:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">UIManager.<span style="color: #006633;">LookAndFeelInfo</span></span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> lafs <span style="color: #339933;">=</span> <span style="color: #003399;">UIManager</span>.<span style="color: #006633;">getInstalledLookAndFeels</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">UIManager.<span style="color: #006633;">LookAndFeelInfo</span></span> info <span style="color: #339933;">:</span> lafs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Вот так вот все просто!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpmag.ru/2009/01/19/java-switching-look-and-feels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

