Iterator and Composite Pattern

Iterator

Definition-Provide a way to access the elements of an aggregate object equentially without exposing its underlying representation.

Composite Pattern

Definition-Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly. This is called recursive composition.

//UML Picture

 

Use the Composite pattern when

- You want to represent part-whole hierarchies of objects

- You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Benefits

-It makes it easy to add new kinds of components

-It makes clients simpler, since they do not have to know if they are dealing with a leaf or a composite component

Liabilities

It makes it harder to restrict the type of components of a composite

//Example

 //code

//Graphic.java

import  java.lang. * ;
import  java.util.ArrayList;
import  java.util.Iterator;
public   abstract   class  Graphic  {
    
public void Draw()
    
{
        
throw new UnsupportedOperationException();
    }

    
public void Add(Graphic g)
    
{
        
throw new UnsupportedOperationException();
    }

    
public void Remove(Graphic g)
    
{
        
throw new UnsupportedOperationException();
    }

    
public Graphic GetChild(int i)
    
{
        
throw new UnsupportedOperationException();
    }

    
}

class  GraphicElement  extends  Graphic
{
    String Name;
    
public GraphicElement(String Name)
    
{
        
this.Name = Name;
    }

    
public void Draw()
    
{
        System.out.println(Name);
    }

    
}

class  CompositeElement  extends  Graphic
{
    String Name;
    ArrayList elements;
    
public CompositeElement(String Name)
    
{
        
this.Name = Name;
        elements 
= new ArrayList();
    }

    
public void Add(Graphic g)
    
{
        elements.add(g);
    }

    
public void Remove(Graphic g)
    
{
        elements.remove(g);
    }

    
public Graphic GetChild(int i)
    
{
        
if(i<elements.size() && elements.get(i)!=null)
            
return (Graphic)elements.get(i);
        
else
            
return null;
    }

    
public void Draw()
    
{
        System.out.println(Name);
        Iterator iterator
=elements.iterator();
        
while(iterator.hasNext())
        
{
            Graphic g
=(Graphic)iterator.next();
            g.Draw();
        }

    }

    
}

//Main.java

 


public   class  Main  {

    
public static void main(String[] args) {
        GraphicElement aText
=new GraphicElement("aText");
        GraphicElement aLine1
=new GraphicElement("aLine");
        GraphicElement aLine2
=new GraphicElement("aLine");
        GraphicElement aRectangle1
=new GraphicElement("aRectangle");
        GraphicElement aRectangle2
=new GraphicElement("aRectangle");
        
        CompositeElement aPicture1
=new CompositeElement("aPicture1");
        CompositeElement aPicture2
=new CompositeElement("aPicture2");
        
        
        aPicture2.Add(aText);
        aPicture2.Add(aLine1);
        aPicture2.Add(aRectangle1);
        
        aPicture1.Add(aPicture2);
        aPicture1.Add(aLine2);
        aPicture1.Add(aRectangle2);
        
        aPicture1.Draw();
    }


}

 

//results

 

//if u have anything interesting, pls contact with me.QQ:95491590

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值