Java中应用的的设计模式 - 结构模式
Structural patternsAdapter?(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)java.io.InputStreamReader(InputStream)?(returns a?Reader)java.io.OutputStreamWriter(OutputStream)?(returns a?Writer)javax.xml.bind.annotation.adapters.XmlAdapter#marshal()?and?#unmarshal()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)java.util.Map#putAll(Map)java.util.List#addAll(Collection)java.util.Set#addAll(Collection)java.nio.ByteBuffer#put(ByteBuffer)?(also on?CharBuffer,?ShortBuffer,?IntBuffer,LongBuffer,?FloatBuffer?and?DoubleBuffer)java.awt.Container#add(Component)?(practically all over Swing thus)Decorator?(recognizeable by creational methods taking an instance of?same?abstract/interface type)- All subclasses of?
java.io.InputStream,?OutputStream,?Reader?and?Writer?have a constructor taking an instance of same type. - Almost all implementations of?
java.util.List,?Set?and?Map?have a constructor taking an instance of same type. java.util.Collections, the?checkedXXX(),?synchronizedXXX()?and?unmodifiableXXX()methods.javax.servlet.http.HttpServletRequestWrapper?and?HttpServletResponseWrapperFacade?(recognizeable by behavioral methods which internally uses instances of?different?independent abstract/interface types)javax.faces.webapp.FacesServlet, it internally uses under each the abstract/interface typesServletContext,?LifeCycle,?ViewHandler,?NavigationHandler?and many more without that the enduser has to worry about it (which are however overrideable by injection).Flyweight?(recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)java.lang.Integer#valueOf(int)?(also on?Boolean,?Byte,?Character,?Short,?Long,?Floatand?Double)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)java.lang.reflect.Proxyjava.rmi.*, 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.
java.io.InputStreamReader(InputStream)?(returns a?Reader)java.io.OutputStreamWriter(OutputStream)?(returns a?Writer)javax.xml.bind.annotation.adapters.XmlAdapter#marshal()?and?#unmarshal()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)
java.util.Map#putAll(Map)java.util.List#addAll(Collection)java.util.Set#addAll(Collection)java.nio.ByteBuffer#put(ByteBuffer)?(also on?CharBuffer,?ShortBuffer,?IntBuffer,LongBuffer,?FloatBuffer?and?DoubleBuffer)java.awt.Container#add(Component)?(practically all over Swing thus)Decorator?(recognizeable by creational methods taking an instance of?same?abstract/interface type)
- All subclasses of?
java.io.InputStream,?OutputStream,?Reader?and?Writer?have a constructor taking an instance of same type. - Almost all implementations of?
java.util.List,?Set?and?Map?have a constructor taking an instance of same type. java.util.Collections, the?checkedXXX(),?synchronizedXXX()?and?unmodifiableXXX()methods.javax.servlet.http.HttpServletRequestWrapper?and?HttpServletResponseWrapperFacade?(recognizeable by behavioral methods which internally uses instances of?different?independent abstract/interface types)
javax.faces.webapp.FacesServlet, it internally uses under each the abstract/interface typesServletContext,?LifeCycle,?ViewHandler,?NavigationHandler?and many more without that the enduser has to worry about it (which are however overrideable by injection).Flyweight?(recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)
java.lang.Integer#valueOf(int)?(also on?Boolean,?Byte,?Character,?Short,?Long,?Floatand?Double)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)
java.lang.reflect.Proxyjava.rmi.*, 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.
- All subclasses of?
- None comes to mind yet. A fictive example would be?