Chain of Responsibility Pattern

Call out objects in sequence </ font>

Design Pattarm MENU

Check with the following class structure

class Explanation
abstract
chain.class
Define chain function
The field next stores the next object instance
set():Set the following objects
doChain()Define
chai1.class~chain3.class Implement chain
user(Main.class) Operation check
  • User Includes the meaning that other developers use this pattern

abstract_class_chain


abstract class chain{
   String name = this.getClass().getName();
   chain  next;             //Store the following objects

   chain set(chain next){
         this.next=next;
         return this.next;  //Returns the next object
   }

   abstract void doChain();
   void  print(){
         System.out.println(name);}
}

chain1.class


class chain1 extends chain{
      void   doChain(){
             print();
             if(next != null){next.doChain();}  //DoChain for the next object()Run
      }
}

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();
}

Recommended Posts

Chain of Responsibility Pattern
Chain Of Responsibility pattern
Design pattern ~ Chain of Responsibility ~
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Which man do you like? Identifying love targets using the Chain of Responsibility pattern
Application example of design pattern (No. 1)