参考文献
参考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)
(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)
本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2012/04/11/2442848.html,如需转载请自行联系原作者