十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
A.创建型模式
创新互联主营魏都网站建设的网络公司,主营网站建设方案,手机APP定制开发,魏都h5成都小程序开发搭建,魏都网站营销推广欢迎魏都等地区企业咨询
抽象工厂(Abstract Factory)
javax.xml.parsers.DocumentBuilderFactory
DocumentBuilderFactory通过FactoryFinder实例化具体的Factory.
使用例子: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("xxx.xml"));
javax.xml.transform.TransformerFactory
使用例子: StreamSource source = new StreamSource(...); StreamSource stylesource = new StreamSource(...); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesource); StreamResult result = new StreamResult(System.out); transformer.transform(source, result)
javax.xml.xpath.XPathFactory
The structure is almost the same as the previous two.
XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression expr = xpath.compile(XPATH_EXPRESSION_STRING); Object result = expr.evaluate(Object item, QName returnType);
2.生成器模式(Builder)
java.lang.Appendable
java.lang.StringBuilder#append()
java.lang.StringBuffer#append()
这里我们看到状态的转换操作都会返回原来的类型的对象,这样就可以构建生成链。
java.nio.ByteBuffer#put()
(CharBuffer
,ShortBuffer
,IntBuffer
,LongBuffer
,FloatBuffer
还有 DoubleBuffer
)
参见上例。
3.工厂方法(Factory method)
java.util.Calendar#getInstance()
工厂方法的构造方法就在所要生成的类型上。 这个抽象工厂有很大不同。
其它的可参见:
java.util.ResourceBundle#getBundle()
java.text.NumberFormat#getInstance()
java.nio.charset.Charset#forName()
java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
java.lang.Object#clone()
5.单例模式(Singleton)
java.lang.Runtime#getRuntime()
java.awt.Desktop#getDesktop()