Associating a Java adapter pattern with an upgrade from a collection class

Source: Internet
Author: User
Tags stub

Historical Collection Classes (before JDK1.1)
The containers provided are arrays,vector,stack,hashtable,properties,bitset. It defines a standard way of visiting elements within a cluster, called the enumeration interface, as follows:

Vector v=new vector ();
For (enumeration enum =v.elements (); enum.hasmoreelements ();) {
Object o = enum.nextelement ();
Processobject (o);
}
In the JDK1.2 version, the iterator interface was introduced, and the new version of the collection object (Hashset,hashmap,weakheahmap,arraylist,treeset,treemap, LinkedList) is a collection element accessed through the iterator interface.
For example: List list=new ArrayList ();
For (iterator It=list.iterator (); It.hasnext ();)
{
System.out.println (It.next ());
}

This will make an error if you run the old version of the program on the new Java compiler. Because there is no elements () in the list interface, there is only iterator (). So how can the old version of the program run on the new Java compiler? If not modified, it is certainly not, but the change to follow the "open-closed" principle.
This is when I think of the adapter pattern in Java design mode.

/*
* @author I set the date for J Mad 2007-4-18
*
*/
Package net.blogjava.lzqdiy;

Import java.util.ArrayList;
Import java.util.Enumeration;
Import Java.util.Iterator;
Import java.util.List;

Public class Newenumeration implements enumeration
{

Iterator it;
Public Newenumeration (iterator it)
{
This.it=it;
TODO auto-generated Constructor stub
}

public boolean hasmoreelements ()
{
TODO auto-generated Method Stub
return It.hasnext ();
}

Public Object nextelement ()
{
TODO auto-generated Method Stub
return It.next ();
}
public static void Main (string[] args)
{
List list=new ArrayList ();
List.add ("a");
List.add ("B");
List.add ("C");
For (enumeration E=new newenumeration (List.iterator ()); E.hasmoreelements ();)
{
System.out.println (E.nextelement ());
}
}
}
Newenumeration is an adapter class that enables adaptation from the iterator interface to the enumeration interface, so that we can use the old version of the code to use the new collection object.

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.