[Java] inner class usage summary

Foreword

  When inner classes to learn basic Java knowledge most people have been understanding, but also most people just understand it, in the long years of development you will discover the true benefits of inner classes

definition

  A class may be defined inside another class, which is an internal class. Internal class is a very useful feature because it allows you to put some grouped together logically related data, and controls the visibility of the class.

  • Static inner classes: static inner classes in addition to access more than enclosing class modifiers than outside, and peripheral class is no different, but the code on a static inner classes organized in a class outside inside
  • Non-static inner classes: the premise of the class is to create an external class instance has been created, while the inner class can access the instance variables of the class, it can be done outside of a class instance is a member of several other private inner class instance access .

Scenes

  • The use of internal class implements multiple inheritance
  • Internal multi-instance class instance class members shared external characteristics: iterative mode

practice

  BACKGROUND : The system is a decision-making process based on the system state machine, there is a workflow object, and each node is a state flow mode state; for flow out through the configuration, the flow configuration having a node and good examples status. Now it requires a different user request may perform the same process.

public class Workflow{
  public Node root   
}

 

  Problem : the same process node, different users perform, will have a different process node status for each user. That is an example of a single process node itself stateless in terms of users, but it is indeed time users will have a status value, then how to place these state?

public  class the Node { 
     ...... 
     Link Link; // adjacency list representation of FIG 
     ...... 
     the Map config = new new the HashMap ();     
}

 

  Program : We now refer to one thing: jvm, the method itself for each execution stateless, placed in the zone method; but each execution, which will create a method frame, pressed into the thread stack. And process execution is not very similar? So I, you create a frame objects perform a process for each execution of the process node, this node multiple users perform the same time, simply create the execution frame on the line, because they will get a private node performs frame of reference, comprising a configuration of the node. So this is the place to perform frame where the user performs the best state.

public class Node{
     private Map config = new HashMap();    

     public ExecuteFrame{
          Map exeConfig = new HashMap();
          public void doExecute(){
             Service.doAction(config.putAll(exeConfig))
        }  
     }  
}    

  The complete code is as follows:

public  class the Node { 
Private the Map config = new new the HashMap ();
// using context, create a node performs spatial public ExecuteFrame newFrame, and (the Context context) { ..... return new new ExecuteFrame (context); } public class ExecuteFrame { the Context context public ExecuteFrame (the context context) { the this .context = context; } the Map exeConfig = new new the HashMap (); public void the doExecute (the context context) { Service.doAction(config.putAll(exeConfig)) ...... } } }

 

   Finally, I also created an execution object, used in the execution process of managing different frame, so that they can follow the execution path of the user, this design I am very satisfied.

 

 

  

Guess you like

Origin www.cnblogs.com/iCanhua/p/11372287.html