"Pragmatic Programmer" - Notes six reading - dummy DI factory mode dependency injection

 

Factory class:

. 1  public  class AgentFinderFactory {
 2  
. 3    Private  static AgentFinderFactory Singleton;
 . 4  
. 5    Private AgentFinderFactory () {
 . 6    }
 . 7  
. 8    public  static AgentFinderFactory the getInstance () {
 . 9      IF (Singleton == null ) {
 10        Singleton = new new AgentFinderFactory (); // Single Example model to ensure uniqueness of this factory class
 . 11      }
 12 is      return Singleton;
 13 is    }
 14  
15    publicAgentFinder getAgentFinder (String agentType) {// depending on the type, class selection output processing
 16      AgentFinder Finder = null ;
 . 17      Switch (agentType) {
 18 is      Case "Spreadsheet" :
 . 19        Finder = new new SpreadsheetAgentFinder ();
 20 is        BREAK ;
 21 is      Case "WebService" :
 22 is        Finder = new new WebServiceAgentFinder ();
 23 is        BREAK ;
 24      default :
 25        Finder = new new WebServiceAgentFinder ();
 26 is        BREAK;
27      }
 28      return finds;
29    }
 30 }

Business class:

public  class HollywoodServiceWithFactory { 

  public  static List <- Agent> getFriendlyAgents (String agentFinderType) { 
    AgentFinderFactory Factory = AgentFinderFactory.getInstance (); // Get factory class, then the class factory initial loading 
    AgentFinder Finder = factory.getAgentFinder (agentFinderType); // The all types of incoming, to obtain the required operation target 
    List <- Agent> Agents = finder.findAllAgents (); // perform business 
    List <- Agent> friendlyAgents = filterAgents (Agents, "the Java Developers" );
     return friendlyAgents; 
  } 

  public  static List < Agent> filterAgents (List <Agent>agents, String agentType) {// This method does not have to be concerned about 
   ..... 
  ....
} }

 

Guess you like

Origin www.cnblogs.com/Deters/p/11568974.html