Several ways to enhance a method in a Java class

Source: Internet
Author: User

 

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.