《Java与模式》学习笔记:设计模式——树结构(合成模式+模板模式)

来源:互联网 发布:阿里云域名优惠口令 编辑:程序博客网 时间:2024/04/30 04:28

这是一次合成模式+模板模式的尝试

这是一个树结构的设计。

该组件只注重设计一个可扩展的树结构,并不提供创建树和遍历树的方式。用户可以针对这个树结构自己写实现。
该组件使用了合成模式+模板模式来设计。树的结构是根据透明的合成模式来设计的,这样使得客户端可以把树叶和树枝对象当成同一类对象处理。而树叶、树枝的抽象和具体实现之间的关系是通过模板模式来设计的,这样使得客户端可以很简单地将结点的具体对象置换掉。

结构说明:
TreeComponent:树结点的高度抽象,定义树结构的一些方法,所有树叶结点和树枝结点都上转成TreeComponent
TreeComposite:抽象树枝结点。
TreeLeaf:抽象树叶结点。

PermissionComposite:具体树枝结点。
PermissionLeaf:具体树叶结点。

PermissionBean:结点的具体内容,也就是结点对象所包含的成员变量。就是一个简单的JavaBean

注意:如果要为这个树结构设计一个遍历的方式,也许可以用迭代子模式为TreeComponent设计一个外禀子的迭代器。(有空再考虑^_^)


树结点的高度抽象,定义树结构的一些方法,所有树叶结点和树枝结点都上转成TreeComponent
public interface TreeComponent {

 /**
  * 添加子结点方法
  * @param component
  * @return
  */
    boolean add(TreeComponent component);

 /**
  * 删除子结点方法
  * @param component
  * @return
  */
    boolean remove(TreeComponent component);

    /**
     * 返回子结点方法
     * @return
     */
    List components();
   
    /**
     * 统计该结点(包括该结点)下,所有结点的数量
     * @return
     */
    int count();
   
    /**
     * 判断当前结点是否是树叶结点
     * @return
     */
    boolean isLeaf();
   
    /**
     * 返回该结点
     * @return
     */
    TreeComponent getComponent();
   
    /**
     * 返回当前结点名
     * @return
     */
    String getName();
   
    /**
     * 统计该结点(包括该结点)下,所有树叶结点的数量
     * @return
     */
    int leafCount();
}


抽象树枝结点。
public abstract class TreeComposite implements TreeComponent{
 
 /**
  * 模板方法:添加子结点方法
  */
 public final boolean add(TreeComponent component) {
  // TODO Auto-generated method stub
  components().add(component);
  return true;
 }
 

 /**
  * 模板方法:统计该结点(包括该结点)下,所有结点的数量
  */
 public final int count() {
  // TODO Auto-generated method stub
  int count = 1;
  List list = components();
  for(int i = 0;i<list.size();i++){
   TreeComponent component = (TreeComponent)list.get(i);
   count += component.count();
  }
  return count;
 }
 
 /**
  * 模板方法:统计该结点(包括该结点)下,所有树叶结点的数量
  */
 public final int leafCount() {
  // TODO Auto-generated method stub
  int count = 0;
  List list = components();
  for(int i = 0;i<list.size();i++){
   TreeComponent component = (TreeComponent)list.get(i);
   count += component.leafCount();
  }
  return count;
 }

 /**
  * 模板方法:删除子结点方法
  */
 public final boolean remove(TreeComponent component) {
  // TODO Auto-generated method stub
  components().remove(component);
  return true;
 }

 /**
  * 具体方法:返回该结点
  */
 public final TreeComponent getComponent() {
  // TODO Auto-generated method stub
  return this;
 }

 /**
  * 具体方法:判断当前结点是否是树叶结点
  */
 public final boolean isLeaf() {
  // TODO Auto-generated method stub
  return false;
 }
 
 /**
  * 抽象方法:返回当前结点名
  */
 public abstract String getName() ;

 /**
  * 抽象方法:返回子结点
  */
 public abstract List components() ;
}


抽象树叶结点。
public abstract class TreeLeaf implements TreeComponent{

 /**
  * 具体方法:添加子结点方法
  */
 public final boolean add(TreeComponent component) {
  // TODO Auto-generated method stub
  return false;
 }

 /**
  * 具体方法:返回子结点方法
  */
 public final List components() {
  // TODO Auto-generated method stub
  return null;
 }

 /**
  * 具体方法:删除子结点方法
  */
 public final boolean remove(TreeComponent component) {
  // TODO Auto-generated method stub
  return false;
 }
 
 /**
  * 具体方法:统计该结点(包括该结点)下,所有结点的数量
  */
 public final int count() {
  // TODO Auto-generated method stub
  return 1;
 }
 
 /**
  * 具体方法:判断当前结点是否是树叶结点
  */
 public final boolean isLeaf() {
  // TODO Auto-generated method stub
  return true;
 }
 
 /**
  * 具体方法:返回该结点
  */
 public final TreeComponent getComponent() {
  // TODO Auto-generated method stub
  return this;
 }
 
 /**
  * 具体方法:统计该结点(包括该结点)下,所有树叶结点的数量
  */
 public final int leafCount() {
  // TODO Auto-generated method stub
  return 1;
 }
 
 /**
  * 抽象方法:返回当前结点名
  */
 public abstract String getName() ;
}


具体树枝结点。
public class PermissionComposite extends TreeComposite{

 //状态延迟到子类实现,父类中只申明属性的抽象访问方法
 private List list = new ArrayList();
 private PermissionBean bean;
 
 /**
  * 构造函数
  * @param bean
  */
 public PermissionComposite(PermissionBean bean) {
  // TODO Auto-generated constructor stub
  this.bean = bean;
 }
 
 /**
  * 父类强制子类实现的抽象方法:返回子结点
  */
 public List components() {
  // TODO Auto-generated method stub
  return list;
 }

 /**
  * 父类强制子类实现的抽象方法:返回当前结点名
  */
 public String getName() {
  // TODO Auto-generated method stub
  return bean.getTitle();
 }
}


具体树叶结点。
public class PermissionLeaf extends TreeLeaf{

 //状态延迟到子类实现,父类中只申明属性的抽象访问方法
 private PermissionBean bean;
 
 /**
  * 构造函数
  * @param bean
  */
 public PermissionLeaf(PermissionBean bean) {
  // TODO Auto-generated constructor stub
  this.bean = bean;
 }
 
 /**
  * 父类强制子类实现的抽象方法:返回当前结点名
  */
 public String getName() {
  // TODO Auto-generated method stub
  return bean.getTitle();
 }
 
}


结点的具体内容,也就是结点对象所包含的成员变量。就是一个简单的JavaBean
public class PermissionBean {
 private String functionid;
 
 private String title;
 
 private String parent;
 
 private String type;
 
 public PermissionBean() {
  // TODO Auto-generated constructor stub
 }
 
 public PermissionBean(String functionid,String title,String parent,String type) {
  // TODO Auto-generated constructor stub
  this.functionid = functionid;
  this.title = title;
  this.parent = parent;
  this.type = type;
 }

 public String getFunctionid() {
  return functionid;
 }

 public void setFunctionid(String functionid) {
  this.functionid = functionid;
 }

 public String getParent() {
  return parent;
 }

 public void setParent(String parent) {
  this.parent = parent;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }
}
 

原创粉丝点击