Wednesday, April 13, 2016

Design Patters example in JDK

Swing
EventListener: Observer
BorderFactory: Factory
Look and feel: Abstract Factory


1. Creational Patterns
Factory
BorderFactory.createLineBorder
BorderFactory.createTitledBorder
Factory Method
Calendar.getInstance()
Abstract Factory
SWT control (or JDK look & feel)
IGUIFactory (WinFactory, OSXFactory ...)
IButton (WinButton, OSXButton ...)
if(Win)
createWinButton
Builder (too many parameters)
StringBuffer.append
StringBuilder.append
ProtoType (reduce the class number)
Object.clone() (Cloneable)
Singleton
Runtime.getRuntime()
lazy initialization
eager initialization
static block initialization
static class holder
enum way
NullObject

2. Structural Patterns (Structural patterns are concerned with how classes and objects arecomposed to
form larger structures.Structural class patterns use inheritance to compose
interfacesor implementations.)
Adapter (Convert the interface of a class into another interface clients expect)
InputStreamReader(InputStream): returns a Reader
Arrays.asList
Bridge (TODO):
Composite (Compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of object uniformly.)
Container.add: return Component
Decorator (Attach additional responsibilities to an object dynamically. Decorators provide
a flexible alternative to subclassing for extending functionality.):
1. All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
2. java.util.Collections, the synchronizedXXX() and unmodifiableXXX() methods.
Facade
My example: ACUtils.getResult(String queryStr) return List<String>
Flyweight
Integer.valueOf, Boolean.valueOf
proxy (Provide a surrogate or placeholder for another object to control access to it.)
java.lang.reflect.Proxy (InvocationHandler.newInstance())
rmi/web service

3. Behavioral Patterns (Behavioral patterns are concerned with algorithms and theassignment of
responsibilities between objects. Behavioral patternsdescribe not just patterns
of objects or classes but also the patternsof communication between them.)
Chain of Responsibility (giving morethan one object a chance to handle the request.)
java exception handling, set the processor request
Command (the toolkit can't implement the request
explicitly in the buttonor menu, because only applications that use the toolkit
know whatshould be done on which object.)
java.lang.Runnable
javax.swing.Action
Interpreter
java.text.Format
java.util.Patterns
JavaCC
Iterator
Iterator (has remove method and has a shorter/better method name)
Enumeration
Mediator
Executor.execute
Momento (capture and externalize an object'sinternal state so that the object can be restored to this state later.)
All implementations of java.io.Serializable
xml marshal/unmarshal
Observer
EventListner
State(TODO)
Strategy:
Comparator.compare
Template method
All non-abstract methods of java.util.AbstractList or other class
Visitor
SimpleFileVisitor

No comments:

Post a Comment