1. 类图
2. Java实现代码
package cn.edu.ynu.sei.composite;
/**
* 抽象组件角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public interface Component
{
/**
* 返回自己的实例
* @return
*/
public Composite getComposite();
/**
* 某个业务方法
*/
public void sampleOperation();
}
/**
* 抽象组件角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public interface Component
{
/**
* 返回自己的实例
* @return
*/
public Composite getComposite();
/**
* 某个业务方法
*/
public void sampleOperation();
}
package cn.edu.ynu.sei.composite;
import java.util.Enumeration;
import java.util.Vector;
/**
* 树枝角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public class Composite implements Component
{
private Vector<Component> componentVector = new Vector<Component>();
@Override
public Composite getComposite()
{
return this;
}
@Override
public void sampleOperation()
{
Enumeration<Component> enumeration = components();
while (enumeration.hasMoreElements())
{
enumeration.nextElement().sampleOperation();
}
}
/**
* 聚集管理方法,增加一个自构件对象
* @param component
*/
public void add(Component component)
{
componentVector.addElement(component);
}
/**
* 聚集管理方法,删除一个子构件对象
* @param component
*/
public void remove(Component component)
{
componentVector.removeElement(component);
}
/**
* 聚集管理方法,返回聚集的Enumeration对象
* @return
*/
private Enumeration<Component> components()
{
return componentVector.elements();
}
}
import java.util.Enumeration;
import java.util.Vector;
/**
* 树枝角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public class Composite implements Component
{
private Vector<Component> componentVector = new Vector<Component>();
@Override
public Composite getComposite()
{
return this;
}
@Override
public void sampleOperation()
{
Enumeration<Component> enumeration = components();
while (enumeration.hasMoreElements())
{
enumeration.nextElement().sampleOperation();
}
}
/**
* 聚集管理方法,增加一个自构件对象
* @param component
*/
public void add(Component component)
{
componentVector.addElement(component);
}
/**
* 聚集管理方法,删除一个子构件对象
* @param component
*/
public void remove(Component component)
{
componentVector.removeElement(component);
}
/**
* 聚集管理方法,返回聚集的Enumeration对象
* @return
*/
private Enumeration<Component> components()
{
return componentVector.elements();
}
}
package cn.edu.ynu.sei.composite;
/**
* 树叶角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public class Leaf implements Component
{
@Override
public Composite getComposite()
{
// TODO Auto-generated method stub
return null;
}
@Override
public void sampleOperation()
{
// TODO Auto-generated method stub
}
}
/**
* 树叶角色
*
* @author 88250
* @version 1.0.0, 2007-9-4
*/
public class Leaf implements Component
{
@Override
public Composite getComposite()
{
// TODO Auto-generated method stub
return null;
}
@Override
public void sampleOperation()
{
// TODO Auto-generated method stub
}
}