凪瀬 Blog
Programming SHOT BAR

目次

Blog 利用状況
  • 投稿数 - 260
  • 記事 - 0
  • コメント - 46506
  • トラックバック - 192
ニュース
広告
  • Java開発者募集中
  • 経歴不問
  • 腕に自信のある方
  • 富山市内
  • (株)凪瀬アーキテクツ
アクセサリ
  • あわせて読みたい
凪瀬悠輝(なぎせ ゆうき)
  • Java技術者
  • お茶好き。カクテル好き。
  • 所属は(株)凪瀬アーキテクツ
  • Twitter:@nagise

書庫

日記カテゴリ

 

今回はStateパターン編です。 Stateパターンはオブジェクト指向のポリモフィズムで動きを切り替えるという点で前回の Strategyパターンと同じです。

ただし、Stateパターンはそのインスタンス自身が「状態」を表現するという点で異なります。

電卓のUIのStateを作る

まず、Stateの使いどころですが、ここでは以前にわんくま同盟で取り上げられた 電卓を例にしてみましょう。

επιστημηさんのところに 電卓を状態遷移で捉える 話題がでていたのですが、この状態遷移というのがStateパターンの使いどころになります。

電卓基本動作の状態遷移表
初期状態 数値入力状態 数式作成状態 計算結果表示状態
数字入力 なにかする
数値入力状態
なにかする
数値入力状態
なにかする
数値入力状態
なにかする
数値入力状態
演算子入力 なにかする
初期状態
なにかする
数式作成状態
なにかする
数式作成状態
なにかする
数式作成状態
'='入力 なにかする
初期状態
なにかする
計算結果表示状態
なにかする
計算結果表示状態
なにかする
計算結果表示状態
'C'入力 なにかする
初期状態
なにかする
初期状態
なにかする
初期状態
なにかする
初期状態

この、「初期状態」「数値入力状態」「数式作成状態」「計算結果表示状態」がStateパターンでは オブジェクトのインスタンスで表現されます。 そして、そのクラスには「数字入力」「演算子入力」「'='入力」「'C'入力」の4つのメソッドが定義されます。

Java /** @param <R> Stateの具象型 */
public interface State<R extends State> {
  /** 数値の入力
   @return 遷移するState. 以下同じ */
  R onInputNumber(int num);
  /** 演算子の入力 */
  R onInputOperation(Operation ope);
  /** '='の入力 */
  R onInputEquale();
  /** 'C'の入力 */
  R onInputClear();
}

C# public interface State<R> where R : State {
    /// <summary>数値の入力</summary>
    /// <returns>遷移するState. 以下同じ</returns>
    R onInputNumber(int num);
    /// <summary>演算子の入力</summary>
    R onInputOperation(Operation ope);
    /// <summary>'='の入力</summary>
    R onInputEquale();
    /// <summary>'C'の入力</summary>
    R onInputClear();
}

より抽象的にすることで再利用性を持たせる

通常のStateパターンだと、この時点で具象型を用いるのでジェネリクスの出番はないところですが、 電卓と同じユーザインターフェースを持つ別のアプリケーションを作れるようにジェネリクスで 抽象化しておきます。

ご覧の通り、各メソッドは処理をした上で次に遷移するべきStateを返すようになっています。 このときに自信の型を返したいので自己言及するジェネリクス( Java版C#版) で取り上げた手法を用います。

これらの実装を作る際にはジェネリクス(サンプルソースのR)を具象化するわけですが、この型は 電卓用であれば電卓用、同一UIを用いた別の何かであればそれ用に独自に拡張します。 電卓用のStateと別の何かのStateは混同して利用されることはありません

つまり、このUIを共通に提供するような再利用性の高いフレームワークを設計する場合でないと、 このようなStateパターンの多様な拡張性を持たせると言う設計は活きてこないのです。

逆に、このようなジェネリクスを活用することで、今まで再利用が不可能だと思っていたパーツを 再利用可能な形でライブラリとすることができます。

補足

Javaの場合はさらに Strategyのインスタンスをenumで扱う で取り上げたように、Stateのインスタンスをenum化することができます。

投稿日時 : 2008年6月5日 21:17
コメント
  • # Hey! I know this is somewhat off-topic but I had to ask. Does managing a well-established website such as yours require a lot of work? I'm brand new to blogging however I do write in my diary everyday. I'd like to start a blog so I can easily share my o
    Hey! I know this is somewhat off-topic but I had t
    Posted @ 2021/09/02 22:04
    Hey! I know this is somewhat off-topic but I had to ask.

    Does managing a well-established website such as yours require
    a lot of work? I'm brand new to blogging however I do write in my diary everyday.
    I'd like to start a blog so I can easily share my own experience and
    thoughts online. Please let me know if you have any kind of recommendations or tips for
    brand new aspiring blog owners. Thankyou!
  • # Hi! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do yo
    Hi! This is kind of off topic but I need some guid
    Posted @ 2021/09/05 23:11
    Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I'm not very techincal but I
    can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start.

    Do you have any ideas or suggestions? Many thanks
  • # Hi! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do yo
    Hi! This is kind of off topic but I need some guid
    Posted @ 2021/09/05 23:12
    Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I'm not very techincal but I
    can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start.

    Do you have any ideas or suggestions? Many thanks
  • # Hi! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do yo
    Hi! This is kind of off topic but I need some guid
    Posted @ 2021/09/05 23:13
    Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I'm not very techincal but I
    can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start.

    Do you have any ideas or suggestions? Many thanks
  • # Hi! This is kind of off topic but I need some guidance from an established blog. Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do yo
    Hi! This is kind of off topic but I need some guid
    Posted @ 2021/09/05 23:14
    Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I'm not very techincal but I
    can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start.

    Do you have any ideas or suggestions? Many thanks
  • # tadalafil cheep
    Advatly
    Posted @ 2021/09/17 5:52
    http://buyplaquenilcv.com/ - hydroxychloroquine for rheumatoid arthritis
タイトル  
名前  
Url
コメント