<?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>株式会社エス・スリー・フォー &#187; misc</title>
	<atom:link href="http://www.s34.co.jp/category/cpptechdoc/misc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.s34.co.jp</link>
	<description>株式会社エス・スリー・フォーは、一人一人のスキルを重視したシステム・ネットワーク構築のプロフェッショナル集団です。蓄積されたノウハウと最新技術により、お客様が考えられているシステムを明確にし、ご提案させて頂きます。</description>
	<lastBuildDate>Mon, 23 Jan 2012 07:59:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>デバッグするとバグが出る?! : DLLにまつわる問題</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/bug-dll/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/bug-dll/#comments</comments>
		<pubDate>Mon, 10 Apr 2006 10:38:10 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=4169</guid>
		<description><![CDATA[※ このアーティクルはWindows/NT環境下でMicrosoft Visual C++ 6.0 SP3 による開発の最中に出くわした&#8217;厄介&#8217;なバグとその顛末です。 debug-modeに切り換えたとたんバグが消えてしまうという症状に悩まされるのはよくあることですが、逆にdebug-modeでしか発生しないバグという何とも奇妙な&#8230; 何が起こったのか ここに掲載するアーティクルのため、小さなお試しコードを書いていました。XMLのチュートリアルXMLを用いた状態遷移で、IBM alphaWorksの XML4C(XML Parser for C++)を使ってXMLファイルをパースする試みの最中でした。 「こんなのどーってことないや」とタカをくくって鼻歌混じりにコードをこさえ、コンパイルも実行もいたって正常に完了しました。 僕は普段はもっぱらrelease-modeでコンパイル/実行を繰り返しながら開発を進めていきます。debug-modeは、たまに不可解なバグに遭遇するときにお世話になる程度なんです。 で、何の気なしにdebug-modeでコンパイルし、実行してみると突如として なんてダイアログが現われたのです。release-modeでは何の問題もなかったというのに&#8230; Visual Studioのデバッガは以下のコードがヘンだと主張していました: DOMString x(&#34;hello&#34;); char* tmp = x.transcode(); cout &#60;&#60; tmp &#60;&#60; endl; delete[] tmp; // ここでassert この最後の delete[] tmp;で&#34;不正なヒープ領域を解放した!&#34;と怒っているようです。 XML4Cが提供するクラスDOMStringはその名のとおりDOM(Document Object Model)内で使われているUNICODE文字列です。DOMStringのメソッドtranscode()は内包するUNICODE文字列をnativeなコード、すなわちShift_JISに変換し、その先頭ポインタを返してくれます。XML4Cのマニュアルには、 DOMString::transcode() で得られたポインタはきちんと解放するように と書かれていたので、ぼくはその注意を忠実に守っただけですよ&#8230; その原因は? いくらrelease-modeでおっけーだったからといって、debug-modeでこんなシビアなassert吐くようでは納得いきません。コード生成オプションをさんざんいじくりまわしてダメ!と言われる組み合わせを模索し、ようやくその原因を突き止めました&#8230; XML4CはDLLとリンクライブラリ、そしてヘッダファイルで構成されています。「もしや&#8230;」と思ってDLLが使っているDLLを調べたところ、MSVCRT.DLLが使われていたんです。 MSVCRT.DLLとは、Visual C++のruntime-libraryをDLL化したものです。runtime-libraryをDLL化し、それを呼び出すことにすれば、それぞれの実行モジュールにruntime-libraryがリンクされませんから、実行モジュール(ここではDLL)はそれだけコンパクトになります。 不具合の原因はここにありました。 XML4Cは変換結果を格納する領域をXML4C自身が確保します。このとき operator new が使われています。これは最終的には [...]]]></description>
			<content:encoded><![CDATA[<p>※ このアーティクルはWindows/NT環境下でMicrosoft Visual C++ 6.0 SP3 による開発の最中に出くわした&#8217;厄介&#8217;なバグとその顛末です。</p>
<p>debug-modeに切り換えたとたんバグが消えてしまうという症状に悩まされるのはよくあることですが、逆にdebug-modeでしか発生しないバグという何とも奇妙な&#8230;</p>
<h3>何が起こったのか</h3>
<p><a href="http://www.s34.co.jp/category/cpptechdoc/">ここ</a>に掲載するアーティクルのため、小さなお試しコードを書いていました。XMLのチュートリアル<a href="http://www.s34.co.jp/cpptechdoc/article/xml-fsm/">XMLを用いた状態遷移</a>で、IBM alphaWorksの XML4C(XML Parser for C++)を使ってXMLファイルをパースする試みの最中でした。</p>
<p><strong>「こんなのどーってことないや」</strong>とタカをくくって鼻歌混じりにコードをこさえ、コンパイルも実行もいたって正常に完了しました。</p>
<p>僕は普段はもっぱらrelease-modeでコンパイル/実行を繰り返しながら開発を進めていきます。debug-modeは、たまに不可解なバグに遭遇するときにお世話になる程度なんです。</p>
<p>で、何の気なしにdebug-modeでコンパイルし、実行してみると突如として</p>
<p><img src="http://www.s34.co.jp/wp-content/uploads/2010/02/assert.gif" alt="Debug Assertion Failed!" title="assert" width="393" height="226" class="aligncenter size-full wp-image-4320" /></p>
<p>なんてダイアログが現われたのです。release-modeでは何の問題もなかったというのに&#8230;</p>
<p>Visual Studioのデバッガは以下のコードがヘンだと主張していました:</p>
<pre>
<code>  <strong>DOMString</strong> x(&quot;hello&quot;);
  char* tmp = x.<strong>transcode</strong>();
  cout &lt;&lt; tmp &lt;&lt; endl;
  delete[] tmp; <em>// ここでassert</em></code>
</pre>
<p>この最後の <code>delete[] tmp;</code>で&quot;不正なヒープ領域を解放した!&quot;と怒っているようです。</p>
<p>XML4Cが提供するクラスDOMStringはその名のとおりDOM(Document Object Model)内で使われているUNICODE文字列です。DOMStringのメソッドtranscode()は内包するUNICODE文字列をnativeなコード、すなわちShift_JISに変換し、その先頭ポインタを返してくれます。XML4Cのマニュアルには、</p>
<blockquote><p>DOMString::transcode() で得られたポインタは<strong>きちんと解放するように</strong></p></blockquote>
<p>と書かれていたので、ぼくはその注意を忠実に守っただけですよ&#8230;</p>
<h3>その原因は?</h3>
<p>いくらrelease-modeでおっけーだったからといって、debug-modeでこんなシビアなassert吐くようでは納得いきません。コード生成オプションをさんざんいじくりまわしてダメ!と言われる組み合わせを模索し、ようやくその原因を突き止めました&#8230;</p>
<p>XML4CはDLLとリンクライブラリ、そしてヘッダファイルで構成されています。「もしや&#8230;」と思ってDLLが使っているDLLを調べたところ、<strong>MSVCRT.DLL</strong>が使われていたんです。</p>
<p><strong>MSVCRT.DLL</strong>とは、Visual C++のruntime-libraryをDLL化したものです。runtime-libraryをDLL化し、それを呼び出すことにすれば、それぞれの実行モジュールにruntime-libraryがリンクされませんから、実行モジュール(ここではDLL)はそれだけコンパクトになります。</p>
<p class="note"><strong>不具合の原因はここにありました。</strong></p>
<p>XML4Cは変換結果を格納する領域をXML4C自身が確保します。このとき operator new が使われています。これは最終的には runtime-library が提供するヒープ領域確保関数が呼び出されることになります。つまりこれはMSVCRT.DLLにある確保関数です。</p>
<p>一方、XML4Cを利用するアプリケーションをdebug-modeで構築すると、マルチスレッド/rutime-DLL(オプション:-MDd)のとき、使われるライブラリは<strong>MSVCRT<em>D</em>.DLL</strong>です。</p>
<p>すると、DLLとアプリケーションとで、使われるruntime-libraryが異なることになります。</p>
<p>この状況においても通常はほとんど問題は生じません。が、僕が遭遇した状況では、<strong>(DLLが使っている)release版runtimeが確保した領域を(アプリケーションが使っている)debug版runtimeが解放する</strong>ことになります。</p>
<p>debug版runtimeのヒープ領域確保関数はデバッグのための小さな領域を余分に確保し、解放関数でそこを検証することでヒープ領域の不正なアクセスを検出します。</p>
<p>release版runtimeが確保したヒープ領域には、そのデバッグ用のエリアが存在しません。にもかかわらずdebug版の解放関数が検証を行ない、結果的に<strong>異常</strong>と判断されたのです。</p>
<h3>どうやって解決したか</h3>
<p>ほとんどの場合、DLLがどんなコンパイル・オプションで構築されたかなんて気にする必要はありません(し、実際気にしないでしょう)。その意味で今回の例は非常に特殊といえます。DLLのコンパイル・オプションまで調査するハメになったのは、<strong>DLLが確保した領域をアプリケーションが解放する</strong>という特殊な場合だからです。</p>
<p>このDLLを作ったのが僕ならば、確保した領域を解放する関数もついでに提供するでしょう。そうすれば領域の解放はDLL内で行われるため、領域確保時との整合性が保てます。</p>
<p>しかしながらXML4Cは天下のIBM製、ソースコードが公開されているとはいえ、勝手にいぢくるわけにもまいりません。使う側で何らかの手段を講じることにしました。</p>
<p>まず、DLLとアプリケーションとのコンパイルオプションを一致させること。XML4Cを使うときは:</p>
<ul>
<li>マルチスレッド</li>
<li>ランタイムDLL</li>
<li>release-mode</li>
</ul>
<p>でなくてはなりません(コンパイル・オプション <strong>-MD</strong>)。</p>
<p>最初の2つについてはコードの冒頭に以下のような&#8217;オマジナイ&#8217;を書いておくことで適合しないコンパイル・オプションを抑止しました:</p>
<pre>
<code><em>/* マルチスレッド / runtime-DLL でないならコンパイル停止 */</em>
#if !defined(<strong>_MT</strong>) || !defined(<strong>_DLL</strong>)
#  error sorry, multithread &amp; runtime-DLL only.
#endif</code>
</pre>
<p>まことに厳しいのが3番目の制約です。これはすなわちXML4Cを使うアプリケーションをdebug-mode(コンパイル・オプション<strong>-MD<em>d</em></strong>)で構築できないことを意味します。</p>
<p>XML4CのAPIドキュメントを丹念に読み返し、幸いにもライブラリ側で領域を取るんじゃなくて、アプリケーション側で用意した領域に出力するUNICODE-&gt;native文字列の変換関数 XMLString::transcode() を見つけました。debug-modeではこれを使うようにコードの修正を行ないました。</p>
<pre>
<code>#ifndef _DEBUG

  <em>// release-mode</em>
  std::string transocode(const DOMString&amp; x)  {
    char* tmp = x.transcode();
    std::string ret(tmp);
    delete[] tmp;
    return ret;
  }

#else

  <em>// debug-mode 変換結果を格納する領域はアプリケーション側で確保する</em>
  std::string  transcode(const DOMString&amp; x)  {
    const wchar_t* buf = x.rawBuffer(); <em>// UNICODEバッファと</em>
    unsigned       len = x.length();        <em>// その長さ</em>
    wchar_t*       wcs = <strong>new wchar_t[len+1]</strong>; <em>// 領域確保</em>
    char*          mbs = reinterpret_cast&lt;char*&gt;(wcs);
    *std::copy(buf, buf+len, wcs) = L'\0'; <em>// UNICODEバッファからコピー</em>
    XMLString::transcode(wcs, mbs, len*2+1); <em>// nativeコードに変換</em>

    std::string ret(mbs); <em>// std::stringを生成</em>
    <strong>delete[] wcs</strong>; <em>// 領域解放</em>
    return ret;
  }

#endif

  DOMString x(&quot;Hello&quot;);
  cout &lt;&lt; transcode(x) &lt;&lt; endl;</code>
</pre>
<p>ま、こんなやりかたで急場をしのいだわけですが、このwork-aroundはできれば避けたいですね。だってrelase時とdebug時では異なるコードがコンパイルされるわけで、それでは何の為のdebugだかわかりませんからね。</p>
<p class="note"><strong>教訓 : DLLが領域確保する関数を作ったら、その領域を解放する関数も公開すべし!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/bug-dll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; 最初の例</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 07:37:21 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=1280</guid>
		<description><![CDATA[&#8216;リファクタリング(Refactoring)&#8217;をご存知ですか? 「外部から見たときの振る舞いを保ちつつ、理解や修正が簡単になるように、ソフトウェアの内部構造を変化させること」です。 プログラムの改造や拡張に先立って適切なリファクタリングを行うことでその後の作業が楽になります。逆に&#8217;乱れた&#8217;状態のまま改造/拡張を繰り返すと次第にあちこちにほころびが目立ち始め、バグも出やすくなります。リファクタリングを行わずに改造/拡張を続けたソフトウェアは、さながら建て増しを繰り返して迷路と化した温泉旅館の様相を呈してきます。 このリファクタリングの様々なテクニックや定石を解説した本: &#8220;リファクタリング &#8211; プログラム体質改善のテクニック&#8221; マーチン・ファウラー 著 ピアソン・エデュケーション IDBN4-89471-228-8 は、変化に強く、単純で美しいコードを維持するための頼りになるガイドブックです。是非ご一読を。 この本は第一章：「リファクタリング-最初の例」から始まります。ビデオレンタルの料金計算を題材に、コード群にリファクタリングを施していく過程が詳しく解説されています。 「リファクタリング」では言語としてJavaが用いられています。C++/C#/VB.NETに翻訳するのはさほど苦労することはありませんが、やはり「C++/C#/VB.NET によるリファクタリング-最初の例」が欲しくなります。 また、この章ではリファクタリングによって最終的にどんなコードへと変貌を遂げたのか、その&#8217;生まれ変わったリスト&#8217;が示されていません。 そこで、本アーティクルでは&#8217;リファクタリング前&#8217;と&#8217;リファクタリング後&#8217;のそれぞれのコードを Java, C++, C#, および VB.NET で示します。あなたのコードを磨くサンプルとしてお役立てください。 リファクタリング前 Java (before_java.zip) C++ (before_cpp.zip) C# (before_cs.zip) VB.NET (before_vb.zip) リファクタリング後 Java (after_java.zip) C++ (after_cpp.zip) C# (after_cs.zip) VB.NET (after_vb.zip)]]></description>
			<content:encoded><![CDATA[<p>&#8216;<strong>リファクタリング</strong>(Refactoring)&#8217;をご存知ですか? 「外部から見たときの振る舞いを保ちつつ、理解や修正が簡単になるように、ソフトウェアの内部構造を変化させること」です。</p>
<p>プログラムの改造や拡張に先立って適切なリファクタリングを行うことでその後の作業が楽になります。逆に&#8217;乱れた&#8217;状態のまま改造/拡張を繰り返すと次第にあちこちにほころびが目立ち始め、バグも出やすくなります。リファクタリングを行わずに改造/拡張を続けたソフトウェアは、さながら建て増しを繰り返して迷路と化した温泉旅館の様相を呈してきます。</p>
<p>このリファクタリングの様々なテクニックや定石を解説した本:</p>
<p><strong>&#8220;リファクタリング &#8211; プログラム体質改善のテクニック&#8221;<br/><br />
マーチン・ファウラー 著<br/><br />
ピアソン・エデュケーション IDBN4-89471-228-8</strong></p>
<p>は、変化に強く、単純で美しいコードを維持するための頼りになるガイドブックです。是非ご一読を。</p>
<p>この本は第一章：「リファクタリング-最初の例」から始まります。ビデオレンタルの料金計算を題材に、コード群にリファクタリングを施していく過程が詳しく解説されています。</p>
<p>「リファクタリング」では言語としてJavaが用いられています。C++/C#/VB.NETに翻訳するのはさほど苦労することはありませんが、やはり「C++/C#/VB.NET によるリファクタリング-最初の例」が欲しくなります。</p>
<p>また、この章ではリファクタリングによって最終的にどんなコードへと変貌を遂げたのか、その&#8217;生まれ変わったリスト&#8217;が示されていません。</p>
<p>そこで、本アーティクルでは&#8217;リファクタリング前&#8217;と&#8217;リファクタリング後&#8217;のそれぞれのコードを Java, C++, C#, および VB.NET で示します。あなたのコードを磨くサンプルとしてお役立てください。</p>
<h3>リファクタリング前</h3>
<ul>
<li><a href="/cpptechdoc/misc/refactoring-before-java/">Java</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/before_java.zip'>before_java.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-before-cpp/">C++</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/before_cpp.zip'>before_cpp.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-before-cs/">C#</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/before_cs.zip'>before_cs.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-before-vb/">VB.NET</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/before_vb.zip'>before_vb.zip</a>)</li>
</ul>
<h3>リファクタリング後</h3>
<ul>
<li><a href="/cpptechdoc/misc/refactoring-after-java/">Java</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/after_java.zip'>after_java.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-after-cpp/">C++</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/after_cpp.zip'>after_cpp.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-after-cs/">C#</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/after_cs.zip'>after_cs.zip</a>)</li>
<li><a href="/cpptechdoc/misc/refactoring-after-vb/">VB.NET</a> (<a href='http://www.s34.co.jp/wp-content/uploads/2010/01/after_vb.zip'>after_vb.zip</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング前(Java)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-java/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-java/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3067</guid>
		<description><![CDATA[Movie.java // Movie.java public class Movie { public static final int CHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASE = 1; private String _title; private int _priceCode; public Movie(String title, int priceCode) { _title = title; _priceCode = priceCode; } public int getPriceCode() { return _priceCode; } public [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.java</h3>
<pre lang="Java">
// Movie.java

public class Movie {

  public static final int CHILDRENS = 2;
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;

  private String _title;
  private int _priceCode;

  public Movie(String title, int priceCode) {
    _title = title;
    _priceCode = priceCode;
  }

  public int getPriceCode() {
    return _priceCode;
  }

  public void setPriceCode(int arg) {
    _priceCode = arg;
  }

  public String getTitle() {
    return _title;
  }

}
</pre>
<h3>Rental.java</h3>
<pre>
// Rental.java

public class Rental {

  private Movie _movie;
  private int _daysRented;

  public Rental(Movie movie, int daysRented) {
    _movie = movie;
    _daysRented = daysRented;
  }

  public int getDaysRented() {
    return _daysRented;
  }

  public Movie getMovie() {
    return _movie;
  }

}
</pre>
<h3>Customer.java</h3>
<pre>
// Customer.java
import java.util.*;

public class Customer {

  private String _name;
  private Vector _rentals = new Vector();

  public Customer(String name) {
    _name = name;
  }

  public void addRental(Rental arg) {
    _rentals.addElement(arg);
  }

  public String getName() {
    return _name;
  }

  public String statement() {
    double totalAmount = 0;
    int frequentRenterPoints = 0;
    Enumeration rentals = _rentals.elements();
    String result = "Rental Record for " + getName() + "\n";
    while ( rentals.hasMoreElements() ) {
      double thisAmount = 0;
      Rental each = (Rental)rentals.nextElement();
      // 一行ごとに金額を計算
      switch ( each.getMovie().getPriceCode() ) {
        case Movie.REGULAR :
          thisAmount += 2;
          if ( each.getDaysRented() &gt; 2 )
            thisAmount += (each.getDaysRented() - 2) * 1.5;
          break;
        case Movie.NEW_RELEASE :
          thisAmount += each.getDaysRented() * 3;
          break;
        case Movie.CHILDRENS :
          thisAmount += 1.5;
          if ( each.getDaysRented() &gt; 3 )
            thisAmount += (each.getDaysRented() - 3) * 1.5;
          break;
      }
      // レンタルポイントを加算
      frequentRenterPoints++;
      // 新作を二日以上借りた場合はボーナスポイント
      if ( (each.getMovie().getPriceCode() == Movie.NEW_RELEASE) &amp;&amp;
           each.getDaysRented() &gt; 1 )
        frequentRenterPoints++;
      // この貸し出しに対する数値の表示
      result += "\t" + each.getMovie().getTitle() + "\t" +
                String.valueOf(thisAmount)+ "\n";
      totalAmount += thisAmount;
    }
    // フッタ部分の追加
    result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
    result += "You earned " + String.valueOf(frequentRenterPoints) +
              " frequent renter points";
    return result;
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング後(Java)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-java/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-java/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3076</guid>
		<description><![CDATA[Movie.java // Movie.java public class Movie { public static final int CHILDRENS = 2; public static final int REGULAR = 0; public static final int NEW_RELEASE = 1; private String _title; private Price _price; public Movie(String title, int priceCode) { _title = title; setPriceCode(priceCode); } public int getPriceCode() { return _price.getPriceCode(); } public void setPriceCode(int [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.java</h3>
<pre>
// Movie.java

public class Movie {

  public static final int CHILDRENS = 2;
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;

  private String _title;
  private Price  _price;

  public Movie(String title, int priceCode) {
    _title = title;
    setPriceCode(priceCode);
  }

  public int getPriceCode() {
    return _price.getPriceCode();
  }

  public void setPriceCode(int arg) {
    switch ( arg ) {
    case REGULAR :
      _price = new RegularPrice();
      break;
    case CHILDRENS :
      _price = new ChildrensPrice();
      break;
    case NEW_RELEASE :
      _price = new NewReleasePrice();
      break;
    default:
      throw new IllegalArgumentException("不正な料金コード");
    }
  }

  public String getTitle() {
    return _title;
  }

  public double getCharge(int daysRented) {
    return _price.getCharge(daysRented);
  }

  public int getFrequendRenterPoints(int daysRented) {
    return _price.getFrequentRenterPoints(daysRented);
  }

}
</pre>
<h3>Rental.java</h3>
<pre>
// Rental.java

public class Rental {

  private Movie _movie;
  private int _daysRented;

  public Rental(Movie movie, int daysRented) {
    _movie = movie;
    _daysRented = daysRented;
  }

  public int getDaysRented() {
    return _daysRented;
  }

  public Movie getMovie() {
    return _movie;
  }

  public double getCharge() {
    return _movie.getCharge(_daysRented);
  }

  public int getFrequentRenterPoints() {
    return _movie.getFrequendRenterPoints(_daysRented);
  }

}
</pre>
<h3>Customer.java</h3>
<pre>
// Customer.java

import java.util.*;

public class Customer {

  private String _name;
  private Vector _rentals = new Vector();

  public Customer(String name) {
    _name = name;
  }

  public void addRental(Rental arg) {
    _rentals.addElement(arg);
  }

  public String getName() {
    return _name;
  }

  public String statement() {
    Enumeration rentals = _rentals.elements();
    String result = "Rental Record for " + getName() + "\n";
    while ( rentals.hasMoreElements() ) {
      Rental each = (Rental)rentals.nextElement();
      // この貸し出しに対する数値の表示
      result += "\t" + each.getMovie().getTitle() + "\t" +
                String.valueOf(each.getCharge())+ "\n";
    }
    // フッタ部分の追加
    result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
    result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) +
              " frequent renter points";
    return result;
  }

  double getTotalCharge() {
    double result = 0;
    Enumeration rentals = _rentals.elements();
    while ( rentals.hasMoreElements() ) {
      Rental each = (Rental)rentals.nextElement();
      result += each.getCharge();
    }
    return result;
  }

  double getTotalFrequentRenterPoints() {
    int result = 0;
    Enumeration rentals = _rentals.elements();
    while ( rentals.hasMoreElements() ) {
      Rental each = (Rental)rentals.nextElement();
      result += each.getFrequentRenterPoints();
    }
    return result;
  }

}
</pre>
<h3>Price.java</h3>
<pre>
// Price.java

public abstract class Price {

  abstract public int getPriceCode();
  abstract public double getCharge(int daysRented);
  public int getFrequentRenterPoints(int daysRented) {
    return 1;
  }
}

class ChildrensPrice extends Price {

  public int getPriceCode() {
    return Movie.CHILDRENS;
  }

  public double getCharge(int daysRented) {
    double result = 1.5;
    if ( daysRented &gt; 3 )
      result += (daysRented -3) * 1.5;
    return result;
  }

}

class NewReleasePrice extends Price {

  public int getPriceCode() {
    return Movie.NEW_RELEASE;
  }

  public double getCharge(int daysRented) {
    return daysRented * 3;
  }

  public int getFrequentRenterPoints(int daysRented) {
    return ( daysRented &gt; 1 ) ? 2 : 1;
  }

}

class RegularPrice extends Price {

  public int getPriceCode() {
    return Movie.REGULAR;
  }

  public double getCharge(int daysRented) {
    double result = 2;
    if ( daysRented &gt; 2 )
      result += (daysRented -2) * 1.5;
    return result;
  }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング前(C++)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cpp/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cpp/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3080</guid>
		<description><![CDATA[Movie.h #ifndef __MOVIE_H__ #define __MOVIE_H__ #include &#60;string&#62; class Movie { public: enum price_code { CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1 }; Movie(std::string title, price_code priceCode); price_code getPriceCode() const; void setPriceCode(price_code arg); std::string getTitle() const; private: std::string _title; price_code _priceCode; }; #endif Movie.cpp #include "Movie.h" Movie::Movie(std::string title, price_code priceCode) : _title(title), _priceCode(priceCode) { [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.h</h3>
<pre>
#ifndef __MOVIE_H__
#define __MOVIE_H__

#include &lt;string&gt;

class Movie {
public:
  enum price_code {
    CHILDRENS = 2,
    REGULAR = 0,
    NEW_RELEASE = 1
  };

  Movie(std::string title, price_code priceCode);
  price_code getPriceCode() const;
  void setPriceCode(price_code arg);
  std::string getTitle() const;

private:
  std::string _title;
  price_code  _priceCode;

};

#endif
</pre>
<h3>Movie.cpp</h3>
<pre>
#include "Movie.h"

Movie::Movie(std::string title, price_code priceCode)
  : _title(title), _priceCode(priceCode) {
}

Movie::price_code Movie::getPriceCode() const {
  return _priceCode;
}

void Movie::setPriceCode(price_code arg) {
  _priceCode = arg;
}

std::string Movie::getTitle() const {
  return _title;
}
</pre>
<h3>Rental.h</h3>
<pre>
#ifndef __RENTAL_H__
#define __RENTAL_H__

class Movie;

class Rental {
public:
  Rental(Movie* movie, int daysRented);
  int getDaysRented() const;
  const Movie* getMovie() const;

private:
  Movie* _movie;
  int _daysRented;

};

#endif
</pre>
<h3>Rental.cpp</h3>
<pre>
#include "Rental.h"

Rental::Rental(Movie* movie, int daysRented) : _movie(movie), _daysRented(daysRented) {
}

int Rental::getDaysRented() const {
  return _daysRented;
}

const Movie* Rental::getMovie() const {
  return _movie;
}
</pre>
<h3>Customer.h</h3>
<pre>
#ifndef __CUSTOMER_H__
#define __CUSTOMER_H__

#include &lt;vector&gt;
#include &lt;string&gt;

class Rental;

class Customer {
public:
  explicit Customer(std::string name);
  void addRental(Rental* arg);
  std::string getName() const;
  std::string statement(double&amp; amount, int&amp; point) const;
  std::string statement() const;

private:
  std::string _name;
  std::vector&lt;Rental*&gt; _rentals;

};

inline std::string Customer::statement() const {
  double amount;
  int point;
  return statement(amount,point);
}

#endif
</pre>
<h3>Customer.cpp</h3>
<pre>
#include &lt;sstream&gt;

#include "Customer.h"
#include "Rental.h"
#include "Movie.h"

Customer::Customer(std::string name) : _name(name) {
}

void Customer::addRental(Rental* arg) {
  _rentals.push_back(arg);
}

std::string Customer::getName() const {
  return _name;
}

std::string Customer::statement(double&amp; totalAmount, int&amp; frequentRenterPoints) const {
  totalAmount = 0;
  frequentRenterPoints = 0;
  std::ostringstream result;
  result &lt;&lt; "Rental Record for " &lt;&lt; getName() &lt;&lt; "\n";
  for ( std::vector&lt;Rental*&gt;::const_iterator rentals = _rentals.begin();
        rentals != _rentals.end(); ++rentals ) {
    double thisAmount = 0;
    const Rental* each = *rentals;
    // 一行ごとに金額を計算
    switch ( each-&gt;getMovie()-&gt;getPriceCode() ) {
    case Movie::REGULAR :
      thisAmount += 2;
      if ( each-&gt;getDaysRented() &gt; 2 )
        thisAmount += (each-&gt;getDaysRented() - 2) * 1.5;
      break;
    case Movie::NEW_RELEASE :
      thisAmount += each-&gt;getDaysRented() * 3;
      break;
    case Movie::CHILDRENS :
      thisAmount += 1.5;
      if ( each-&gt;getDaysRented() &gt; 3 )
        thisAmount += (each-&gt;getDaysRented() - 3) * 1.5;
      break;
    }
    // レンタルポイントを加算
    frequentRenterPoints++;
    // 新作を二日以上借りた場合はボーナスポイント
    if ( (each-&gt;getMovie()-&gt;getPriceCode() == Movie::NEW_RELEASE) &amp;&amp;
         each-&gt;getDaysRented() &gt; 1 )
      frequentRenterPoints++;
    // この貸し出しに対する数値の表示
    result &lt;&lt; "\t" &lt;&lt; each-&gt;getMovie()->getTitle() &lt;&lt; "\t"
           &lt;&lt;  thisAmount &lt;&lt; "\n";
    totalAmount += thisAmount;
  }
  // フッタ部分の追加
  result &lt;&lt; "Amount owed is " &lt;&lt; totalAmount &lt;&lt; "\n"
         &lt;&lt; "You earned " &lt;&lt; frequentRenterPoints &lt;&lt; " frequent renter points";
  return result.str();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング後(C++)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cpp/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cpp/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3085</guid>
		<description><![CDATA[Movie.h #ifndef __MOVIE_H__ #define __MOVIE_H__ #include &#60;string&#62; class Price; class Movie { public: enum price_code { CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1 }; Movie(std::string title, price_code priceCode); ~Movie(); price_code getPriceCode() const; void setPriceCode(price_code arg); std::string getTitle() const; double getCharge(int daysRented) const; int getFrequentRenterPoints(int daysRented) const; private: std::string _title; Price* _price; }; #endif [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.h</h3>
<pre>
#ifndef __MOVIE_H__
#define __MOVIE_H__

#include &lt;string&gt;

class Price;

class Movie {
public:
  enum price_code {
    CHILDRENS = 2,
    REGULAR = 0,
    NEW_RELEASE = 1
  };

  Movie(std::string title, price_code priceCode);
 ~Movie();
  price_code getPriceCode() const;
  void setPriceCode(price_code arg);
  std::string getTitle() const;
  double getCharge(int daysRented) const;
  int getFrequentRenterPoints(int daysRented) const;

private:
  std::string _title;
  Price* _price;

};

#endif
</pre>
<h3>Movie.cpp</h3>
<pre>
#include &lt;stdexcept&gt;

#include "Movie.h"
#include "Price.h"

Movie::Movie(std::string title, price_code priceCode)
  : _title(title), _price(0) {
  setPriceCode(priceCode);
}

Movie::~Movie() {
  delete _price;
}

Movie::price_code Movie::getPriceCode() const {
  return _price-&gt;getPriceCode();
}

void Movie::setPriceCode(price_code arg) {
  delete _price;
  switch ( arg ) {
  case REGULAR :
    _price = new RegularPrice();
    break;
  case CHILDRENS :
    _price = new ChildrensPrice();
    break;
  case NEW_RELEASE :
    _price = new NewReleasePrice();
    break;
  default:
    throw std::runtime_error("不正な料金コード");
  }
}

std::string Movie::getTitle() const {
  return _title;
}

double Movie::getCharge(int daysRented) const {
  return _price-&gt;getCharge(daysRented);
}

int Movie::getFrequentRenterPoints(int daysRented) const {
  return _price-&gt;getFrequentRenterPoints(daysRented);
}
</pre>
<h3>Rental.h</h3>
<pre>
#ifndef __RENTAL_H__
#define __RENTAL_H__

#include "Movie.h"

class Rental {
public:
  Rental(Movie* movie, int daysRented);
  int getDaysRented() const;
  const Movie* getMovie() const;
  double getCharge() const;
  int getFrequentRenterPoints() const;

private:
  Movie* _movie;
  int _daysRented;

};

#endif
</pre>
<h3>Rental.cpp</h3>
<pre>
#include "Rental.h"

Rental::Rental(Movie* movie, int daysRented) : _movie(movie), _daysRented(daysRented) {
}

int Rental::getDaysRented() const {
  return _daysRented;
}

const Movie* Rental::getMovie() const {
  return _movie;
}

double Rental::getCharge() const {
  return _movie-&gt;getCharge(_daysRented);
}

int Rental::getFrequentRenterPoints() const {
  return _movie-&gt;getFrequentRenterPoints(_daysRented);
}
</pre>
<h3>Customer.h</h3>
<pre>
#ifndef __CUSTOMER_H__
#define __CUSTOMER_H__

#include &lt;vector&gt;
#include &lt;string&gt;

class Rental;

class Customer {
  friend class CustomerTest;

public:
  explicit Customer(std::string name);
  void addRental(Rental* arg);
  std::string getName() const;
  std::string statement() const;

private:
  double getTotalCharge() const;
  int getTotalFrequentRenterPoints() const;

  std::string _name;
  std::vector&lt;Rental*&gt; _rentals;

};

#endif
</pre>
<h3>Customer.cpp</h3>
<pre>
#include &lt;sstream&gt;

#include "Customer.h"
#include "Rental.h"
#include "Movie.h"

Customer::Customer(std::string name) : _name(name) {
}

void Customer::addRental(Rental* arg) {
  _rentals.push_back(arg);
}

std::string Customer::getName() const {
  return _name;
}

std::string Customer::statement() const {
  std::ostringstream result;
  result &lt;&lt; "Rental Record for " &lt;&lt; getName() &lt;&lt; "\n";
  for ( std::vector&lt;Rental*&gt;::const_iterator rentals = _rentals.begin();
        rentals != _rentals.end(); ++rentals ) {
    const Rental* each = *rentals;
    // この貸し出しに対する数値の表示
    result &lt;&lt; "\t" &lt;&lt; each-&gt;getMovie()-&gt;getTitle() &lt;&lt; "\t"
           &lt;&lt;  each-&gt;getCharge() &lt;&lt; "\n";
  }
  // フッタ部分の追加
  result &lt;&lt; "Amount owed is " &lt;&lt; getTotalCharge() &lt;&lt; "\n"
         &lt;&lt; "You earned " &lt;&lt; getTotalFrequentRenterPoints() &lt;&lt; " frequent renter points";
  return result.str();
}

double Customer::getTotalCharge() const {
  double result = 0;
  for ( std::vector&lt;Rental*&gt;::const_iterator rentals = _rentals.begin();
        rentals != _rentals.end(); ++rentals ) {
    const Rental* each = *rentals;
    result += each-&gt;getCharge();
  }
  return result;
}

int Customer::getTotalFrequentRenterPoints() const {
  int result = 0;
  for ( std::vector&lt;Rental*&gt;::const_iterator rentals = _rentals.begin();
        rentals != _rentals.end(); ++rentals ) {
    const Rental* each = *rentals;
    result += each-&gt;getFrequentRenterPoints();
  }
  return result;
}
</pre>
<h3>Price.h</h3>
<pre>
#ifndef __PRICE_H__
#define __PRICE_H__

#include "Movie.h"

class Price {
public:
  virtual ~Price() {}
  virtual Movie::price_code getPriceCode() const =0;
  virtual double getCharge(int daysRented) const =0;
  virtual int getFrequentRenterPoints(int daysRented) const;
};

class ChildrensPrice : public Price {
public:
  virtual Movie::price_code getPriceCode() const;
  virtual double getCharge(int daysRented) const;
};

class NewReleasePrice : public Price {
public:
  virtual Movie::price_code getPriceCode() const;
  virtual double getCharge(int daysRented) const;
  virtual int getFrequentRenterPoints(int daysRented) const;
};

class RegularPrice : public Price {
public:
  virtual Movie::price_code getPriceCode() const;
  virtual double getCharge(int daysRented) const;
};

#endif
</pre>
<h3>Price.cpp</h3>
<pre>
#include "Price.h"

// Price

int Price::getFrequentRenterPoints(int daysRented) const {
  return 1;
}

// ChildrensPrice

Movie::price_code ChildrensPrice::getPriceCode() const {
  return Movie::CHILDRENS;
}

double ChildrensPrice::getCharge(int daysRented) const {
  double result = 1.5;
  if ( daysRented &gt; 3 )
    result += (daysRented - 3) * 1.5;
  return result;
}

// NewReleasePrice

Movie::price_code NewReleasePrice::getPriceCode() const {
  return Movie::NEW_RELEASE;
}

double NewReleasePrice::getCharge(int daysRented) const {
  return daysRented * 3;
}

int NewReleasePrice::getFrequentRenterPoints(int daysRented) const {
  return  ( daysRented &gt; 1 ) ? 2 : 1;
}

// RegularPrice

Movie::price_code RegularPrice::getPriceCode() const {
  return Movie::REGULAR;
}

double RegularPrice::getCharge(int daysRented) const {
  double result = 2;
  if ( daysRented &gt; 2 )
    result += (daysRented - 2) * 1.5;
  return result;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cpp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング前(C#)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cs/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cs/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3091</guid>
		<description><![CDATA[Movie.cs // Movie.cs namespace csharp_before { public class Movie { public enum enumPriceCode { CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1 }; private string _title; private enumPriceCode _priceCode; public Movie(string title, enumPriceCode priceCode) { _title = title; _priceCode = priceCode; } public enumPriceCode PriceCode { get { return _priceCode; } set { _priceCode [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.cs</h3>
<pre>
// Movie.cs

namespace csharp_before {

  public class Movie {

    public enum enumPriceCode {
      CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1
    };

    private string _title;
    private enumPriceCode _priceCode;

    public Movie(string title, enumPriceCode priceCode) {
      _title = title;
      _priceCode = priceCode;
    }

    public enumPriceCode PriceCode {
      get { return _priceCode; }
      set { _priceCode = value; }
    }

    public string Title {
      get { return _title; }
    }

  }

}
</pre>
<h3>Rental.cs</h3>
<pre>
// Rental.cs

namespace csharp_before {

  public class Rental {

    private Movie _movie;
    private int _daysRented;

    public Rental(Movie movie, int daysRented) {
      _movie = movie;
      _daysRented = daysRented;
    }

    public int DaysRented {
      get { return _daysRented; }
    }

    public Movie Movie {
      get { return _movie; }
    }

  }

}
</pre>
<h3>Customer.cs</h3>
<pre>
// Customer.cs

namespace csharp_before {

  public class Customer {

    private string _name;
    private System.Collections.ArrayList _rentals;

    public Customer(string name) {
      _rentals = new System.Collections.ArrayList();
      _name = name;
    }

    public void addRental(Rental arg) {
      _rentals.Add(arg);
    }

    public string Name {
      get { return _name; }
    }

    public string statement() {
      double totalAmount = 0;
      int frequentRenterPoints = 0;
      string result = "Rental Record for " + Name + "\n";
      foreach ( Rental each in _rentals ) {
        double thisAmount = 0;
        // 一行ごとに金額を計算
        switch ( each.Movie.PriceCode ) {
        case Movie.enumPriceCode.REGULAR :
          thisAmount += 2;
          if ( each.DaysRented &gt; 2 )
            thisAmount += (each.DaysRented - 2) * 1.5;
          break;
        case Movie.enumPriceCode.NEW_RELEASE :
          thisAmount += each.DaysRented * 3;
          break;
        case Movie.enumPriceCode.CHILDRENS :
          thisAmount += 1.5;
          if ( each.DaysRented &gt; 3 )
            thisAmount += (each.DaysRented - 3) * 1.5;
          break;
        }
        // レンタルポイントを加算
        frequentRenterPoints++;
        // 新作を二日以上借りた場合はボーナスポイント
        if ( (each.Movie.PriceCode == Movie.enumPriceCode.NEW_RELEASE) &amp;&amp;
              each.DaysRented &gt; 1 )
          frequentRenterPoints++;
        // この貸し出しに対する数値の表示
        result += "\t" + each.Movie.Title + "\t" +
                  thisAmount.ToString() + "\n";
        totalAmount += thisAmount;
      }
      // フッタ部分の追加
      result += "Amount owed is " + totalAmount.ToString() + "\n";
      result += "You earned " + frequentRenterPoints.ToString() +
                " frequent renter points";
      return result;
    }
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-cs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング後(C#)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cs/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cs/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3095</guid>
		<description><![CDATA[Movie.cs // Movie.cs namespace csharp_after { public class Movie { public enum enumPriceCode { CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1 } private string _title; private Price _price; public Movie(string title, enumPriceCode priceCode) { _title = title; PriceCode = priceCode; } public enumPriceCode PriceCode { get { return _price.PriceCode; } set { switch [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.cs</h3>
<pre>
// Movie.cs

namespace csharp_after {

  public class Movie {

    public enum enumPriceCode {
      CHILDRENS = 2, REGULAR = 0, NEW_RELEASE = 1
    }

    private string _title;
    private Price  _price;

    public Movie(string title, enumPriceCode priceCode) {
      _title = title;
      PriceCode = priceCode;
    }

    public enumPriceCode PriceCode {
      get { return _price.PriceCode; }
      set {
        switch ( value ) {
        case Movie.enumPriceCode.REGULAR :
          _price = new RegularPrice();
          break;
        case Movie.enumPriceCode.CHILDRENS :
          _price = new ChildrensPrice();
          break;
        case Movie.enumPriceCode.NEW_RELEASE :
          _price = new NewReleasePrice();
          break;
        default:
          throw new System.Exception("不正な料金コード");
        }
      }
    }

     public string Title {
       get { return _title; }
     }

     public double getCharge(int daysRented) {
       return _price.getCharge(daysRented);
     }

     public int getFrequentRenterPoints(int daysRented) {
       return _price.getFrequentRenterPoints(daysRented);
     }

  }
}
</pre>
<h3>Rental.cs</h3>
<pre>
// Rental.cs

namespace csharp_after {

  public class Rental {

    private Movie _movie;
    private int _daysRented;

    public Rental(Movie movie, int daysRented) {
      _movie = movie;
      _daysRented = daysRented;
    }

    public int DaysRented {
      get { return _daysRented; }
    }

    public Movie Movie {
      get { return _movie; }
    }

    public double Charge {
      get { return _movie.getCharge(_daysRented); }
    }

    public int FrequentRenterPoints {
      get { return _movie.getFrequentRenterPoints(_daysRented); }
    }

  }

}
</pre>
<h3>Customer.cs</h3>
<pre>
// Customer.cs

namespace csharp_after {

  public class Customer {

    private string _name;
    private System.Collections.ArrayList _rentals;

    public Customer(string name) {
      _rentals = new System.Collections.ArrayList();
      _name = name;
    }

    public void addRental(Rental arg) {
      _rentals.Add(arg);
    }

    public string Name {
      get { return _name; }
    }

    public string statement() {
      string result = "Rental Record for " + Name + "\n";
      foreach ( Rental each in _rentals ) {
        // この貸し出しに対する数値の表示
        result += "\t" + each.Movie.Title + "\t" +
                  each.Charge.ToString() + "\n";
      }
      // フッタ部分の追加
      result += "Amount owed is " + TotalCharge.ToString() + "\n";
      result += "You earned " + TotalFrequentRenterPoints.ToString() +
                " frequent renter points";
      return result;
    }

    double TotalCharge {
      get {
        double result = 0;
        foreach ( Rental each in _rentals ) {
          result += each.Charge;
        }
        return result;
      }
    }

    double TotalFrequentRenterPoints {
      get {
        int result = 0;
        foreach ( Rental each in _rentals ) {
          result += each.FrequentRenterPoints;
        }
        return result;
      }
    }
  }

}
</pre>
<h3>Price.cs</h3>
<pre>
// Price.cs

namespace csharp_after {

  public abstract class Price {

    abstract public Movie.enumPriceCode PriceCode { get; }
    abstract public double getCharge(int daysRented);
    virtual public int getFrequentRenterPoints(int daysRented) { return 1; }
  }

  class ChildrensPrice : Price {

    public override Movie.enumPriceCode PriceCode {
      get { return Movie.enumPriceCode.CHILDRENS; }
    }

    public override double getCharge(int daysRented) {
      double result = 1.5;
      if ( daysRented &gt; 3 )
        result += (daysRented -3) * 1.5;
      return result;
    }

  }

  class NewReleasePrice : Price {

    public override Movie.enumPriceCode PriceCode {
      get { return Movie.enumPriceCode.NEW_RELEASE; }
    }

    public override double getCharge(int daysRented) {
      return daysRented * 3;
    }

    public override int getFrequentRenterPoints(int daysRented) {
      return ( daysRented &gt; 1 ) ? 2 : 1;
    }

  }

  class RegularPrice : Price {

    public override Movie.enumPriceCode PriceCode {
      get { return Movie.enumPriceCode.REGULAR; }
    }

    public override double getCharge(int daysRented) {
      double result = 2;
      if ( daysRented &gt; 2 )
        result += (daysRented -2) * 1.5;
      return result;
    }
  }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-cs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング前(VB.NET)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-vb/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-vb/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3101</guid>
		<description><![CDATA[Movie.vb Namespace visualbasic_before Public Class Movie Public Enum enumPriceCode CHILDRENS = 2 REGULAR = 0 NEW_RELEASE = 1 End Enum Private _title As String Private _priceCode As enumPriceCode Public Sub New(ByVal title As String, ByVal priceCode As enumPriceCode) _title = title _priceCode = priceCode End Sub Public ReadOnly Property PriceCode() As enumPriceCode Get PriceCode = [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.vb</h3>
<pre>
Namespace visualbasic_before

  Public Class Movie

    Public Enum enumPriceCode
      CHILDRENS = 2
      REGULAR = 0
      NEW_RELEASE = 1
    End Enum

    Private _title As String
    Private _priceCode As enumPriceCode

    Public Sub New(ByVal title As String, ByVal priceCode As enumPriceCode)
      _title = title
      _priceCode = priceCode
    End Sub

    Public ReadOnly Property PriceCode() As enumPriceCode
      Get
        PriceCode = _priceCode
      End Get
    End Property

    Public ReadOnly Property Title() As String
      Get
        Title = _title
      End Get
    End Property

  End Class

End Namespace
</pre>
<h3>Rental.vb</h3>
<pre>
Namespace visualbasic_before

  Public Class Rental

    Private _movie As Movie
    Private _daysRented As Integer

    Public Sub New(ByVal movie As Movie, ByVal daysRented As Integer)
      _movie = movie
      _daysRented = daysRented
    End Sub

    Public ReadOnly Property DaysRented() As Integer
      Get
        DaysRented = _daysRented
      End Get
    End Property

    Public ReadOnly Property Movie() As Movie
      Get
        Movie = _movie
      End Get
    End Property

  End Class

End Namespace
</pre>
<h3>Customer.vb</h3>
<pre>
Namespace visualbasic_before

  Public Class Customer

    Private _name As String
    Private _rentals As System.Collections.ArrayList

    Public Sub New(ByVal name As String)
      _rentals = New System.Collections.ArrayList()
      _name = name
    End Sub

    Public Sub addRental(ByVal arg As Rental)
      _rentals.Add(arg)
    End Sub

    Public ReadOnly Property Name() As String
      Get
        Name = _name
      End Get
    End Property

    Public Function statement() As String
      Dim totalAmount As Double
      Dim frequentRenterPoints As Integer
      Dim result As String
      Dim item As Rental
      totalAmount = 0
      frequentRenterPoints = 0
      result = "Rental Record for " + Name + Chr(13)
      For Each item In _rentals
        Dim thisAmount As Double
        thisAmount = 0
        ' 一行ごとに金額を計算
        Select Case item.Movie.PriceCode
        Case Movie.enumPriceCode.REGULAR
          thisAmount += 2
          If item.DaysRented &gt; 2 Then
            thisAmount += (item.DaysRented - 2) * 1.5
          End If
        Case Movie.enumPriceCode.NEW_RELEASE
          thisAmount += item.DaysRented * 3
        Case Movie.enumPriceCode.CHILDRENS
          thisAmount += 1.5
          If item.DaysRented &gt; 3 Then
            thisAmount += (item.DaysRented - 3) * 1.5
          End If
        End Select
        ' レンタルポイントを加算
        frequentRenterPoints = frequentRenterPoints + 1
        ' 新作を二日以上借りた場合はボーナスポイント
        If item.Movie.PriceCode = Movie.enumPriceCode.NEW_RELEASE And item.DaysRented &gt; 1 Then
          frequentRenterPoints = frequentRenterPoints + 1
        End If
        ' この貸し出しに対する数値の表示
        result += Chr(9) + item.Movie.Title + Chr(9) + thisAmount.ToString() + Chr(13) + Chr(10)
        totalAmount += thisAmount
      Next
      ' フッタ部分の追加
      result += "Amount owed is " + totalAmount.ToString() + Chr(13) + Chr(10)
      result += "You earned " + frequentRenterPoints.ToString() + " frequent renter points"

      statement = result
    End Function
End Class

End Namespace
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-before-vb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++/C#/VB.NETによる リファクタリング &#8211; リファクタリング後(VB.NET)</title>
		<link>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-vb/</link>
		<comments>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-vb/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 06:27:20 +0000</pubDate>
		<dc:creator>cpptechdoc</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://www.s34.co.jp/?p=3105</guid>
		<description><![CDATA[Movie.vb Namespace visualbasic_after Public Class Movie Public Enum enumPriceCode CHILDRENS = 2 REGULAR = 0 NEW_RELEASE = 1 End Enum Private _title As String Private _price As Price Public Sub New(ByVal title As String, ByVal code As enumPriceCode) _title = title PriceCode = code End Sub Public Property PriceCode() As enumPriceCode Get PriceCode = _price.PriceCode [...]]]></description>
			<content:encoded><![CDATA[<h3>Movie.vb</h3>
<pre>
Namespace visualbasic_after

  Public Class Movie

    Public Enum enumPriceCode
      CHILDRENS = 2
      REGULAR = 0
      NEW_RELEASE = 1
    End Enum

    Private _title As String
    Private _price As Price

    Public Sub New(ByVal title As String, ByVal code As enumPriceCode)
      _title = title
      PriceCode = code
    End Sub

    Public Property PriceCode() As enumPriceCode
      Get
        PriceCode = _price.PriceCode
      End Get
      Set(ByVal Value As enumPriceCode)
        Select Case Value
        Case Movie.enumPriceCode.REGULAR
          _price = New RegularPrice()
        Case Movie.enumPriceCode.CHILDRENS
          _price = New ChildrensPrice()
        Case Movie.enumPriceCode.NEW_RELEASE
          _price = New NewReleasePrice()
        Case Else
          Throw New System.Exception("不正な料金コード")
        End Select
      End Set
    End Property

    Public ReadOnly Property Title() As String
      Get
        Title = _title
      End Get
    End Property

    Public Function getCharge(ByVal daysRented As Integer) As Double
       getCharge = _price.getCharge(daysRented)
    End Function

    Public Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
       getFrequentRenterPoints = _price.getFrequentRenterPoints(daysRented)
    End Function

  End Class

End Namespace
</pre>
<h3>Rental.vb</h3>
<pre>
Namespace visualbasic_after

  Public Class Rental

    Private _movie As Movie
    Private _daysRented As Integer

    Public Sub New(ByVal movie As Movie, ByVal daysRented As Integer)
      _movie = movie
      _daysRented = daysRented
    End Sub

    Public ReadOnly Property DaysRented() As Integer
      Get
        DaysRented = _daysRented
      End Get
    End Property

    Public ReadOnly Property TheMovie() As Movie
      Get
        TheMovie = _movie
      End Get
    End Property

    Public ReadOnly Property Charge() As Double
      Get
        Charge = _movie.getCharge(_daysRented)
      End Get
    End Property

    Public ReadOnly Property FrequentRenterPoints() As Integer
      Get
        FrequentRenterPoints = _movie.getFrequentRenterPoints(_daysRented)
      End Get
    End Property

  End Class

End Namespace
</pre>
<h3>Customer.vb</h3>
<pre>
Namespace visualbasic_after

  Public Class Customer

    Private _name As String
    Private _rentals As System.Collections.ArrayList

    Public Sub New(ByVal name As String)
      _rentals = New System.Collections.ArrayList()
      _name = name
    End Sub

    Public Sub addRental(ByVal arg As Rental)
      _rentals.Add(arg)
    End Sub

    Public ReadOnly Property Name() As String
      Get
        Name = _name
      End Get
    End Property

    Public Function statement() As String
      Dim result As String = "Rental Record for " + Name + Chr(13) + Chr(10)
      Dim item As Rental
      For Each item In _rentals
        ' この貸し出しに対する数値の表示
        result += Chr(9) + item.TheMovie.Title + Chr(9) + item.Charge.ToString() + Chr(13) + Chr(10)
      Next
      ' フッタ部分の追加
      result += "Amount owed is " + TotalCharge.ToString() + Chr(13) + Chr(10)
      result += "You earned " + TotalFrequentRenterPoints.ToString() + " frequent renter points"
      statement = result
    End Function

    Public ReadOnly Property TotalCharge() As Double
      Get
        Dim result As Double = 0
        Dim item As Rental
        For Each item In _rentals
          result += item.Charge
        Next
        TotalCharge = result
      End Get
    End Property

    Public ReadOnly Property TotalFrequentRenterPoints() As Double
      Get
        Dim result As Integer = 0
        Dim item As Rental
        For Each item In _rentals
          result += item.FrequentRenterPoints
        Next
        TotalFrequentRenterPoints = result
      End Get
    End Property
  End Class

End Namespace
</pre>
<h3>Price.vb</h3>
<pre>
Namespace visualbasic_after

  Public MustInherit Class Price

    Public ReadOnly Property PriceCode() As Movie.enumPriceCode
      Get
        PriceCode = getPriceCode()
      End Get
    End Property

    Public MustOverride Function getCharge(ByVal daysRented As Integer) As Double

    Public Overridable Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
      getFrequentRenterPoints = 1
    End Function

    Public Overridable Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.REGULAR
    End Function

  End Class

  Class ChildrensPrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.CHILDRENS
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      Dim result As Double = 1.5
      If daysRented &gt; 3 Then
        result += (daysRented - 3) * 1.5
      End If
      getCharge = result
    End Function

  End Class

  Public Class NewReleasePrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.NEW_RELEASE
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      getCharge = daysRented * 3
    End Function

    Public Overrides Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
      If daysRented &gt; 1 Then
        getFrequentRenterPoints = 2
      Else
        getFrequentRenterPoints = 1
      End If
    End Function

  End Class

  Class RegularPrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.REGULAR
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      Dim result As Double = 2
      If daysRented &gt; 2 Then
        result += (daysRented - 2) * 1.5
      End If
      getCharge = result
    End Function

  End Class

End Namespace
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.s34.co.jp/cpptechdoc/misc/refactoring-after-vb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

