<?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>nariyu memo &#187; オフロスキー</title>
	<atom:link href="http://nariyu.playwell.jp/tag/%e3%82%aa%e3%83%95%e3%83%ad%e3%82%b9%e3%82%ad%e3%83%bc/feed/" rel="self" type="application/rss+xml" />
	<link>http://nariyu.playwell.jp</link>
	<description>メモ</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:19:00 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Flex でビュー（デザイン）とロジックの分離</title>
		<link>http://nariyu.playwell.jp/2009/09/separation-of-view-and-logic/</link>
		<comments>http://nariyu.playwell.jp/2009/09/separation-of-view-and-logic/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 03:31:34 +0000</pubDate>
		<dc:creator>nariyu</dc:creator>
				<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[MXML]]></category>
		<category><![CDATA[オフロスキー]]></category>

		<guid isPermaLink="false">http://www.playwell.jp/blog/nariyu/?p=311</guid>
		<description><![CDATA[呼んだ？呼んだよね？
さあ今日はね。 Flex アプリでレイアウトや配色などのデザイン（ビュー）とロジックを分離するやり方を考えてみようよ。
なに？大丈夫だよ。絶対できるって。
じゃあ始めるよ。
すごく簡単なウェブアプリ [...]]]></description>
			<content:encoded><![CDATA[<p>呼んだ？呼んだよね？<br />
さあ今日はね。 Flex アプリでレイアウトや配色などのデザイン（ビュー）とロジックを分離するやり方を考えてみようよ。</p>
<p>なに？大丈夫だよ。絶対できるって。<br />
じゃあ始めるよ。</p>
<p>すごく簡単なウェブアプリなら「 Flex ってこんなに簡単にできちゃうよー」ってサンプルを紹介するサイトなんかにあるように MXML だけでもできちゃうことがあるけど、ちょっと複雑になってくるとそうはいかないよね。</p>
<p>ってゆうか、そんな「MXML だけで作れちゃうウェブアプリ」のサンプルはエラーハンドリングを実装していない場合がほとんどだしね。<br />
だって Flex を布教するためにはエラーハンドリングみたいな地味で面白くもない技術を紹介してもしょうがないもん。<br />
仕方ないよね。うん。</p>
<p>・・・で、ちょっと複雑になると &lt;mx:Script&gt; タグを使って MXML に実装を書くこともあるけど、規模が大きくなってくると「やっぱりビューとロジックは分けるべきじゃない？」って言い出す人がいるよね。</p>
<p>面倒だけど仕方ないよ。</p>
<p>そんな訳でここらへんでビューとロジックの分離方法についておさらいしておこうね。</p>
<p>やり方はたくさんあると思うけど、今回はよく使われる二つの方法を復習するよ。<br />
いいかい。ちゃんとついてきてよ。</p>
<p><span id="more-311"></span></p>
<h3>方法1）IMXMLObject を使ったビューとロジックの分離</h3>
<p>IMXMLObject は Flex SDK に含まれる Adobe が推奨する方法（？）だね。</p>
<p>パッケージ名を含む完全なクラス名は <strong>mx.core.IMXMLObject</strong> だよ。<br />
mx.core！ なんか大切そうな感じだね。</p>
<p>でも中身はかなりシンプルなんだ。コメントを除くとこれだけだよ↓</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> mx.core
<span style="color: #000000;">&#123;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> interface IMXMLObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #339966; font-weight: bold;">function</span> initialized<span style="color: #000000;">&#40;</span>document<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, id<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>これの使い方は簡単。<br />
まずは views/MyCustomComponent.mxml っていうファイルを作って、こんな風に書く↓</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span></span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">    width=<span style="color: #ff0000;">&quot;400&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #000000;">    horizontalAlign=<span style="color: #ff0000;">&quot;center&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;button&quot;</span> label=<span style="color: #ff0000;">&quot;ボタン&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>これはただの白い四角形の中にボタンが置いてあるだけの簡単なカスタムコンポーネントだよ。<br />
これがレイアウトや配色等のデザイン、つまり「<strong>ビュー</strong>」ってことになるんだ。</p>
<p>で、このボタンをクリックするとメッセージが表示されるようにロジックを実装してみるよ。</p>
<p>そのためにさっき出てきた IMXMLObject を実装したクラスを作る。ここではこれをヘルパーと呼ぶことにしよう。</p>
<p>ファイル名は views/helpers/MyCustomComponentHelper.as という ActionScrtipt ファイルだ。<br />
そこに次のように書く↓</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> views.helpers
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.core.IMXMLObject;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.FlexEvent;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> views.MyCustomComponent;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyCustomComponentHelper implements IMXMLObject
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> view<span style="color: #000000; font-weight: bold;">:</span>MyCustomComponent;
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyCustomComponentHelper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> initialized<span style="color: #000000;">&#40;</span>document<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, id<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            view = document <span style="color: #0033ff; font-weight: bold;">as</span> MyCustomComponent;
            view.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>FlexEvent.CREATION_COMPLETE,
                creationCompleteHandler<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> creationCompleteHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span>FlexEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            view.button.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>,
                button_clickHandler<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> button_clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;ボタンがクリックされたよ&quot;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>急に長くなってきたけど、これだけではまだ動かないから最初に作った MXML ファイルにこのヘルパーを追記するよ。</p>
<p>こんな感じだ。</p>
<p>views/MyCustomComponent.mxml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span></span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">    xmlns:helpers=<span style="color: #ff0000;">&quot;views.helpers.*&quot;</span></span>
<span style="color: #000000;">    width=<span style="color: #ff0000;">&quot;400&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span></span>
<span style="color: #000000;">    horizontalAlign=<span style="color: #ff0000;">&quot;center&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;helpers:MyCustomComponentHelper</span> id=<span style="color: #ff0000;">&quot;helper&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;button&quot;</span> label=<span style="color: #ff0000;">&quot;ボタン&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>4行目と7行目が追加されたヘルパーだ。</p>
<p>これでカスタムコンポーネントが完成した。ビューである MyCustomCompoent.mxml にはロジックは書かれていない。</p>
<p>ここまでのパッケージ構成はこんな感じになる↓</p>
<p><img src="http://blog.playwell.jp/nariyu/wp-content/uploads/2009/09/separate-of-view-and-logic-1.png" alt="separate-of-view-and-logic-1" title="separate-of-view-and-logic-1" width="326" height="108" class="alignnone size-full wp-image-325" /></p>
<p>あとは、アプリケーションにビューを追加すればOK。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span></span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">    xmlns:views=<span style="color: #ff0000;">&quot;views.*&quot;</span></span>
<span style="color: #000000;">    layout=<span style="color: #ff0000;">&quot;absolute&quot;</span></span>
<span style="color: #000000;">    <span style="color: #7400FF;">&lt;views:MyCustomComponent</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>これで目的は達成できたね。</p>
<p>だけど、ちょっと待って。ヘルパーの中に「view.addEventListener(ほにゃらら」とか 「view.button.addEventListener(ほにゃらら」ってのが出てきてる。</p>
<p>規模が大きくなってくるとなんだかいちいち「view.」って書くのが面倒だよね。</p>
<p>それに別のクラスからヘルパーのメソッドを呼ぶときには「myCustomComponent.helper.メソッド()」って書かなきゃいけない。<br />
「myCustomComponent.helper.ほにゃらら」ってなにそれ長い！面倒！</p>
<p>そこで最近よく使っているのが次の方法なんだ。</p>
<h3>方法2）ロジックでビューを継承しちゃおうよ法（仮称）</h3>
<p>これはこの名前の通り、「ビューを規定クラスにして、ロジックをそのサブクラスとして実装する」っていう方法だ。</p>
<p>まずは規定クラスになる<strong>ビュー</strong>を <strong>MXML</strong> で作るよ。</p>
<p>views/mxml/MyCustomComponentView.mxml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span></span>
<span style="color: #000000;">    xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">    width=<span style="color: #ff0000;">&quot;400&quot;</span> height=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> id=<span style="color: #ff0000;">&quot;button&quot;</span> label=<span style="color: #ff0000;">&quot;ボタン&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<p>これでビューは完成だよ。ヘルパーなんて面倒なものは書かなくていいよ。</p>
<p>次にロジックをビューのサブクラスとして ActionScript ファイルで作るんだ。</p>
<p>views/MyCustomComponent.as</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> views
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
    <span style="color: #0033ff; font-weight: bold;">import</span> mx.events.FlexEvent;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> views.mxml.MyCustomComponentView;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyCustomComponent extends MyCustomComponentView
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyCustomComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>FlexEvent.CREATION_COMPLETE,
                creationCompleteHandler<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> creationCompleteHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span>FlexEvent<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            button.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>,
                button_clickHandler<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> button_clickHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;ボタンがクリックされたよ&quot;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>これだけで完成だよ。簡単でしょ？簡単だよね？<br />
あとは MyCustomComponent をアプリケーションに追加すればいいよ。<br />
MXML ファイルのほうじゃなくて ActionScript ファイルのほうだよ。</p>
<p>このパッケージ構成はこんな感じになるよ↓</p>
<p><img src="http://blog.playwell.jp/nariyu/wp-content/uploads/2009/09/separate-of-view-and-logic-2.png" alt="separate-of-view-and-logic-2" title="separate-of-view-and-logic-2" width="332" height="108" class="alignnone size-full wp-image-334" /></p>
<p>これならロジックを実装したクラスに「view.ほにゃらら」とかつけなくてよくなりかなりすっきり！<br />
しかもビューのなかにヘルパーとか書かなくてもいい！行数も短くなってすっきりすっきり！</p>
<p>そしてこれの一番いいところは MXML の中に &lt;mx:Script&gt; タグでロジックを実装しちゃった昔の Flex アプリも簡単にビューとロジックを分離した構造に修正できるってことなんだ。<br />
つまり、規模が小さいうちは MXML の中にロジックを書いて、規模が大きくなってきたらビューとロジックを分離するっていうやりかたも比較的簡単にできちゃうってこと。</p>
<p>わお、ステキだね！</p>
<p>さあ君もやってみよう。</p>
<p>（オフロスキーって知ってますか？）</p>
]]></content:encoded>
			<wfw:commentRss>http://nariyu.playwell.jp/2009/09/separation-of-view-and-logic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
