0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Chain of Responsibility Pattern

Last updated at Posted at 2020-11-14

オブジェクトを順次よび出す

Design Pattarm MENU

以下のクラス構成で確認します

クラス 説明
abstract
chain.class
chain機能を定義
フィールド next は次のオブジェクトインスタンスを格納
set():次のオブジェクトをセット
doChain()を定義
chai1.class~chain3.class chainを実装
user(Main.class) 動作確認

*user 他の開発者がこのパターンを利用する、という意味合いを含みます

abstract_class_chain
abstract class chain{
   String name = this.getClass().getName();
   chain  next;             // 次のオブジェクトを格納

   chain set(chain next){
         this.next=next;
         return this.next;  // 次のオブジェクトを返す 
   }

   abstract void doChain();
   void  print(){
         System.out.println(name);}
}
chain1.class
class chain1 extends chain{
      void   doChain(){
             print();
             if(next != null){next.doChain();}  // 次のオブジェクトのdoChain()を実行
      }
}
chain2.class
class chain2 extends chain{
      void   doChain(){
             print();
             if(next != null){next.doChain();} 
      }
}
chain3.class
class chain3 extends chain{
      void   doChain(){
             print();
             if(next != null){next.doChain();}
      }
}
user(Main.class)
public static void main(String[] args){
   chain1 ch1 = new chain1();
   chain2 ch2 = new chain2();
   chain3 ch3 = new chain3();
   ch1.set(ch2).set(ch3);
   ch1.doChain();
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?