组合模式: Compsite Pattern

组合模式(Compsite Pattern),又叫部分整体模式,是用来把一组相似的对象当作一个单独的对象。的设计模式属于结构型模式,它创建了对象组的树形结构。

###  代码1 Category

```
 private  Integer id;//主键id
 private  Integer parentId;//父级id
 private  String cateName; //分类的名称


 public  Category() {

 }
 public Category(Integer id, Integer parentId, String cateName) {
     this.id = id;
     this.parentId = parentId;
     this.cateName = cateName;
 }

 public Integer getId() {
     return id;
 }

public void setId(Integer id) {
     this.id = id;
 }

 public Integer getParentId() {
     return parentId;
 }

 public void setParentId(Integer parentId) {
     this.parentId = parentId;
 }

 public String getCateName() {
     return cateName;
 }

 public void setCateName(String cateName) {
     this.cateName = cateName;
 }

 @Override
 public String toString() {
     return "{" +
             "id=" + id +
             ", parentId=" + parentId +
             ", cateName='" + cateName + '\'' +
             '}';
 }
### 代码2 Category1

```
public class Category1  extends  Category{

    //一级分类包含二级分类列表
    private List<Category>  category2List = new ArrayList<>();

    public Category1(Integer id, Integer parentId, String cateName) {
        super(id, parentId, cateName);
    }

    //提供一个新增二级分类内容的方法
    public  void  addCategory(Category category) {
        category2List.add(category);
    }


    public List<Category> getCategory2List() {
        return category2List;
    }

    public void setCategory2List(List<Category> category2List) {
        this.category2List = category2List;
    }

    
    public  void print() {

        System.out.println(this);
        //打印二级分类
        for (int i = 0; i < category2List.size(); i++) {

            Category2 category2 = (Category2)category2List.get(i);
            category2.print();
        }
        
    }
}
代码3 Category2



```
public class Category2 extends  Category{



    //一级分类包含三级分类列表
    private List<Category>  category3List = new ArrayList<>();


    public Category2(Integer id, Integer parentId, String cateName) {
        super(id, parentId, cateName);
    }

    //提供一个新增二级分类内容的方法
    public  void  addCategory(Category category) {
        category3List.add(category);
    }


    public List<Category> getCategory2List() {
        return category3List;
    }

    public void setCategory2List(List<Category> category2List) {
        this.category3List = category2List;
    }

    public  void print() {

        System.out.println("----"+this);
        //打印二级分类
        for (int i = 0; i < category3List.size(); i++) {
            System.out.println("--------"+category3List.get(i));
        }

    }
}
代码4  Category3

```
public class Category3 extends  Category{

    public Category3() {
        super();
    }

    public Category3(Integer id, Integer parentId, String cateName) {
        super(id, parentId, cateName);
    }


    public  void print() {
        System.out.println(this);
    }
}
 TMain

```
public class TMain {

    public static void main(String[] args) {

        //1 三级分类
        Category3 category31 = new Category3(3, 2, "电子书");
        Category3 category32 = new Category3(4, 2, "网络原创");
        Category3 category33 = new Category3(5, 2, "数字杂志");
        //2.二级分类
        Category2 category2 = new Category2(2, 1, "电子书刊");
        category2.addCategory(category31);
        category2.addCategory(category32);
        category2.addCategory(category33);

        //3.一级分类
        Category1 category1 = new Category1(1, 0, "图书、音像、电子书刊");
        category1.addCategory(category2);

        category1.print();
    }

}

 

posted on 2021-12-13 11:03  无心睡眠A8  阅读(77)  评论(0)    收藏  举报