协慌网

登录 贡献 社区

Java 核心库中的 GoF 设计模式示例

我正在学习 GoF Java 设计模式,我想看看其中的一些实际示例。 Java 核心库中的这些设计模式有哪些很好的例子?

答案

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 patterns

Abstract factory (recognizeable by creational methods returning the factory itself which in turn can be used to create another abstract/interface type)

Builder (recognizeable by creational methods returning the instance itself)

Factory method (recognizeable by creational methods returning an implementation of an abstract/interface 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)

  • 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, but uses them. The java.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

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

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

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)


Behavioral patterns

Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method in another 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 defined by an abstract type)

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

  1. 整个摆动过程中的观察者模式( ObservableObserver
  2. MVC 也如火如荼
  3. 适配器模式:InputStreamReader 和 OutputStreamWriter 注: ContainerAdapterComponentAdapterFocusAdapterKeyAdapterMouseAdapter不是适配器;它们是适配器。它们实际上是空对象。 Sun 的命名选择不佳。
  4. 装饰器模式( BufferedInputStream可以装饰其他流,例如FilterInputStream
  5. AWT 工具包和 Swing 可插入外观类的 AbstractFactory 模式
  6. java.lang.Runtime#getRuntime()是 Singleton
  7. 调解人模式的ButtonGroup
  8. ActionAbstractAction可以用于不同的视觉表示来执行相同的代码 -> 命令模式
  9. 用于 Flyweight 模式的 JTable 中的内部字符串或 CellRender(也请考虑各种池 - 线程池,连接池,EJB 对象池 - Flyweight 实际上是关于共享资源的管理)
  10. Java 1.0 事件模型是 “责任链” 的一个示例,Servlet 过滤器也是如此。
  11. Collections Framework 中的迭代器模式
  12. AWT / Swing 中的嵌套容器使用 Composite 模式
  13. AWT / Swing 中的布局管理器是策略的一个示例

我猜还有更多

  1. Flyweight与 Byte,Short,Integer,Long 和 String 的某些值一起使用。
  2. 外观已在许多地方使用,但最明显的是脚本接口。
  3. Singleton - 我想到了 java.lang.Runtime。
  4. 抽象工厂- 脚本和 JDBC API。
  5. 命令-TextComponent 的撤消 / 重做。
  6. 解释器-RegEx(java.util.regex。 )和 SQL(java.sql。 )API。
  7. 原型- 不确定是否 100%可以计数,但是我认为clone()方法可以用于此目的。