Iterative sub-patterns of design patterns

Source: Internet
Author: User

Iterative sub-patterns of design patterns

The iterative sub-pattern, also called the cursor pattern, is the behavior pattern of the object. Iterative sub-patterns can sequentially access an element in a cluster without exposing the aggregated interior representation-Dr. Shanhong's Java and patterns

Example: Traversal of a collection first version: Implementing array collections and linked list collections

Collection interface

 Public Interface mycollection {    void  Add (Object o);     int size ();}
View Code

Array Collection

 Public classmyArrayListImplementsmycollection {PrivateObject[] Objects =NewOBJECT[10];//Initial Container size    Private intPosition = 0;//cursor (the position of the next insertion element)@Override Public voidAdd (Object o) {if(Position = = Objects.length) {//if the container is full, expand the containerobject[] Newobjects =NewObject[objects.length * 2]; System.arraycopy (Objects,0, newobjects, 0, objects.length); Objects=newobjects; } Objects[position]=o; Position++; } @Override Public intsize () {returnposition; }}
View Code

Linked List Collection

 Public classMylinkedlistImplementsmycollection {PrivateNode head =NULL; PrivateNode tail =NULL; Private intSize = 0; @Override Public voidAdd (Object o) {node node=NewNode (O,NULL); if(Head = =NULL) {//first oneHead =node; Tail=node; } Else{tail.setnext (node); Tail=node; } size++; } @Override Public intsize () {returnsize; }        classNode {PrivateObject data; PrivateNode Next;  Publicnode (Object data, node next) { This. data =data;  This. Next =Next; }         PublicObject GetData () {returndata; }         Public voidsetData (Object data) { This. data =data; }         PublicNode GetNext () {returnNext; }         Public voidSetnext (Node next) { This. Next =Next; }    }}
View Code

OK, we found that myarraylist and Mylinkedlist have implemented the MyCollection interface, but the implementation method is different, resulting in a different way of traversal, in order to unify the way can only use an abstract class/interface Way

Second Edition: Unified Traversal method

Adding a unified traversal interface

 Public Interface myiterator {    Object next ();     Boolean hasnext ();}
View Code

Let all the collection classes implement the interface (as long as the collection class that implements the MyCollection interface we can call its iterator () method to traverse its elements)

 Public Interface mycollection {    void  Add (Object o);     int size ();    Myiterator iterator ();}
View Code

It's easy to go through the collection at this point.

 while (Iterator.hasnex ()) {    = iterator.next ();}
View Code

Start implementing Myiterator-Array collection below

 Public classmyArrayListImplementsmycollection {PrivateObject[] Objects =NewOBJECT[10];//Initial Container size    Private intPosition = 0;//cursor (the position of the next insertion element)@Override Public voidAdd (Object o) {if(Position = = Objects.length) {//if the container is full, expand the containerobject[] Newobjects =NewObject[objects.length * 2]; System.arraycopy (Objects,0, newobjects, 0, objects.length); Objects=newobjects; } Objects[position]=o; Position++; } @Override Public intsize () {returnposition; } @Override PublicMyiterator iterator () {return NewMyiterator () {intindex = 0; @Override PublicObject Next () {returnObjects[index + +]; } @Override Public BooleanHasnext () {//if (Index < objects.length)//low-level error                if(Index <position)return true; return false;    }        }; }}
View Code

Linked List Collection

 Public classMylinkedlistImplementsmycollection {PrivateNode head =NULL; PrivateNode tail =NULL; Private intSize = 0; @Override Public voidAdd (Object o) {node node=NewNode (O,NULL); if(Head = =NULL) {//first oneHead =node; Tail=node; } Else{tail.setnext (node); Tail=node; } size++; } @Override Public intsize () {returnsize; }        classNode {PrivateObject data; PrivateNode Next;  Publicnode (Object data, node next) { This. data =data;  This. Next =Next; }         PublicObject GetData () {returndata; }         Public voidsetData (Object data) { This. data =data; }         PublicNode GetNext () {returnNext; }         Public voidSetnext (Node next) { This. Next =Next; }} @Override PublicMyiterator iterator () {return NewMyiterator () {Node Currnode=Head; @Override Publicobject Next () {object o=Currnode.getdata (); Currnode=Currnode.getnext (); returno; } @Override Public BooleanHasnext () {returnCurrnode! =NULL;    }        }; }}
View Code

Test

 Public classTest { Public Static voidMain (string[] args) {mycollection Al=Newmyarraylist (); mycollection ll=Newmylinkedlist ();  for(inti=0; i<15; i++) {al.add (i);        Ll.add (i); } Myiterator iterator= Ll.iterator ();//= Al.iterator ();         while(Iterator.hasnext ()) {System.out.print (Iterator.next () )+ " "); }    }}
View Code

Ok, the above is the Cottage Beggar version jdk ArrayList and LinkedList

Iterative sub-patterns of 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.