Design Pattern-iterator pattern and combination pattern

Source: Internet
Author: User

1. The iterator mode provides the same functions as the iterator. It encapsulates sequential traversal of objects, because the storage methods of the object set of the subclass may be different. However, the stored objects are consistent.

Public classitem {..... // various attributes} public class set1 {item [] items ;... public iterator createiterator () {return New itemiterator (items) ;}} public class set2 {arraylist items ;... public iterator createiterator () {return items. iterator () ;}} public class itemiterator implements iterator {item [] items; int position; Public itemiterator (item [] items) {This. items = items; position = 0;} public object next () {item I = items [position]; position = Position + 1; return I;} public Boolean hasnext () {If (position> = items. lenght | items [position] = NULL) return false; else return true ;}}

In this way, we can access the array through an iterator defined by ourselves, and traverse the item objects in set1 and set2 with different storage structures in sequence through the createiterator method. So the iterator mode is:

Iterator mode: provides a method to access various elements in an aggregate object sequentially without exposing its internal representation.

2. Combination Mode

The combination mode is actually a relationship between the whole and the part. It is actually a tree structure. The root node is composed of its child nodes, and the child node has its own child nodes, therefore, the combination mode is defined as follows:

Combination Mode: allows you to combine objects into a tree structure to express the "whole/part" hierarchy. combination allows you to process individual objects and object combinations in a consistent way.

Now we need to traverse the object in the combination mode. What should we do?

Public abstract class itemcomponent {.... // method public void Method1 () {}} public classitem extends itemcomponent {..... // various attributes public void Method1 () {system. out. println ("leaf") ;}} public class set1 extends itemcomponent {item [] items ;... public iterator () {return New itemiterator (items);} public void Method1 () {system. out. println ("not leaf"); iterator = itemcomponent. iterator (); // rewrite while (iterator. hasnext () {itemcomponent t = (itemcomponent) iterator. next (); T. method1 () ;}} public class set2 extends itemcomponent {arraylist items ;... public iterator () {return items. iterator ();} public void Method1 () {system. out. println ("not leaf"); iterator = itemcomponent. iterator (); // rewrite while (iterator. hasnext () {itemcomponent t = (itemcomponent) iterator. next (); T. method1 ();}}}

The above method is a recursive method to achieve full traversal.

public class ItemIterator implements Iterator{    stack stack = new Stack();    public ItemIterator(Iterator iter)    {        stack.push(iter);    }     public Object next()    {        if (hasNext()){            Iterator iterator=(Iterator)stack.peek();            ItemComponent t=(ItemComponent) iterator.next();            if (t instanceof Set1)            {                stack.push(t.createIterator());                }            return t;        }else{            return null;        }    }    public boolean hasNext()    {        if (stack.empty()){            return false;        }else{            Iterator iterator=(Iterator) stack.peek();            if (!iterator.hasNext()){                stack.pop();                return hasNext();            }else {                return true;            }        }    }}

The method defined now is a method that uses the iterator to simulate recursion with stacks.

3. Design Model-single responsibility

Single responsibility: one class should have only one cause of change.

Design Pattern-iterator pattern and combination pattern

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.