Monday, May 8, 2017

JDK design pattern

First, the design pattern is what (1) the problem of repeated solutions (2) to enhance the flexibility of the software (3) to adapt to the changing software

(1) learn from the excellent code design, help to improve the code design capabilities (2) JDK design reflects the majority of the design pattern is to learn the design pattern of a better way (3 ) Can be more in-depth understanding of JDK

Third, the relationship between class inheritance, commission, dependency, aggregation, combination


Fourth, the introduction of the way (1) Role: to summarize a design pattern of the basic points (2) JDK embodied: a design pattern in the JDK is how to reflect the (3) class diagram: a design pattern in the JDK corresponding to the class Figure

Fifth, the classic design patterns in the implementation of the JDK
1.Singleton (singleton)
Role: Guaranteed class has only one instance; provide a global access point
JDK embodies:
(1) Runtime
(2) NumberFormat
Class Diagram:


2.Factory (static factory)
effect:
(1) instead of the constructor to create the object (2) method name than the constructor function is clear
JDK embodies:
(1) Integer.valueOf
(2) Class.forName
Class Diagram:


3.Factory Method (factory method)
Role: Subclass determines which class is instantiated
JDK embodied: Collection.iterator method class diagram:


4.Abstract Factory (abstract factory)
Role: Create a class of objects
JDK embodies:
(1) java.sql package (2) UIManager (swing appearance)
Class Diagram:


5.Builder (constructor)
effect:
(1) the construction logic mentioned in a separate class (2) separation of the class structure logic and performance
JDK embodied: DocumentBuilder (org.w3c.dom)
Class Diagram:


6.Prototype (prototype)
effect:
(1) copy object (2) shallow copy, deep copy
JDK embodied: Object.clone; Cloneable
Class Diagram:

7.Adapter (adapter)
Function: Make incompatible interface compatible
JDK embodies:
(1) java.io.InputStreamReader (InputStream)
(2) java.io.OutputStreamWriter (OutputStream)
Class Diagram:


8.Bridge (bridge)
The role: the abstract part and its realization part of the separation, so that they can be independent changes
JDK embodies: Handler and Formatter in java.util.logging
Class Diagram:

9.Composite (combination)
Role: Consistent treatment of composite objects and independent objects
JDK embodies:
(1) org.w3c.dom
(2) javax.swing.JComponent # add (Component)
Class Diagram:


10.Decorator (decorator)
Role: add new features to the class; prevent class inheritance from the explosive growth
JDK embodies:
(1) java.io package (2) java.util.Collections # synchronizedList (List)
Class Diagram:

11.Façade (appearance)
effect:
(1) encapsulates a set of interactive classes, consistently providing an interface (2) encapsulating subsystems, simplifying subsystem calls
JDK embodied: java.util.logging package class diagram:

12.Flyweight
Role: shared object, saving memory
JDK embodies:
(1) Integer.valueOf (int i); Character.valueOf (char c)
(2) String constant pool diagram:

14.Proxy (proxy)
effect:
(1) transparent call by the proxy object, without having to know the details of the complex implementation (2) to increase the function of the proxy class
JDK embodied: dynamic agent; RMI
Class Diagram:


15.Iterator (iterator)
Role: Separate the set of iterations and the collection itself
JDK embodied: Iterator, Enumeration interface class diagram:

16.Observer (observer)
Action: Notify the object state change
JDK embodies:
(1) java.util.Observer, Observable
(2) Listener in Swing
Class Diagram:

17.Mediator (coordinator)
Role: Used to coordinate the operation of multiple classes
JDK embodies: Swing's ButtonGroup
Class Diagram:

18.Template method (template method)
Role: the definition of the structure of the algorithm, sub-class only to achieve different parts
JDK embodied: ThreadPoolExecutor.Worker
Class Diagram:

19.Strategy (strategy)
Role: provide different algorithms
JDK in the expression: ThreadPoolExecutor in the four types of rejection strategy class:

20.Chain of Responsibility
Action: The request is handled by the object on the chain, but the client does not know which objects the request will be processed
JDK embodies:
(1) java.util.logging.Logger delegates log to parent logger
(2) ClassLoader delegate model class diagram:

21.Command (command)
effect:
(1) package operation, so that the interface is consistent (2) the caller and receiver in space and time on the decoupling
JDK embodied: Runnable; Callable; ThreadPoolExecutor
Class Diagram:

22. Null Object (empty object)
Function: do not need to empty each time, treat the null value, as to treat an object of the same interface
JDK embodied: Collections.EMPTY_LIST
Class Diagram:

23.State (state)
Role: the main object and its state separation, the state object is responsible for the state of the main object conversion, so that the main object class function
JDK reflected: no class map found:

24.Visitor (Visitor)
Role: heterogeneous class to add polymerization operations; collecting aggregated data
JDK in the embodiment: no class map:

25.Interpreter (interpreter)
Function: Use a set of classes to represent a rule
JDK embodied: java.util.regex.Pattern
Class diagram: four operations

26.Memento (memo)
Role: to maintain the object state, if necessary, can be restored
JDK reflected: no class map found:

Sixth, reference
1. Design Pattern (GoF)
2. Software Architecture Design Patterns in Java
3. JDK 5 Documentation
4. http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns
5. http://java.csdn.Net/a/20101129/282644.html

No comments:

Post a Comment