我正在学习GoF Java设计模式,我想看到一些现实生活中的例子。Java核心库中有哪些设计模式的好例子?


当前回答

Observer pattern throughout whole swing (Observable, Observer) MVC also in swing Adapter pattern: InputStreamReader and OutputStreamWriter NOTE: ContainerAdapter, ComponentAdapter, FocusAdapter, KeyAdapter, MouseAdapter are not adapters; they are actually Null Objects. Poor naming choice by Sun. Decorator pattern (BufferedInputStream can decorate other streams such as FilterInputStream) AbstractFactory Pattern for the AWT Toolkit and the Swing pluggable look-and-feel classes java.lang.Runtime#getRuntime() is Singleton ButtonGroup for Mediator pattern Action, AbstractAction may be used for different visual representations to execute same code -> Command pattern Interned Strings or CellRender in JTable for Flyweight Pattern (Also think about various pools - Thread pools, connection pools, EJB object pools - Flyweight is really about management of shared resources) The Java 1.0 event model is an example of Chain of Responsibility, as are Servlet Filters. Iterator pattern in Collections Framework Nested containers in AWT/Swing use the Composite pattern Layout Managers in AWT/Swing are an example of Strategy

我猜还有更多

其他回答

Flyweight用于字节,短,整数,长和字符串的一些值。 Facade被用在很多地方,但最明显的是脚本接口。 单例-想到java.lang.Runtime。 抽象工厂-也脚本和JDBC API。 命令- TextComponent的撤销/重做。 解释器- RegEx (java.util.regex.)和SQL (java.sql.)API。 原型-不是100%确定这是否算数,但我认为clone()方法可以用于此目的。

尽管我对这一点有点不熟悉,但Java XML API经常使用Factory。我是说看看这个:

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
String title = XPathFactory.newInstance().newXPath().evaluate("//title", doc);

...诸如此类。

此外,各种缓冲区(StringBuffer, ByteBuffer, StringBuilder)使用Builder。

工厂方法

java.util.Collection#Iterator is a good example of a Factory Method. Depending on the concrete subclass of Collection you use, it will create an Iterator implementation. Because both the Factory superclass (Collection) and the Iterator created are interfaces, it is sometimes confused with AbstractFactory. Most of the examples for AbstractFactory in the the accepted answer (BalusC) are examples of Factory, a simplified version of Factory Method, which is not part of the original GoF patterns. In Facory the Factory class hierarchy is collapsed and the factory uses other means to choose the product to be returned.

抽象工厂

抽象工厂有多个工厂方法,每个方法创建一个不同的产品。一家工厂生产的产品是为了一起使用(您的打印机和墨盒最好来自同一家(抽象)工厂)。正如上面的回答中提到的,不同平台的AWT GUI组件家族就是一个例子(尽管它的实现与Gof中描述的结构不同)。

抽象工厂模式被用在很多地方。 例如,DatagramSocketImplFactory, PreferencesFactory。还有很多——在Javadoc中搜索名称中包含“Factory”的接口。

同样,工厂模式也有相当多的实例。

Observer pattern throughout whole swing (Observable, Observer) MVC also in swing Adapter pattern: InputStreamReader and OutputStreamWriter NOTE: ContainerAdapter, ComponentAdapter, FocusAdapter, KeyAdapter, MouseAdapter are not adapters; they are actually Null Objects. Poor naming choice by Sun. Decorator pattern (BufferedInputStream can decorate other streams such as FilterInputStream) AbstractFactory Pattern for the AWT Toolkit and the Swing pluggable look-and-feel classes java.lang.Runtime#getRuntime() is Singleton ButtonGroup for Mediator pattern Action, AbstractAction may be used for different visual representations to execute same code -> Command pattern Interned Strings or CellRender in JTable for Flyweight Pattern (Also think about various pools - Thread pools, connection pools, EJB object pools - Flyweight is really about management of shared resources) The Java 1.0 event model is an example of Chain of Responsibility, as are Servlet Filters. Iterator pattern in Collections Framework Nested containers in AWT/Swing use the Composite pattern Layout Managers in AWT/Swing are an example of Strategy

我猜还有更多