Adapter Pattern in Java Design Mode)

Source: Internet
Author: User

The function of Adapter Pattern is to convert the interface without changing the function. There are two types of adapters: Object Adapter and Class Adapter. Because the implementation of the Class Adapter requires multiple inheritance, and Java does not support multiple inheritance, only the Object Adapter is concerned here.


Java. util. Iterator interfaces were not available before JDK1.5, and java. util. Enumeration interfaces play the role of Iterator. If we need to maintain code that has been around for a long time, we may face the dilemma of having no Iterator. At this time, Adapter Pattern will be used. The following two examples show how to use Adapter Pattern to "convert" the Enumeration interface and the Iterator interface ".


The old code adapts to the new Code: Convert Enumeration to Iterator. Check the API documentation and find that the StringTokenizer class implements the Enumeration interface. Let's take this example. Now you need to use Iterator to "traverse" StringTokenizer, but the StringTokenizer class does not implement the Iterator interface. Therefore, we need to write an Adapter to convert the Enumeration interface to the Iterator interface.
Class IteratorAdapter implements Iterator {private Enumeration enume; // Save the Enumeration interface implementation class public IteratorAdapter (Enumeration enume) {this. enume = enume;}/*** the hasMoreElements () method of Enumeartion has the same function as the hasNext () method of Iterator. Call */@ Overridepublic boolean hasNext () {return enume. hasMoreElements ();}/*** the next () method of Enumeration has the same function as the next () method of Iterator. Call */@ Overridepublic Object next () {return enume directly. nextElement ();}/*** because this method is not available in the Enumeration interface, an exception is thrown */@ Overridepublic void remove () {throw new UnsupportedOperationException ();}}

We can see that we only delegate the methods in the unimplemented Iterator interface to the implementation of the Enumeration interface. Use the Adapter as follows:
StringTokenizer st = new StringTokenizer ("a B c d e f g"); // create an adapter object and pass the StringTokenizer object to IteratorAdapter strTokenAdapter = new IteratorAdapter (st ); // at this time, you can use the Iterator method to traverse objects that do not implement the Iterator interface. hasNext () {out. println (strTokenAdapter. next ());}


The new Code adapts to the old code: it is very easy to convert Iterator into Enumeration, because the methods in the Enumeration interface are defined in Iterator.
class EnumerationAdapter implements Enumeration {private Iterator it;public EnumerationAdapter(Iterator it) {this.it = it;}@Overridepublic boolean hasMoreElements() {return it.hasNext();}@Overridepublic Object nextElement() {return it.next();}}


Like the IteratorAdapter and EnumerationAdapter above, the way to save the Object to be adapted to the member variable of the class is called the Object Adapter. The other Class Adapter requires the Adapter Class to inherit the adaptation parties at the same time. For example, there is a class named Apple and a class named Banana, and their public methods are different. If you want to use the Banana Class by calling the Apple Class method, you should let the Adapter inherit both Apple and Banana for the Class Adapter, and then call the method through the Adapter. If you are interested, use C ++ to implement it.

Related Article

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.