pattern in JDK

 

Creational patterns

Abstract factory (recognizeable by creational methods returning an abstract/interface type)

Builder (recognizeable by creational methods returning the instance itself)

Factory method (recognizeable by creational methods returning a concrete type)

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

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


 

Structural patterns

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)

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)

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

Decorator (recognizeable by creational methods taking an instance of same abstract/interface type)

Facade (recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)

Flyweight (recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)

Proxy (recognizeable by creational methods which returns an implementation of given abstract/interface type which in turn delegates/uses a different implementation of given abstract/interface type)

The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.


 

Behavioral patterns

Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method inanother implementation of same abstract/interface type in a queue)

Command (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been encapsulated by the command implementation during its creation)

Interpreter (recognizeable by behavioral methods returning a structurally different instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)

Iterator (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)

Mediator (recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)

Memento (recognizeable by behavioral methods which internally changes the state of the whole instance)

Observer (or Publish/Subscribe) (recognizeable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state)

State (recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)

Strategy (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been passed-in as method argument into the strategy implementation)

Template method (recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)

Visitor (recognizeable by two different abstract/interface types which has methods definied which takes each theother abstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)

 

---------------------------------------------------------------------------------------------------------------------------------------------------------

 

下面是JDK中有关23个经典设计模式的示例,在stakeoverflow也有相应的讨论:
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns

Structural(结构模式)
Adapter:
把一个接口或是类变成另外一种。

  • java.util.Arrays#asList()
  • javax.swing.JTable(TableModel)
  • java.io.InputStreamReader(InputStream)
  • java.io.OutputStreamWriter(OutputStream)
  • javax.xml.bind.annotation.adapters.XmlAdapter#marshal()
  • javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()

Bridge:
把抽象和实现解藕,于是接口和实现可在完全独立开来。

  • AWT (提供了抽象层映射于实际的操作系统)
  • JDBC

Composite:
让使用者把单独的对象和组合对象混用。

  • javax.swing.JComponent#add(Component)
  • java.awt.Container#add(Component)
  • java.util.Map#putAll(Map)
  • java.util.List#addAll(Collection)
  • java.util.Set#addAll(Collection)

Decorator:
为一个对象动态的加上一系列的动作,而不需要因为这些动作的不同而产生大量的继承类。这个模式在JDK中几乎无处不在,所以,下面的列表只是一些典型的。

  • java.io.BufferedInputStream(InputStream)
  • java.io.DataInputStream(InputStream)
  • java.io.BufferedOutputStream(OutputStream)
  • java.util.zip.ZipOutputStream(OutputStream)
  • java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()

Facade:
用一个简单的接口包状一组组件,接口,抽象或是子系统。

  • java.lang.Class
  • javax.faces.webapp.FacesServlet

Flyweight:
有效率地存储大量的小的对象。

  • java.lang.Integer#valueOf(int)
  • java.lang.Boolean#valueOf(boolean)
  • java.lang.Byte#valueOf(byte)
  • java.lang.Character#valueOf(char)

Proxy:
用一个简单的对象来代替一个复杂的对象。

  • java.lang.reflect.Proxy
  • RMI
Creational(创建模式)
Abstract factory:
创建一组有关联的对象实例。这个模式在JDK中也是相当的常见,还有很多的framework例如Spring。我们很容易找到这样的实例。

  • java.util.Calendar#getInstance()
  • java.util.Arrays#asList()
  • java.util.ResourceBundle#getBundle()
  • java.sql.DriverManager#getConnection()
  • java.sql.Connection#createStatement()
  • java.sql.Statement#executeQuery()
  • java.text.NumberFormat#getInstance()
  • javax.xml.transform.TransformerFactory#newInstance()

Builder:
主要用来简化一个复杂的对象的创建。这个模式也可以用来实现一个 Fluent Interface

  • java.lang.StringBuilder#append()
  • java.lang.StringBuffer#append()
  • java.sql.PreparedStatement
  • javax.swing.GroupLayout.Group#addComponent()

Factory:
简单来说,按照需求返回一个类型的实例。

  • java.lang.Proxy#newProxyInstance()
  • java.lang.Object#toString()
  • java.lang.Class#newInstance()
  • java.lang.reflect.Array#newInstance()
  • java.lang.reflect.Constructor#newInstance()
  • java.lang.Boolean#valueOf(String)
  • java.lang.Class#forName()

Prototype:
使用自己的实例创建另一个实例。有时候,创建一个实例然后再把已有实例的值拷贝过去,是一个很复杂的动作。所以,使用这个模式可以避免这样的复杂性。

  • java.lang.Object#clone()
  • java.lang.Cloneable

Singleton:
只允许一个实例。在 Effective Java中建议使用Emun.

  • java.lang.Runtime#getRuntime()
  • java.awt.Toolkit#getDefaultToolkit()
  • java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()
  • java.awt.Desktop#getDesktop()
Behavioral(行为模式)

Chain of responsibility:
把一个对象在一个链接传递直到被处理。在这个链上的所有的对象有相同的接口(抽象类)但却有不同的实现。

  • java.util.logging.Logger#log()
  • javax.servlet.Filter#doFilter()

Command:
把一个或一些命令封装到一个对象中。

  • java.lang.Runnable
  • javax.swing.Action

Interpreter:
一个语法解释器的模式。

  • java.util.Pattern
  • java.text.Normalizer
  • java.text.Format

Iterator:
提供一种一致的方法来顺序遍历一个容器中的所有元素。

  • java.util.Iterator
  • java.util.Enumeration

Mediator:
用来减少对象单的直接通讯的依赖关系。使用一个中间类来管理消息的方向。

  • java.util.Timer
  • java.util.concurrent.Executor#execute()
  • java.util.concurrent.ExecutorService#submit()
  • java.lang.reflect.Method#invoke()

Memento:
给一个对象的状态做一个快照。Date类在内部使用了一个long型来做这个快照。

  • java.util.Date
  • java.io.Serializable

Null Object:
这个模式用来解决如果一个Collection中没有元素的情况。

  • java.util.Collections#emptyList()
  • java.util.Collections#emptyMap()
  • java.util.Collections#emptySet()

Observer:
允许一个对象向所有的侦听的对象广播自己的消息或事件。

  • java.util.EventListener
  • javax.servlet.http.HttpSessionBindingListener
  • javax.servlet.http.HttpSessionAttributeListener
  • javax.faces.event.PhaseListener

State:
这个模式允许你可以在运行时很容易地根据自身内部的状态改变对象的行为。

  • java.util.Iterator
  • javax.faces.lifecycle.LifeCycle#execute()

Strategy:
定义一组算法,并把其封装到一个对象中。然后在运行时,可以灵活的使用其中的一个算法。

  • java.util.Comparator#compare()
  • javax.servlet.http.HttpServlet
  • javax.servlet.Filter#doFilter()

Template method:
允许子类重载部分父类而不需要完全重写。

  • java.util.Collections#sort()
  • java.io.InputStream#skip()
  • java.io.InputStream#read()
  • java.util.AbstractList#indexOf()

Visitor:

作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作.

  • javax.lang.model.element.Element 和javax.lang.model.element.ElementVisitor
  • javax.lang.model.type.TypeMirror 和javax.lang.model.type.TypeVisitor

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值