本文介绍了Java核心库中GoF设计模式的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! > $ b $($) b java.lang.Runtime#getRuntime() java.awt.Desktop#getDesktop() java.lang.System#getSecurityManager() 结构模式 适配器 java.util.Arrays#asList() java.util.Collections#list() java.util.Collections#enumeration() java.io.InputStreamReader(InputStream) (返回一个阅读器) java.io.OutputStreamWriter(OutputStream) (返回一个 Writer ) javax.xml.bind.annotation.adapters.XmlAdapter#marshal() 和 #unmarshal() Bridge 没有想到。一个虚构的例子将是新的LinkedHashMap(LinkedHashSet< K>,List< V>),其返回不能克隆项目的不可修改的链接地图,但是使用他们。 java.util.Collections#newSetFromMap() 和 singletonXXX() 方法接近。 综合 java.awt.Container#add(Component) (几乎遍布Swing) javax.faces.comp onent.UIComponent#getChildren() (几乎遍布JSF UI) 装饰器 java.io.InputStream , OutputStream , Reader 和 Writer 有一个构造函数采用相同类型的实例。 java.util.Collections , checkedXXX() / code> , synchronizedXXX() 和 unmodifiableXXX() 方法。 javax.servlet.http.HttpServletRequestWrapper 和 HttpServletResponseWrapper Facade javax.faces.context.FacesContext ,它内部使用抽象/接口类型 LifeCycle , ViewHandler , NavigationHandler 等等,没有最终用户不得不担心(然而,它们被注射覆盖 javax.faces.context.ExternalContext ,它内部使用 ServletContext , HttpSession , HttpServletRequest , HttpServletResponse 等等。 flyweight java.lang.Integer#valueOf(int) (也在 布尔值 , 字节 , 字符 , Short , Long 和 BigDecimal ) 代理 ul> java.lang.reflect.Proxy java.rmi。* javax.ejb。 EJB (这里的说明) javax.i nject.Inject (这里的说明) javax.persistence.PersistenceContext 行为模式 责任链 (可以通过行为方法识别(间接地)调用在队列中相同抽象/接口类型的另一个实现中的相同方法) java.util.logging.Logger#log() javax.servlet.Filter#doFilter() 命令 java.util 。模式 java.text.Normalizer java.text.Format javax.el.ELResolver 迭代器 java.util.Iterator (因此也包括 java.util.Scanner !) 的所有实施java.util.Enumeration Mediator java.util.Timer (全部 scheduleXXX()方法) java.util.concurrent.Executor#execute() java.util.concurrent.ExecutorService ( invokeXXX()和 submit()方法) java.util.concurrent.ScheduledExecutorService (全部 scheduleXXX()方法) java.lang.reflect.Method#invoke() Memento java.util.Date (setter方法这样做, Date 内部由 long value) java.io.Serializable javax.faces.component.StateHolder 观察者(或发布/订阅) java.util.Observer / java.util.Observable (很少在现实世界中使用) java.util.EventListener (几乎遍布Swing) javax .servlet.http.HttpSessionBindingListener javax.servlet.http.HttpSessionAttributeListener javax.faces.event.PhaseListener 州 javax.faces.lifecycle.LifeCycle#execute() (由 FacesServlet ,行为取决于JSF生命周期的当前阶段(状态)) 策略 java.util.Comparator#compare() ,执行其他 Collections#sort()。 javax.servlet.http.HttpServlet , service()和所有 doXXX()方法采取 HttpServletRequest 和 HttpServletResponse 并且实现者必须处理它们(而不是将它们作为实例变量保留)。 javax.servlet.Filter#doFilter() 模板方法 java.io.InputStream , java.io.OutputStream , java.io.Reader 和 java.io.Writer 。 li> 所有非抽象方法 java.util.AbstractList , java.util。 AbstractSet 和 java.util.AbstractMap 。 javax.servlet.http.HttpServlet ,所有的 doXXX()方法默认情况下会向响应发送HTTP 405方法不允许错误。你可以自由地执行任何一个或任何一个。 访问者 javax.lang.model.element.AnnotationValue 和 AnnotationValueVisitor javax.lang.model.element。元素 和 ElementVisitor javax.lang.model.type.TypeMirror 和 TypeVisitor java.nio.file.FileVisitor 和 SimpleFileVisitor javax.faces.component.visit.VisitContext 和 VisitCallback I am learning GoF Java Design Patterns and I want to see some real life examples of them. What are some good examples of these Design Patterns in Java's core libraries? 解决方案 You can find an overview of a lot of design patterns in Wikipedia. It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as many pattern implementations as possible, found in both the Java SE and Java EE APIs.Creational patternsAbstract factoryjavax.xml.parsers.DocumentBuilderFactory#newInstance()javax.xml.transform.TransformerFactory#newInstance()javax.xml.xpath.XPathFactory#newInstance()Builderjava.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.AppendableFactory methodjava.util.Calendar#getInstance()java.util.ResourceBundle#getBundle()java.text.NumberFormat#getInstance()java.nio.charset.Charset#forName()java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns singleton object per protocol)java.util.EnumSet#of()javax.xml.bind.JAXBContext#createMarshaller() and other similar methodsPrototypejava.lang.Object#clone() (the class has to implement java.lang.Cloneable)Singletonjava.lang.Runtime#getRuntime()java.awt.Desktop#getDesktop()java.lang.System#getSecurityManager()Structural patternsAdapterjava.util.Arrays#asList()java.util.Collections#list()java.util.Collections#enumeration()java.io.InputStreamReader(InputStream) (returns a Reader)java.io.OutputStreamWriter(OutputStream) (returns a Writer)javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()BridgeNone 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, but uses them. The java.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.Compositejava.awt.Container#add(Component) (practically all over Swing thus)javax.faces.component.UIComponent#getChildren() (practically all over JSF UI thus)DecoratorAll subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapperFacadejavax.faces.context.FacesContext, it internally uses among others the abstract/interface types LifeCycle, 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 uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc.Flyweightjava.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Long and BigDecimal)Proxyjava.lang.reflect.Proxyjava.rmi.*javax.ejb.EJB (explanation here)javax.inject.Inject (explanation here)javax.persistence.PersistenceContextBehavioral patternsChain of responsibilityjava.util.logging.Logger#log()javax.servlet.Filter#doFilter()CommandAll implementations of java.lang.RunnableAll implementations of javax.swing.ActionInterpreterjava.util.Patternjava.text.NormalizerAll subclasses of java.text.FormatAll subclasses of javax.el.ELResolverIteratorAll implementations of java.util.Iterator (thus among others also java.util.Scanner!).All implementations of java.util.EnumerationMediatorjava.util.Timer (all scheduleXXX() methods)java.util.concurrent.Executor#execute()java.util.concurrent.ExecutorService (the invokeXXX() and submit() methods)java.util.concurrent.ScheduledExecutorService (all scheduleXXX() methods)java.lang.reflect.Method#invoke()Mementojava.util.Date (the setter methods do that, Date is internally represented by a long value)All implementations of java.io.SerializableAll implementations of javax.faces.component.StateHolderObserver (or Publish/Subscribe)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.HttpSessionBindingListenerjavax.servlet.http.HttpSessionAttributeListenerjavax.faces.event.PhaseListenerStatejavax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)Strategyjava.util.Comparator#compare(), executed by among others Collections#sort().javax.servlet.http.HttpServlet, the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).javax.servlet.Filter#doFilter()Template methodAll 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 the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.Visitorjavax.lang.model.element.AnnotationValue and AnnotationValueVisitorjavax.lang.model.element.Element and ElementVisitorjavax.lang.model.type.TypeMirror and TypeVisitorjava.nio.file.FileVisitor and SimpleFileVisitorjavax.faces.component.visit.VisitContext and VisitCallback 这篇关于Java核心库中GoF设计模式的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!