大话设计模式8 组合模式 迭代器模式

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);
	}
	
	@Override
	public void Add(Component c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Remove(Component c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Display(int depth) {
		// TODO Auto-generated method stub
		for(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);
	}
	
	@Override
	public void Add(Component c) {
		// TODO Auto-generated method stub
		list.add(c);
	}

	@Override
	public void Remove(Component c) {
		// TODO Auto-generated method stub
		list.remove(c);
	}

	@Override
	public void Display(int depth) {
		// TODO Auto-generated method stub
		for(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 stub
		Node 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");
	}
	
	@Override
	public Iterator getIterator() {
		// TODO Auto-generated method stub
		return 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 stub
		ConcreteAggregate 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对象实现对集合对象的操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值