我正在学习GoF Java设计模式,我想看到一些现实生活中的例子。Java核心库中有哪些设计模式的好例子?
当前回答
抽象工厂模式被用在很多地方。 例如,DatagramSocketImplFactory, PreferencesFactory。还有很多——在Javadoc中搜索名称中包含“Factory”的接口。
同样,工厂模式也有相当多的实例。
其他回答
工厂方法
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中描述的结构不同)。
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写的,但我会看看明天是否能做得更好。这就是编辑的作用。
Flyweight用于字节,短,整数,长和字符串的一些值。 Facade被用在很多地方,但最明显的是脚本接口。 单例-想到java.lang.Runtime。 抽象工厂-也脚本和JDBC API。 命令- TextComponent的撤销/重做。 解释器- RegEx (java.util.regex.)和SQL (java.sql.)API。 原型-不是100%确定这是否算数,但我认为clone()方法可以用于此目的。
抽象工厂模式被用在很多地方。 例如,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 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap