我正在学习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
我猜还有更多
其他回答
抽象工厂模式被用在很多地方。 例如,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
我猜还有更多
尽管我对这一点有点不熟悉,但Java XML API经常使用Factory。我是说看看这个:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
String title = XPathFactory.newInstance().newXPath().evaluate("//title", doc);
...诸如此类。
此外,各种缓冲区(StringBuffer, ByteBuffer, StringBuilder)使用Builder。
Flyweight用于字节,短,整数,长和字符串的一些值。 Facade被用在很多地方,但最明显的是脚本接口。 单例-想到java.lang.Runtime。 抽象工厂-也脚本和JDBC API。 命令- TextComponent的撤销/重做。 解释器- RegEx (java.util.regex.)和SQL (java.sql.)API。 原型-不是100%确定这是否算数,但我认为clone()方法可以用于此目的。
RMI是基于Proxy的。
应该可以为GoF中的23种模式中的大多数引用一个:
Abstract Factory: java.sql interfaces all get their concrete implementations from JDBC JAR when driver is registered. Builder: java.lang.StringBuilder. Factory Method: XML factories, among others. Prototype: Maybe clone(), but I'm not sure I'm buying that. Singleton: java.lang.System Adapter: Adapter classes in java.awt.event, e.g., WindowAdapter. Bridge: Collection classes in java.util. List implemented by ArrayList. Composite: java.awt. java.awt.Component + java.awt.Container Decorator: All over the java.io package. Facade: ExternalContext behaves as a facade for performing cookie, session scope and similar operations. Flyweight: Integer, Character, etc. Proxy: java.rmi package Chain of Responsibility: Servlet filters Command: Swing menu items Interpreter: No directly in JDK, but JavaCC certainly uses this. Iterator: java.util.Iterator interface; can't be clearer than that. Mediator: JMS? Memento: Observer: java.util.Observer/Observable (badly done, though) State: Strategy: Template: Visitor:
在这23个例子中,我想不出10个是用Java写的,但我会看看明天是否能做得更好。这就是编辑的作用。
推荐文章
- 如何添加JTable在JPanel与空布局?
- Statement和PreparedStatement的区别
- 为什么不能在Java中扩展注释?
- 在Java中使用UUID的最重要位的碰撞可能性
- PHP中接口的意义是什么?
- 转换列表的最佳方法:map还是foreach?
- 虚拟方法和抽象方法的区别
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?