大話設計模式8 組合模式 迭代器模式

來源:互聯網
上載者:User

1.組合模式

將對象組合成樹形結構進行表示,使使用者對單個對象和組合對象的使用具有一致性。當使用者可以忽略組合對象與單個對象的不同,統一的使用組合結構中的所有對象時,可以考慮使用組合模式。

import java.util.ArrayList;abstract class Component{protected String mName;public Component(String name){mName=name;}public abstract void Add(Component c);public abstract void Remove(Component c);public abstract void Display(int depth);}class Leaf extends Component{public Leaf(String name){super(name);}@Overridepublic void Add(Component c) {// TODO Auto-generated method stub}@Overridepublic void Remove(Component c) {// TODO Auto-generated method stub}@Overridepublic void Display(int depth) {// TODO Auto-generated method stubfor(int i=0;i<depth;i++){System.out.print("-");}System.out.println("Leaf:"+mName);}}class Node extends Component{ArrayList<Component> list=new ArrayList();public Node(String name){super(name);}@Overridepublic void Add(Component c) {// TODO Auto-generated method stublist.add(c);}@Overridepublic void Remove(Component c) {// TODO Auto-generated method stublist.remove(c);}@Overridepublic void Display(int depth) {// TODO Auto-generated method stubfor(int i=0;i<depth;i++){System.out.print("-");}System.out.println("Node:"+mName);for(Component c:list){c.Display(depth+1);}}}public class Composite {public static void main(String[] args) {// TODO Auto-generated method stubNode root=new Node("Root");root.Add(new Leaf("LeafA"));root.Add(new Leaf("LeafB"));Node nodeA=new Node("NodeA");nodeA.Add(new Leaf("LeafC"));nodeA.Add(new Leaf("LeafD"));root.Add(nodeA);root.Add(new Node("NodeB"));root.Display(1);root.Remove(nodeA);root.Display(1);}}


2. 迭代器模式

當對一個存放任意對象的集合進行遍曆,或需要採用多種方式遍曆時,都可以考慮採用迭代器模式。

迭代器模式就是分離了對集合遍曆的行為,抽象出一個迭代器類來負責,這樣既隱藏了集合的內部結構,又使外部可以透明的訪問集合內部資料。

import java.util.ArrayList;abstract class Iterator{public abstract Object First();public abstract Object Next();}abstract class Aggregate{public abstract Iterator getIterator();}class ConcreteIterator extends Iterator{private ConcreteAggregate mConcreteAggregate;private int mCount=0;public ConcreteIterator(ConcreteAggregate concreteAggregate){mConcreteAggregate=concreteAggregate; }public Object First(){return mConcreteAggregate.Get(0);}public Object Next(){if(mCount<mConcreteAggregate.Size()){return mConcreteAggregate.Get(++mCount);}else{return null;}}}class ConcreteAggregate extends Aggregate{ArrayList<Object> list=new ArrayList();  public ConcreteAggregate(){list.add("a");list.add("b");list.add("c");}@Overridepublic Iterator getIterator() {// TODO Auto-generated method stubreturn new ConcreteIterator(this);}public Object Get(int index){if(index<list.size()&&index>-1){return list.get(index);}else{return null;}}public int Size(){return list.size();}}public class IteratorPattern {public static void main(String[] args) {// TODO Auto-generated method stubConcreteAggregate c=new ConcreteAggregate();Iterator i=c.getIterator();System.out.println("First:"+i.First());Object o=i.Next();while(null!=o){System.out.println("Next:"+o);o=i.Next();};}}

感覺迭代器模式就是在集合類裡定義一個返回Iterator對象的方法,而在Iterator類裡則擁有一個集合類的引用,以此,通過Iterator對象實現對集合對象的操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.