唐磊的个人博客

JDK里的设计模式

关于本文的说明:本文是本来从网上收集进过自己细心整理而得。参考文章来自:http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns,本人不保证内容的正确性。仅供参考,因用word排版,因此贴到Wordpress格式比较乱,时间关系,不作调整,还请谅解。

Creational patterns 1

  1. Abstract factory 1

  2. Builder 2

  3. Factory method 2

  4. Prototype 2

  5. Singleton 3

Structural patterns 3

  1. Adapter 3

  2. Bridge 3

  3. Composite 3

  4. Decorator 4

  5. Façade 4

  6. Flyweight 4

  7. Proxy 5

Behavioral patterns 5

  1. Chain of responsibility 5

  2. Command 5

  3. Interpreter 6

  4. Iterator 6

  5. Mediator 6

  6. Memento 6

  7. Observer 7

  8. State 7

  9. Strategy 7

  10. Template method 8

  11. Visitor 8

————————————————————————————————————————————————————————

Creational patterns创建性模式

1. Abstract factory创建一组有关联的对象,在JDK中比较常见,还有很多的框架也用了这个模式,例如spring等。

——recognizeable by creational methods returning an abstract/interface type

**

**  java.util.Calendar#getInstance()

 java.util.Arrays#asList()

 java.util.ResourceBundle#getBundle()

 java.net.URL#openConnection()

 java.sql.DriverManager#getConnection()

 java.sql.Connection#createStatement()

 java.sql.Statement#executeQuery()

 java.text.NumberFormat#getInstance()

 java.lang.management.ManagementFactory (all getXXX() methods)

 java.nio.charset.Charset#forName()

 javax.xml.parsers.DocumentBuilderFactory#newInstance()

 javax.xml.transform.TransformerFactory#newInstance()

 javax.xml.xpath.XPathFactory#newInstance()

2. Builder用来简化一个复杂的对象的创建。

——(recognizeable by creational methods returning the instance itself)

 java.lang.StringBuilder#append() (unsynchronized)

 java.lang.StringBuffer#append() (synchronized)

 java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)

 javax.swing.GroupLayout.Group#addComponent()

 All implementations of java.lang.Appendable

3. Factory method按照需求返回一个类型的实例

—— (recognizeable by creational methods returning a concrete type)

 java.lang.Object#toString() (overrideable in all subclasses)

 java.lang.Class#newInstance()

 java.lang.Integer#valueOf(String) (also on Boolean, Byte, Character, Short, Long, Float and Double)

 java.lang.Class#forName()

 java.lang.reflect.Array#newInstance()

 java.lang.reflect.Constructor#newInstance()

4. Prototype使用自己的实例创建另一个实例

——recognizeable by creational methods returning a different instance of itself with the same properties)

 java.lang.Object#clone() (the class has to implement java.lang.Cloneable)

5. Singleton单例就不说了

——(recognizeable by creational methods returning the same instance (usually of itself) everytime)

 java.lang.Runtime#getRuntime()

 java.awt.Desktop#getDesktop()

————————————————————————————————————————————————————————

Structural patterns结构性模式

**

  1. Adapter把一个接口或者类变成另一个**

——(recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)

 java.io.InputStreamReader(InputStream) (returns a Reader)

 java.io.OutputStreamWriter(OutputStream) (returns a Writer)

 javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()

7. Bridge抽象和实现解耦,使接口和实现独立出来

——(recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)

 A fictive example would be new LinkedHashMap(LinkedHashSet, List) which returns an unmodifiable linked map which doesn’t clone the items, but uses them. The java.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

8. Composite把单独的对象和组合的对象混合使用

——( recognizeable by behavioral methods taking an instance of same abstract/interface type)

 java.util.Map#putAll(Map)

 java.util.List#addAll(Collection)

 java.util.Set#addAll(Collection)

 java.nio.ByteBuffer#put(ByteBuffer) (also on CharBu