設計模式-迭代器模式和組合模式

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   ar   div   sp   log   

1.迭代器模式完成的功能是和迭代器一樣的,封裝了對對象的順序遍曆,因為子類的對象集合的儲存方式可能並不一致。但是儲存的對象是一致的。

public classItem{    .....//各種屬性}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;    }}

通過這種方法,我們就可以通過自己定義的一個迭代器來訪問數組,同時通過createIterator的方法來順序遍曆儲存結構不一樣的Set1和Set2中item對象。所以迭代器模式就是:

迭代器模式:提供一種方法順序訪問一個彙總對象中的各種元素,而又不暴露其內部的表示。

2.組合模式

組合模式其實就是整體與部分的一個關係,其實就是一個樹形結構,根節點由他的子節點組合而成,而子節點又有自己的子節點來組合而成,所以組合模式的定義如下:

組合模式:允許你將對象組合成樹形結構來表現“整體/部分”的階層,組合能讓客戶以一致的方法處理個別對象以及對象組合。

現在我們需要對組合模式下的對象這棵進行遍曆怎麼辦?

public abstract class ItemComponent{    ....//方法    public void method1(){    }}public classItem extends ItemComponent{    .....//各種屬性    public void method1()    {        System.out.println("leaf");    }    }public class Set1 extends ItemComponent{    item[] items;    ...    public Iterator Iterator(){        return new ItemIterator(items);    }    public void method1()    {        System.out.println("not leaf");        Iterator iterator=itemComponent.iterator();//重寫        while (iterator.hasNext())        {            ItemComponent t=(ItemComponent) iterator.next();            t.method1();        }    }}public class Set2 extends ItemComponent{    ArrayList items;    ...    public Iterator Iterator(){        return  items.iterator();    }        public void method1()    {        System.out.println("not leaf");        Iterator iterator=itemComponent.iterator();//重寫        while (iterator.hasNext())        {            ItemComponent t=(ItemComponent) iterator.next();            t.method1();        }    }    }

上面這種方式是一種通過遞迴的方式來實現全部遍曆的

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;            }        }    }}

現在定義的這種方式,是通過迭代器用棧類比遞迴的一種方式。

3。設計模式-職責單一

單一責任: 一個類應該只有一個引起變化的原因。

設計模式-迭代器模式和組合模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.