Traverse elements in an aggregate object-iterator mode (3)

Source: Internet
Author: User
3. Complete Solution

To simplify the structure of the abstractobjectlist class and provide different traversal methods for different data collection classes, developers of Sunny software company used the iterator mode to reconstruct the design of the abstractobjectlist class, the data traversal structure of the restructured sales management system is shown in Figure 4:

Figure 4 Data traversal structure of the Sales Management System

(Note: to simplify the class diagram and code, only one specific aggregation class and specific iterator class are provided in this structure)

In Figure 4, abstractobjectlist acts as an abstract aggregation class, productlist acts as a specific aggregation class, abstractiterator acts as an abstract iterator, and productiterator acts as a specific iterator. The complete code is as follows:

// In this example, we do not use the built-in iterator in JDK to describe the implementation process of the custom iterator. In fact, JDK built-in iterator has implemented forward traversal of a list object import Java. util. *; // abstract aggregation class abstract class abstractobjectlist {protected list <Object> objects = new arraylist <Object> (); Public abstractobjectlist (List objects) {This. objects = objects;} public void addobject (Object OBJ) {This. objects. add (OBJ);} public void removeobject (Object OBJ) {This. objects. remove (OBJ);} public list getobjects () {return this. objects;} // declare the Abstract Factory method for creating an iterator object public abstract actiterator createiterator ();} // item data class: specific aggregation class productlist extends actobjectlist {public productlist (list products) {super (products) ;}// specific factory method for creating iterator objects public extends actiterator createiterator () {return New productiterator (this) ;}// abstract iterator interface extends actiterator {public void next (); // move to the next element public Boolean islast (); // determine whether it is the last element public void previous (); // move it to the previous element public Boolean isfirst (); // determine whether it is the first element public object getnextitem (); // obtain the next element public object getpreviusitem (); // obtain the previous element} // product iterator: Class productiterator implements extends actiterator {private productlist; private list products; private int cursor1; // defines a cursor to record the position of forward traversal. Private int cursor2; // defines a cursor to record the position of Public productiterator (productlist list) in reverse traversal) {This. productlist = List; this. products = List. getobjects (); // get the set object cursor1 = 0; // set the initial value of forward traversal cursor cursor2 = products. size ()-1; // set the initial value of the reverse traversal cursor} public void next () {If (cursor1 <products. size () {cursor1 ++;} public Boolean islast () {return (cursor1 = products. size ();} public void previous () {If (cursor2>-1) {cursor2 -- ;}} public Boolean isfirst () {return (cursor2 =-1);} public object getnextitem () {return products. get (cursor1);} public object getpreviusitem () {return products. get (cursor2 );}}

Write the following client test code:

Class client {public static void main (string ARGs []) {list products = new arraylist (); products. add ("Yi tianjian"); products. add ("tu longdao"); products. add ("trangula"); products. add ("sunflower Collection"); products. add ("Chapter 42"); abstractobjectlist list; abstractiterator iterator; List = new productlist (products); // create an aggregate object iterator = List. createiterator (); // create the iterator object system. out. println ("Forward traversal:"); While (! Iterator. islast () {system. out. print (iterator. getnextitem () + ","); iterator. next ();} system. out. println (); system. out. println ("-----------------------------"); system. out. println ("reverse traversal:"); While (! Iterator. isfirst () {system. Out. Print (iterator. getpreviusitem () + ","); iterator. Previous ();}}}

Compile and run the program. The output result is as follows:

Forward traversal:

Yi tianjian, tu longdao, heartbroken grass, sunflower collection, 42-Chapter Sutra,

-----------------------------

Reverse traversal:

The sutra of Chapter forty-two, the sunflower collection, the cut-up grass, the Dragon Sword, and the Yi tianjian,

If you need to add a new aggregation class, such as the customer data collection class, and provide the customer data collection class with forward traversal and reverse traversal operations different from the Product Data Collection class, you only need to add a new aggregation subclass and a new specific iterator class. The original class library code does not need to be modified and complies with the "open and closed principle". If you need to replace an iterator for the productlist class, you only need to add a new specific iterator class as a subclass of the abstract iterator class to implement the Traversal method again. The original iterator Code does not need to be modified and complies with the "open and closed principle "; however, to add a new method to the iterator, You need to modify the source code of the abstract iterator, which violates the "Open and Close principle ".

 

【Author】 Liu WeiHttp://blog.csdn.net/lovelion]

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.