* One way: the way of inheritance .
* inheritance can only be used when it is possible to control the construction of this class .
Connection is an interface that implements classes that are not deterministic ( provided by each vendor ) and cannot use this method
* two ways: decorator mode .
* Both the wrapper object and the wrapped object are to implement the same interface .
* The Wrapper object needs to get a reference to the wrapped object .
Disadvantage: If the interface method is more , enhance one of the methods . other functions of the method require the original call .
* three ways: dynamic proxy mode .
* The Enhanced object implementation interface is available .
"case of the successor and decorator"
/**
* inherited ways to enhance a method in a class :
*/
Class man{
public void Run () {
System.out.println (" Run .... ");
}
}
Class Superman extends man{
public void Run () {
Super.run ();
System.out.println (" Fly ....");
}
}
/**
* enhancements to the method of completing the class using the Decorator's way
*/
Interface waiter{
public void Server ();
}
Class Waiteress implements waiter{
@Override
public void Server () {
System.out.println (" service ...");
}
}
Class Waiteresswrapper implements waiter{
Private Waiter waiter;
Public Waiteresswrapper (waiter Waiter) {
This.waiter = waiter;
}
@Override
public void Server () {
System.out.println (" Smile ...");
This.waiter.server ();
}
}
"use decorator mode to enhance Connection 's close method "
public class MyConnection implements connection{
Private Connection conn;
Private List<connection> List;
Public myconnection (Connection conn,list<connection> List) {
This.conn = conn;
This.list = list;
}
@Override
public void Close () throws SQLException {
List.add (conn);
}
...
}
Getconnection method for connection pooling :
@Override
how to get connected:
Public Connection getconnection () throws SQLException {
If (list.size () <= 0) {
For (int I=1;i<=3;i++) {
Connection conn = Jdbcutils.getconnection ();
List.add (conn);
}
}
Connection conn = List.remove (0);
MyConnection myconn = new MyConnection (conn, list);
Return myconn;
}
Several ways to enhance a method in a Java class