Packaging patterns for Java design patterns

Source: Internet
Author: User

Sometimes the method of an object may not be the function we want, and we would like to overwrite this method. And for the overwrite, our most straightforward feeling is through the subclass inherits the way, but sometimes for the use of web development, we can know to get the object implementation interface, and the real object belongs to which class we do not know, for example, connection interface, HttpServletResponse interface like this, we don't know about third-party jar packages or other framework-specific implementation classes, so we can only enhance or overwrite an object's methods with a known interface.

There are generally three ways to enhance an object method:

⑴ inherits an implementation class using subclasses;

⑵ use packaging design mode;

⑶ uses dynamic proxies.

This article mainly explains the packaging design pattern, but to know that the dynamic agent is the best solution.

The packaging design pattern mainly has the following five steps:

① customizes a class that implements the same interface as the target object (the object being enhanced).

② defines a member variable in a custom class to remember (point to) the target object.

③ defines a constructor that is used to receive the target object.

④ overrides a method that you want to enhance or rewrite.

⑤ for other methods that you do not want to enhance or overwrite, use the member variable that you just remembered for the target object to one by one to invoke the target object's method.

As an example of a database connection connection interface, the connection object that we give to other methods in the database connection pool must not be a connection object that the database directly supplies, because its Close method destroys the connection directly. And what we should return is the connection object that calls the Close method to put the connection back in the pool, so we use a wrapper class to encapsulate the connection object provided by the database, overwrite its Close method, and, for other methods, use the original object directly to invoke it. The code is as follows:

1 classMyConnectionImplementsconnection{//class for packaging design Patterns2         PrivateConnection Conn;3          PublicMyConnection (Connection conn) {//receive target object4              This. conn =Conn;5         }6         7 @Override8          Public voidClose ()throwsSQLException {9Connectionlist.addfirst ( This. conn);//calling the Close method simply returns the connection back to the pool without destroyingTen     } One  A         @Override -          PublicStatement createstatement ()throwsSQLException { -              This. Conn.createstatement ();//for non-enhanced methods, you can invoke the target object's method, which is the case with other methods in the MyConnection wrapper class. the             return NULL; -     } -。。。//The following omit other methods that are covered in the connection interface, as shown above (Createstatement method) for methods that do not want to be enhanced -}//myconnection Packing class complete

Packaging patterns for Java design patterns

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.