问题描述
我在一系统中,有几处用到了树结构,组织机构,资源菜单,分类这些都是用到了无限级分类,做了几个发现这样很多重复的代码,从 entity,dao,manager,acton都很多代码方法都重复了有时要优化一下,发现别的模块也需要优化,这样重复地方多了,怎样设计好点呢?我的是(SSH+EXT) 问题补充:/** * 组织机构 * * * */@Entity@Table(name = "t_org")@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)public class Organization { private String name; private String sn;private String description;private Organization parent;private boolean leaf;//是否叶子节点private boolean expanded;//是否展开 public boolean getLeaf() {return leaf;}public void setLeaf(boolean leaf) {this.leaf = leaf;}public boolean getExpanded() {return expanded;}public void setExpanded(boolean expanded) {this.expanded = expanded;}private List<Organization> children; private int orderNo;public int getOrderNo() {return orderNo;}public void setOrderNo(int orderNo) {this.orderNo = orderNo;} @Column(name = "orgName", nullable = false)public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSn() {return sn;}public void setSn(String sn) {this.sn = sn;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@ManyToOne(targetEntity = com.abu.jxc.org.entity.Organization.class)public Organization getParent() {return parent;}public void setParent(Organization parent) {this.parent = parent;} @OneToMany(targetEntity = com.abu.jxc.org.entity.Organization.class, mappedBy = "parent", cascade = { CascadeType.ALL })public List<Organization> getChildren() {return children;}public void setChildren(List<Organization> children) {this.children = children;} }/** * 组织机构 * * */@Component("orgDao")public class OrgDao extends HibernateDao<Organization, Long> { }public interface OrgManager {/** * * * @param parentId * @return */List seachOrgs(Long parentId); */void delOrg(Long id);}@Component("orgManagerImpl")@Transactionalpublic class OrgManagerImpl implements OrgManager {private OrgDao orgDao;@Resource(name = "orgDao")public void setOrgDao(OrgDao orgDao) {this.orgDao = orgDao;} publicList seachOrgs(Long parentId){ } */public void delOrg(Long id){}}
解决方案
重构喽 重复部分尽量设计成可扩展的,即OCP原则封装变化原则、接口隔离 etc.建议你贴代码