博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Examples of GoF Design Patterns(GOF提及的设计模式在java中的例子)
阅读量:6256 次
发布时间:2019-06-22

本文共 5700 字,大约阅读时间需要 19 分钟。

参考文献

参考1:

参考2:

参考3:

正文

You can find an overview of a lot design patterns in . It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as much as possible pattern implementations found in both the Java SE and Java EE API's.

 (recognizeable by creational methods returning an abstract/interface type)

  •  (all getXXX() methods)
  •  (Returns singleton object per protocol)

 (recognizeable by creational methods returning the instance itself)

  •  (unsynchronized)
  •  (synchronized)
  •  (also on , , , , and )
  • All implementations of 

 (recognizeable by creational methods returning a concrete type)

  •  (overrideable in all subclasses)
  •  (also on , , , , , and )

 (recognizeable by creational methods returning a different instance of itself with the same properties)

  •  (the class has to implement )

 (recognizeable by creational methods returning the same instance (usually of itself) everytime)


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

  •  (returns a Reader)
  •  (returns a Writer)
  •  and 

 (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  and  methods however comes close.

 (recognizeable by behavioral methods taking an instance of same abstract/interface type)

  •  (also on , , ,,  and )
  •  (practically all over Swing thus)

 (recognizeable by creational methods taking an instance of same abstract/interface type)

  • All subclasses of , ,  and  have a constructor taking an instance of same type.
  • Almost all implementations of ,  and  have a constructor taking an instance of same type.
  • , the ,  and methods.
  •  and 

 (recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)

  • , it internally uses among others the abstract/interface types , ,  and many more without that the enduser has to worry about it (which are however overrideable by injection).
  • , which internally uses ,, , , etc.

 (recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)

  •  (also on , , , , , and )

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

  • , the whole API actually.

The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.


 (recognizeable by behavioral methods which (indirectly) invokes the same method inanother implementation of same abstract/interface type in a queue)

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

  • All implementations of 
  • All implementations of 

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

  • All subclasses of 
  • All subclasses of 

 (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)

  • All implementations of  (thus among others also !).
  • All implementations of 

 (recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)

  •  (all scheduleXXX() methods)
  •  (the invokeXXX() and submit() methods)
  •  (all scheduleXXX() methods)

 (recognizeable by behavioral methods which internally changes the state of the whole instance)

  •  (the setter methods do that, Date is internally represented by a long value)
  • All implementations of 
  • All implementations of 

 (recognizeable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state)

  • / (rarely used in real world though)
  • All implementations of  (practically all over Swing thus)

 (recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)

  •  (controlled by , the behaviour is dependent on current phase (state) of JSF lifecycle)

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

  • , executed by among others Collections#sort().
  • , the service() and all doXXX() methods takeHttpServletRequest and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).

 (recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)

  • All non-abstract methods of , , and .
  • All non-abstract methods of ,  and.
  • , all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.

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

  •  and 
  •  and 
  •  and 
本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2012/04/11/2442848.html,如需转载请自行联系原作者
你可能感兴趣的文章
struts2 Preparable接口
查看>>
hdu4578(线段树)
查看>>
写一个脚本简单检测局域网存活的机器
查看>>
Dubbo
查看>>
angular与jquery 进行json提交数据,报文头格式不一致的解决方案
查看>>
更换笔记本内存:自己动手修电脑(一)
查看>>
POJ2262-Goldbach's Conjecture
查看>>
区分扫描枪输入和键盘输入的实现
查看>>
【ssh服务配置】
查看>>
【mongdb主从复制和同步】
查看>>
下载文件downloadFile
查看>>
课后作业-阅读任务-阅读笔记-3
查看>>
hdoj1078(介绍记忆化搜索及其模板)
查看>>
cf-Round542-Div2-B(贪心)
查看>>
有关Python的PIL库的学习体会和实例
查看>>
日志挖掘(logminer)
查看>>
LaTeX技巧005:定制自己炫酷的章节样式实例
查看>>
LeetCode解题思路:27. Remove Element
查看>>
CCF NOI1138 高精度加法
查看>>
构造函数私有方法和公有方法
查看>>