擬似コードによる例とは? わかりやすく解説

Weblio 辞書 > 辞書・百科事典 > ウィキペディア小見出し辞書 > 擬似コードによる例の意味・解説 

擬似コードによる例

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2017/04/13 17:26 UTC 版)

State パターン」の記事における「擬似コードによる例」の解説

ドローソフトを例に取る。このプログラム任意の時点においてさまざまなツールのうちの1つとして振る舞うマウスカーソルを持つ。複数のカーソルオブジェクトを切り替える代わりにカーソルは現在使用されているツールを表す内部的な状態を保持する。(例えばマウスクリックの結果としてツール依存するメソッド呼ばれると、メソッド呼び出しカーソルの状態へと渡される。 各ツール1つの状態に対応している共有される抽象状態クラスはAbstractToolである。 class AbstractTool is function moveTo(point) is input: the location point the mouse moved to (this function must be implemented by subclasses) function mouseDown(point) is input: the location point the mouse is at (this function must be implemented by subclasses) function mouseUp(point) is input: the location point the mouse is at (this function must be implemented by subclasses) この定義により、各ツールマウスカーソル移動およびクリックドラッグ開始と終了を扱わなければならない。 その基底クラス用い単純なペン範囲選択の各ツール定義すると以下のようになるsubclass PenTool of AbstractTool is last_mouse_position := invalid mouse_button := up function moveTo(point) is input: the location point the mouse moved to if mouse_button = down (draw a line from the last_mouse_position to point) last_mouse_position := point function mouseDown(point) is input: the location point the mouse is at mouse_button := down last_mouse_position := point function mouseUp(point) is input: the location point the mouse is at mouse_button := up subclass SelectionTool of AbstractTool is selection_start := invalid mouse_button := up function moveTo(point) is input: the location point the mouse moved to if mouse_button = down (select the rectangle between selection_start and point) function mouseDown(point) is input: the location point the mouse is at mouse_button := down selection_start := point function mouseUp(point) is input: the location point the mouse is at mouse_button := up この例では、コンテキストのためのクラスCursor呼ばれている。抽象状態クラス(この場合AbstractTool)で名付けられメソッド群はコンテキストにおいても実装されている。コンテキストクラスではこれらのメソッドは、current_toolにより表される現在の状態の、対応するメソッド呼び出す。 class Cursor is current_tool := new PenTool function moveTo(point) is input: the location point the mouse moved to current_tool.moveTo(point) function mouseDown(point) is input: the location point the mouse is at current_tool.mouseDown(point) function mouseUp(point) is input: the location point the mouse is at current_tool.mouseUp(point) function usePenTool() is current_tool := new PenTool function useSelectionTool() is current_tool := new SelectionTool Cursorオブジェクトが、適切なメソッド呼び出しアクティブとなっているツールへと渡すことにより、異な時点においてPenToolとSelectionToolのどちらとしてでも振舞えのである。これがStateパターン本質である。この場合では、PenCursorクラスとSelectCursorクラス作ることで状態とオブジェクト結合することも可能であり、単なる継承へと単純化できたであろうが、実践においては新しツール選択される毎に新しオブジェクトへとコピーするにはコストがかかりすぎたりエレガントでなかったりするようなデータCursor保持していることもあるであろう

※この「擬似コードによる例」の解説は、「State パターン」の解説の一部です。
「擬似コードによる例」を含む「State パターン」の記事については、「State パターン」の概要を参照ください。

ウィキペディア小見出し辞書の「擬似コードによる例」の項目はプログラムで機械的に意味や本文を生成しているため、不適切な項目が含まれていることもあります。ご了承くださいませ。 お問い合わせ



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「擬似コードによる例」の関連用語

擬似コードによる例のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



擬似コードによる例のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
ウィキペディアウィキペディア
Text is available under GNU Free Documentation License (GFDL).
Weblio辞書に掲載されている「ウィキペディア小見出し辞書」の記事は、WikipediaのState パターン (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。

©2025 GRAS Group, Inc.RSS