当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

设计模式(1)Factory模式和Abstract Factory模式


    Factory模式
     利用给Factory对象传递不同的参数,以返回具有相同基类或实现了同一接口的对象。
    Abstract Factory模式
     先利用Factory模式返回Factory对象,在通过Factory对象返回不同的对象!
   
    Java中的例子-----JAXP1.0.1 : 
      以下是用Simple API for XML(SAX) Parse XML文件的片段
      ... 
     
      String uri = "file:" + new File (xmlfilename).getAbsolutePath ();
      Parser parser;
      //1. Abstract Factory模式
      SAXParserFactory spf = SAXParserFactory.newInstance ();   
      String validation = System.getProperty (
   "javax.xml.parsers.validation", "false");
      if (validation.equalsIgnoreCase("true"))
       spf.setValidating (true);
  
      //2. Factory模式
      SAXParser sp = spf.newSAXParser ();   
      parser = sp.getParser ();
      parser.setDocumentHandler (this);     
      parser.parse (uri);
      ....
     
      1. SAXParserFactory中的静态方法newInstance()根据系统属性javax.xml.parsers.SAXParserFactory不同的值
      生成不同的SAXParserFactory对象spf。然后SAXParserFactory对象又利用方法newSAXParser()生成SAXParser对象。
      注意:
      SAXParserFactory 的定义为:
      public abstract class SAXParserFactory extends java.lang.Object
      SAXParserFactoryImpl 的定义为:
      public class SAXParserFactoryImpl extends javax.xml.parsers.SAXParserFactory
     
      public static SAXParserFactory newInstance() {
     String factoryImplName = null;
      try {
       factoryImplName =
        System.getProperty("javax.xml.parsers.SAXParserFactory",
                "com.sun.xml.parser.SAXParserFactoryImpl");
     }catch (SecurityException se) {
       factoryImplName = "com.sun.xml.parser.SAXParserFactoryImpl";
     }
      SAXParserFactory factoryImpl;
      try {
       Class clazz = Class.forName(factoryImplName);
       factoryImpl = (SAXParserFactory)clazz.newInstance();
      }catch (ClassNotFoundException cnfe) {
       throw new FactoryConfigurationError(cnfe);
     } catch (IllegalAccessException iae) {
       throw new FactoryConfigurationError(iae);
     } catch (InstantiationException ie) {
       throw new FactoryConfigurationError(ie);
     }
      return factoryImpl;
      }
     
      2. newSAXParser()方法在SAXParserFactory定义为抽象方法,
      SAXParserFactoryImpl继承了SAXParserFactory,它实现了方法newSAXParser():
     
      public SAXParser newSAXParser() throws SAXException,
     ParserConfigurationException
      {
        SAXParserImpl saxParserImpl = new SAXParserImpl (this);
        return saxParserImpl;
      }
     
      注意:
       SAXParserImpl的定义为:
       public class SAXParserImpl extends javax.xml.parsers.SAXParser
       SAXParserImpl的构造函数定义为:
       SAXParserImpl (SAXParserFactory spf) throws SAXException,
   ParserConfigurationException
       {
        super();
       this.spf = spf;
       if (spf.isValidating ()) {
        parser = new ValidatingParser();
        validating = true;
        }
       else {
        parser = new Parser();
        }
  
       if (spf.isNamespaceAware ()) {
        namespaceAware = true;
        throw new ParserConfigurationException
   ("Namespace not supported by SAXParser");
       }
       } 
       
      本例子中用到的class和interface的类关系图如下
  
相关内容
赞助商链接