Java design mode adapter mode (Adapter pattern)

Source: Internet
Author: User
Tags throw exception

The function of Adapter pattern is to convert the interface without changing the function. Adapter is divided into two categories, one is Object Adapter, and the other is class Adapter. Because the implementation of class Adapter requires multiple inheritance, and Java does not support multiple inheritance, it is only concerned with object Adapter.


There is no Java.util.Iterator interface before JDK1.5, the Java.util.Enumeration interface plays a Iterator role. Suppose, then, that we need to maintain some of the older codes, we may face an Iterator dilemma. This is where Adapter Pattern comes in handy. Here are two examples of how to "convert" enumeration interfaces to Iterator interfaces with Adapter Pattern.


Old code to adapt to the new code: Convert enumeration to iterator check the API documentation, found StringTokenizer this class to implement the enumeration interface, then take this for example. Now there is a need to "traverse" the StringTokenizer in a Iterator way, but the StringTokenizer class does not implement the Iterator interface. So we need to write a Adapter to convert the enumeration interface into a Iterator interface.


Class Iteratoradapter implements Iterator {Private enumeration Enume;//Save Enumeration interface Implementation Classes public Iteratoradapter (Enume Ration enume) {this.enume = Enume;} /** * Enumeartion's hasMoreElements () method functions similarly to the Hasnext () method of iterator, calling */@Overridepublic Boolean hasnext () {return Enume.hasmoreelements ();} The next () method of the/** * enumeration is the same as the next () method of iterator, which calls directly/@Overridepublic Object next () {return enume.nextelement ();} /** * Because there is no such method in the enumeration interface, throw exception */@Overridepublic void Remove () {throw new Unsupportedoperationexception ()}}

We can see that we are actually entrusting the implementation of the enumeration interface to the implementation of the method in the Iterator interface that is not implemented. The usage of Adapter is as follows:
StringTokenizer st = new StringTokenizer ("a b c D E F g");//Create adapter object, pass StringTokenizer object in Iteratoradapter strtokenadap ter = new Iteratoradapter (ST);//This is the time to traverse an object that does not implement the iterator interface in a iterator way (Strtokenadapter.hasnext ()) { Out.println (Strtokenadapter.next ());}


The new code adapts to the old code: converting Iterator to enumeration is very easy, 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 ();}}


As above Iteratoradapter and Enumerationadapter, the way to save the object to be adapted to the class's member variable is called Object Adapter. There is also a class Adapter that requires the Adapter class to inherit the appropriate two parties at the same time. For example, there is a class named Apple and a class named Banana, and their public methods are different. Now that you want to use the Banana class in a way that calls the Apple class method, for Class Adapter, you should let Adapter inherit Apple and Banana at the same time, and then call through the Adapter completion method. If you are interested, you can try it out in C + +.

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.