Skip to main content

Search CooLWJ

选择搜索范围,输入关键词后按回车。

Language

Which design patterns are used in the JDK?

I, Structural Patterns

1, Adapter pattern

Used to convert one interface into another

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()

2, Bridge Pattern

This pattern decouples the abstraction from the implementation of its operations, allowing the abstraction and implementation to vary independently

AWT (It provides an abstraction layer which maps onto the native OS the windowing support.)

JDBC

3, Composite Pattern

Makes individual objects and compositions of objects equivalent from the client's perspective. In other words, a method of a particular type also accepts that same type as a parameter

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)

4,Decorator pattern

Dynamically attach additional functionality to an object; this is also an alternative to subclassing. As you can see, when creating an instance of a type, you also pass in an object of the same type. This is found throughout the JDK—you will see it everywhere—so the following list includes only a small selection

java.io.BufferedInputStream(InputStream)

java.io.DataInputStream(InputStream)

java.io.BufferedOutputStream(OutputStream)

java.util.zip.ZipOutputStream(OutputStream)

java.util.Collections#checked<list |sortedmap |sortedset |set |map>

5, Facade pattern

Provide a simple interface to a set of components, interfaces, abstractions, or subsystems

java.lang.Class

javax.faces.webapp.FacesServlet

6, Flyweight Pattern

Use caching to speed up access to large numbers of small objects

java.lang.Integer#valueOf(int)

java.lang.Boolean#valueOf(boolean)

java.lang.Byte#valueOf(byte)

java.lang.Character#valueOf(char)

7, Proxy Pattern

The proxy pattern uses a simple object in place of a complex object or one that takes a long time to create.

java.lang.reflect.Proxy

RMI


An illustration depicting software design through a Java coffee cup and an assembly scene

II, Creational Patterns

1, Abstract Factory Pattern

The Abstract Factory pattern provides a protocol for creating a series of related or independent objects without specifying their concrete types. It decouples an application from the concrete implementation of the framework it uses. This is found throughout the JDK and many frameworks, such as Spring. It is also easy to recognize: a method that creates a new object but returns an interface or abstract class is an example of the Abstract Factory pattern

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()

2,Builder pattern (Builder)

Defines a new class to construct instances of another class, simplifying the creation of complex objects. The Builder pattern also commonly uses method chaining to implement

java.lang.StringBuilder#append()

java.lang.StringBuffer#append()

java.sql.PreparedStatement

javax.swing.GroupLayout.Group#addComponent()

3, Factory Method

is simply a method that returns a concrete object

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()

4,Prototype Pattern

Allows instances of a class to generate copies of themselves. If creating an object instance is extremely complex and time-consuming, you can use this pattern to copy an object and modify it directly instead of creating a new instance from scratch

java.lang.Object#clone()

java.lang.Cloneable

5,Singleton pattern

Used to ensure that a class has only one instance. In Effetive Java, Joshua Bloch suggests another approach: using an enum

java.lang.Runtime#getRuntime()

java.awt.Toolkit#getDefaultToolkit()

java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()

java.awt.Desktop#getDesktop()


III, Behavioral Patterns

1, Chain of Responsibility pattern

Decouples objects by passing a request from one object to the next in a chain until the request has been fully handled

java.util.logging.Logger#log()

javax.servlet.Filter#doFilter()

2, Command Pattern

Encapsulate operations within objects so they can be stored, passed, and returned

java.lang.Runnable

javax.swing.Action

3,Interpreter pattern

This pattern typically defines a language’s grammar and then parses statements that follow that grammar

java.util.Pattern

java.text.Normalizer

java.text.Format

4, Iterator Pattern

Provides a consistent way to access objects in a collection sequentially, independent of the collection's underlying implementation

java.util.Iterator

java.util.Enumeration

5,Mediator Pattern

Uses an intermediary object to distribute messages and reduce direct dependencies between classes

java.util.Timer

java.util.concurrent.Executor#execute()

java.util.concurrent.ExecutorService#submit()

java.lang.reflect.Method#invoke()

6,Memento pattern

Creates a snapshot of an object's state so that the object can restore its original state without exposing its contents. The Date object implements the Memento pattern through an internal long value

java.util.Date

java.io.Serializable

7, Null Object Pattern

This pattern uses a meaningless object to represent the absence of an object. It eliminates the need for additional handling of null objects

java.util.Collections#emptyList()

java.util.Collections#emptyMap()

java.util.Collections#emptySet()

8, Observer Pattern

It allows an object to flexibly send messages to interested objects

java.util.EventListener

javax.servlet.http.HttpSessionBindingListener

javax.servlet.http.HttpSessionAttributeListener

javax.faces.event.PhaseListener

9, State Pattern

By changing an object's internal state, you can dynamically change its behavior at runtime

java.util.Iterator

javax.faces.lifecycle.LifeCycle#execute()

10, Strategy Pattern

Use this pattern to encapsulate a group of algorithms as a series of objects. Passing these objects allows you to change the program's functionality flexibly

java.util.Comparator#compare()

javax.servlet.http.HttpServlet

javax.servlet.Filter#doFilter()

11, Template Method Pattern

Allow subclasses to override part of a method rather than the entire method; you can control which operations subclasses need to override

java.util.Collections#sort()

java.io.InputStream#skip()

java.io.InputStream#read()

java.util.AbstractList#indexOf()

12,Visitor Pattern

Provides a convenient, maintainable way to operate on a group of objects. It allows you to modify or extend their behavior without changing the objects being operated on

javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor

javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor

Translator’s note: Many points may be debatable. Whether something qualifies as a particular pattern is not especially important; what matters is that its design can offer lessons for improving our code.


Comments

Sending

Choose your theme

Your theme applies across the site.