1.12 (Design mode) chain of responsibility pattern

Chain Chain of Responsibility pattern creates a receiver object requests, such decoupling the requester of the recipient,

Further requestor without concern for the specific implementation of the recipient, the recipient simply passes the request to chain to.

 

The following give an example of a loaded class, the class loading mechanism employed parent delegate, to the first class uppermost BootstrapClassLoader,

If the load is BootstrapClassLoader can be loaded, if not loaded, then passed to the next stage ExtensionClassLoader. Loading,

If loading is to load, on the contrary passed on to the next stage loader ApplicationClassLoader.

And so on, until finally all class loader can not be loaded, the class loader throws an exception.

 

 

First we create an abstract class ClassLoader subsequent BootstrapClassLoader, ExtensionClassLoader inherit the abstract class, etc.

public  abstract  class ClassLoader {
     public  static  int bootstrp =. 1 ;
     public  static  int EXTENSION = 2 ;
     public  static  int the APPLICATION =. 3 ; 
    
    protected  int priority; // current priority class loader 
    
    // next stage loader 
    protected ClassLoader nextClassLoader; 
    
    public  void setNextClassLoader (ClassLoader nextClassLoader) {
         the this .nextClassLoader = nextClassLoader; 
    } 
    
    public  voidrunLoader (String className, int priority) { 
        System.out.println ( "loader of the current priority:" + the this .priority + "- the current priority class:" + priority);
         IF ( the this .priority == priority) { // the current class can be loaded is installed 
            Loader (className); 
        } the else { // the current class loader can not load the current class loader passed to the next stage 
            IF (nextClassLoader =! null ) { // If the a class loader is present is passed to the next stage, otherwise an exception is thrown.
                // System.out.println ( "-" + this.priority + ":" + priority); 
                nextClassLoader.runLoader (className, priority); 
            } the else{ // current loading does not load, and the subsequent load is null class loader is thrown failed 
                System.out.println ( "Class Loader the Fail" ); 
            } 
        } 
    } 
    public  abstract  void Loader (String className); 
}

Setting a priority class has priority, transmitted to the first class BootstrapClassLoader, if the priority class equal to the priority of the loader, loader is invoked loaded.

Otherwise passing to the next stage by loading nextClassLoader.runLoader (className, priority), and finally under a loader not an exception is thrown.

 

ClassLoader subclass needs to do two things at initialization:

1, the priority specified by the current class loader construction method,

2, the next specifies the current class loader class loader by setNextClassLoader parent class, load chain configuration.

 

BootstrapClassLoader 

public class BootstrapClassLoader extends ClassLoader{
    
    public BootstrapClassLoader(int priority) {
        this.priority = priority;
    }
    
    @Override
    public void loader(String className) {
        // TODO Auto-generated method stub
        System.out.println("BootstrapClassLoader:" + className);
    }
    
}

 

 

ExtensionClassLoader

public class ExtensionClassLoader extends ClassLoader{
    
    public ExtensionClassLoader(int priority) {
        this.priority = priority;
    }
    
    @Override
    public void loader(String className) {
        // TODO Auto-generated method stub
        System.out.println("ExtensionClassLoader:" + className);
    }
    
}

 

 

ApplicationClassLoader

public class ApplicationClassLoader extends ClassLoader{
    
    public ApplicationClassLoader(int priority) {
        this.priority = priority;
    }
    
    @Override
    public void loader(String className) {
        // TODO Auto-generated method stub
        System.out.println("ApplicationClassLoader:" + className);
    }
    
}

 

 

Main

public  class the Main {
     public  static ClassLoader buildLoaderChain () {
         // set the priority of each loader 
        ClassLoader bootstrapClassLoader = new new BootstrapClassLoader (ClassLoader.BOOTSTRP); 
        ClassLoader extensionClassLoader = new new ExtensionClassLoader (ClassLoader.EXTENSION); 
        ClassLoader applicationClassLoader = new new ApplicationClassLoader (ClassLoader.APPLICATION );
         // set the liability chain bootstrap-> extension-> the Application, 
         // priority 123    
         //First passed to the bootstrap loader can then be loaded, if you can not pass to the extension, and so on.
        // If the responsible node priority chain single priority class is loaded == 
        
        // configured load chain 
        bootstrapClassLoader.setNextClassLoader (extensionClassLoader); 
        extensionClassLoader.setNextClassLoader (applicationClassLoader); 
        return bootstrapClassLoader; 
    } 
    
    public  static  void main (String args []) { 
        ClassLoader classLoader = buildLoaderChain (); 
        classLoader.runLoader ( "bootstrp", ClassLoader.BOOTSTRP);        // on Bootstrap loading. 1 
        classLoader.runLoader ( "EXTENSION", ClassLoader.EXTENSION);        // Extension loading 1-> 2
        classLoader.runLoader("APPLICATION", ClassLoader.APPLICATION); // application加载  1->2->3 
    }
}    

 

Run Results: 
loader of the current priority: the priority of the current class 1--:. 1 BootstrapClassLoader: bootstrp loader of the current priority: 1-- current class priority: 2 loader of the current priority: 2-- current class priority: 2 ExtensionClassLoader: EXTENSION loader of the current priority: 1-- current class priority: 3 loader of the current priority: 2-- current class priority: 3 loader of the current priority: 3-- current class priority: 3 ApplicationClassLoader : APPLICATION

 

References:

https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html

Guess you like

Origin www.cnblogs.com/huang-changfan/p/10982968.html