Java 核心类库中的一些设计模式

来源:互联网 发布:银行模拟教学软件 编辑:程序博客网 时间:2024/05/16 14:52

今天访问java 并发编程网,看到一个翻译征集令,并发编程网的作者从stackoverflow 网站上选取了一些经典问答,遂决定翻译几篇

征集令地址:http://ifeve.com/stackoverflow-assembly/

翻译系列文章:

1.Java 核心类库中的一些设计模式

2. hashMap 与hashTable之间的区别

3.在java中如何创建一个内存泄露



译文:

在Java 核心类库中关于GOF设计模式的例子

问题:   我正在学习GOF的java 设计模式,我想找一些它们中鲜活的例子,在java 核心类库中有哪些好的例子呢?

回答:你可以在维基百科上获得许多设计模式概述。同时也会提到哪些是由GOF提出的。在这里我总结一下,我尽可能多的从java SE 和java EE 的 API 中选取一些设计模式的实现

1.抽象工厂模式 (abstract factory)

  • javax.xml.parsers.DocumentBuilderFactory#newInstance()
  • javax.xml.transform.TransformerFactory#newInstance()
  • javax.xml.xpath.XPathFactory#newInstance()
2.生成器模式(builder)

  • java.lang.StringBuilder#append() (不同步,线程不安全)
  • java.lang.StringBuffer#append() (同步的,线程安全)
  • java.nio.ByteBuffer#put() (同时还有CharBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBuffer and DoubleBuffer)
  • javax.swing.GroupLayout.Group#addComponent()
  •  java.lang.Appendable 的所有实现类
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) (每次协议返回单个对象)
4. 原型模式(prototype)

  • java.lang.Object#clone() (这个类必须实现 java.lang.Cloneable  接口)
5.单例模式 (singleton)

  • java.lang.Runtime#getRuntime()
  • java.awt.Desktop#getDesktop()
6.适配器模式(adapter)

  • java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns aReader)
  • java.io.OutputStreamWriter(OutputStream) (returns aWriter)
  • javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and#unmarshal()
7. 桥接模式(bridge)

None comes to mind yet. A fictive example would be new LinkedHashMap(LinkedHashSet<K>, List<V>) which returns an unmodifiable linked map which doesn't clone the items, butuses them. Thejava.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

8.组合模式(composite)

  • java.awt.Container#add(Component) (practically all over Swing thus)
  • javax.faces.component.UIComponent#getChildren() (practically all over JSF UI thus)
9.装饰模式(decorator)

  • All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
  • java.util.Collections, thecheckedXXX(),synchronizedXXX() and unmodifiableXXX() methods.
  • javax.servlet.http.HttpServletRequestWrapper andHttpServletResponseWrapper

10.外观模式(facade)

  • javax.faces.context.FacesContext, it internally uses among others the abstract/interface typesLifeCycle,ViewHandler,NavigationHandler and many more without that the enduser has to worry about it (which are however overrideable by injection).
  • javax.faces.context.ExternalContext, which internally usesServletContext,HttpSession,HttpServletRequest,HttpServletResponse, etc.
11.享元模式(flyweight)

java.lang.Integer#valueOf(int) (同时还有Boolean,Byte,Character,Short and Long)


12.代理模式(proxy)

  • java.lang.reflect.Proxy
  • java.rmi.*, 整个API
13. 职责链模式(chain of responsibility)

  • java.util.logging.Logger#log()
  • javax.servlet.Filter#doFilter()
14. 命令模式(command)

  • All implementations of java.lang.Runnable
  • All implementations of javax.swing.Action

15. 解析器模式(interpreter)

  • java.util.Pattern
  • java.text.Normalizer
  • All subclasses of java.text.Format
  • All subclasses of javax.el.ELResolver

16. 迭代器模式 (iterator)

  • All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
  • All implementations of java.util.Enumeration
17. 中介者模式 (mediator)

  • java.util.Timer (allscheduleXXX() methods)
  • java.util.concurrent.Executor#execute()
  • java.util.concurrent.ExecutorService (theinvokeXXX() andsubmit() methods)
  • java.util.concurrent.ScheduledExecutorService (allscheduleXXX() methods)
  • java.lang.reflect.Method#invoke()

18.备忘录模式(memento)

  • java.util.Date (the setter methods do that,Date is internally represented by along value)
  • All implementations of java.io.Serializable
  • All implementations of javax.faces.component.StateHolder
19. 观察者模式(observer)

  • java.util.Observer/java.util.Observable (rarely used in real world though)
  • All implementations of java.util.EventListener (practically all over Swing thus)
  • javax.servlet.http.HttpSessionBindingListener
  • javax.servlet.http.HttpSessionAttributeListener
  • javax.faces.event.PhaseListener
20.状态模式(state)

javax.faces.lifecycle.LifeCycle#execute()

(controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)


21.策略模式(strategy)

  • java.util.Comparator#compare(), executed by among othersCollections#sort().
  • javax.servlet.http.HttpServlet, theservice() and alldoXXX() methods takeHttpServletRequest andHttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).
  • javax.servlet.Filter#doFilter()

22. 模板方法模式(template method)

  • All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
  • All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
  • javax.servlet.http.HttpServlet, all thedoXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
23. 访问者模式(visitor)

  • javax.lang.model.element.AnnotationValue andAnnotationValueVisitor
  • javax.lang.model.element.Element andElementVisitor
  • javax.lang.model.type.TypeMirror andTypeVisitor


原文:

原文地址:http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries















0 0
原创粉丝点击