Optimizing the CH01 of the responsibility chain to solve the problem of too high coupling degree
Remember the previous article we used an abstract class, and then the subclass to inherit the method to achieve the level of bridging, and found that the coupling is too high.
In order to solve this problem.
We use interfaces for abstraction, and then use an "intermediary" to automatically determine the amount of money to be applied.
This is a web-like filter. Let's look a look!
First, we create an interface to abstract the method of approving fees
Public interface Abstracthandler { //abstract The method of approving funds, there are two parameters ///The first parameter is: application funding ///The second parameter is: the class that handles the link, that is, the mediation void Execute (int money,handlerchain chain);}
Two, create three levels, respectively implement Abstracthandler interface
A) money<=200
Public class Implements Abstracthandler { @Override publicvoid execute (int money , Handlerchain chain) { if(money<=200) {// to determine funding System.out.println ("A review passed ......."); } Else { // If the approval permission is insufficient, go to the top level leader application, chain.dofilter ( money ); }}}
b) money<=500
public class Handlerb implements Abstracthandler { @Override public void execute (int money,handlerchain chain) { if (money<=500) { System.out.println ("b audit can be passed ..."); } else{
Chain.dofilter (Money);}}}
c) money<=1000
Public class Implements Abstracthandler { @Override publicvoid execute (int money , Handlerchain chain) { if(money<=1000) { System.out.println ("C audit passed ...") ); } Else { chain.dofilter (money); }}}
Third, the most important is the distribution intermediary, that is, link processing
Public classHandlerchain {//used to store all handler instances generics are abstract interfaces PrivateList<abstracthandler> list=NewArraylist<abstracthandler>(); //defines an iterator used to iterate the list collection PrivateIterator<abstracthandler>it; /*** Initialize the chain of responsibility using the constructor method * Add subclass instantiation to list*/ PublicHandlerchain () {List.add (NewHandlera ()); List.add (NewHandlerb ()); List.add (NewHandlerc ()); It=List.iterator (); } /*** To judge the permission to approve funds in list *@param Money*/ Public voidDoFilter (intMoney ) { if(It.hasnext ()) {//If there is a next one, execute//Use the method that is instantiated in the list, where there is a judgment in the subclass method//If the approval is successful this output, if the approval funds exceed the permissions, this time the next list of the following sub-class method is executed;It.next (). Execute (Money, This); } }}
Third, with this intermediary, our main method can reduce the excessive coupling
Public class Main { publicstaticvoid main (string[] args) { // instantiate an intermediary and apply for funds directly within the intermediary New Handlerchain (); Chain.dofilter (); }}
Output result: A audit passed ......
The responsibility chain model in Java (CH02)