组合模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
使用场景:系统对象层次具备整体和部分,呈树形结构,且要求具备统一行为(如树形菜单,操作系统目录结构,公司组织架构等)。
uml图:
组合模式_第1张图片
最近想做个家谱系统,也是树形结构,刚好可以先拿这个举个例子,如下图所示:
组合模式_第2张图片

import java.util.List;

public abstract class FamilyComponent {
     

    public void add(FamilyComponent person) {
     
        //默认抛出异常
        throw new UnsupportedOperationException();
    }

    public void remove(FamilyComponent person) {
     
        throw new UnsupportedOperationException();
    }

    public List<FamilyComponent> getAllChildren() {
     
        throw new UnsupportedOperationException();
    }
    

    public FamilyComponent getChild(int i) {
     
        throw new UnsupportedOperationException();
    }
}


import java.util.List;

public class Person extends FamilyComponent{
     
    private String name;
    private int age;
    public  Person(String name,int age){
     
       this.name=name;
       this.age=age;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public int getAge() {
     
        return age;
    }

    public void setAge(int age) {
     
        this.age = age;
    }


    @Override
    public List<FamilyComponent> getAllChildren(){
     
        return null;
    };


    @Override
    public String toString() {
     
        return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }
}
import java.util.ArrayList;
import java.util.List;

public class PersonComponent extends FamilyComponent {
     
    private Person person;
    private List<FamilyComponent> child;

    public PersonComponent(Person person) {
     
        this.person = person;
        child = new ArrayList<>();
    }

    @Override
    public void add(FamilyComponent person) {
     
        child.add(person);
    }

    @Override
    public void remove(FamilyComponent person) {
     
        child.remove(person);
    }

    @Override
    public List<FamilyComponent> getAllChildren() {
     
        return child;
    }

    @Override
    public FamilyComponent getChild(int i) {
     
        return child.get(i);
    }

    public Person getPerson() {
     
        return person;
    }

    public void setPerson(Person person) {
     
        this.person = person;
    }

    @Override
    public String toString() {
     
        return "非叶子节点{" +
            "person=" + person +
            '}';
    }

    public void print(FamilyComponent component) {
     
        //根节点
        System.out.println(component);
        System.out.println("--------");
        //打印所有节点
        if (component.getAllChildren() != null && component.getAllChildren().size()>0) {
     
            for (FamilyComponent person : component.getAllChildren()) {
     
                if (component.getAllChildren() != null) {
     
                    print(person);
                }
            }
        }
    }
    }

public class test {
     
    public static void main(String[] args) {
     
        Person grandmother = new Person("姥姥", 80);
        Person mother = new Person("妈妈", 54);
        Person aunt = new Person("大姨", 50);
        Person me = new Person("槐序", 24);
        Person brother = new Person("哥哥", 26);
        Person sister = new Person("表姐", 28);
        PersonComponent grandmother1 = new PersonComponent(grandmother);
        PersonComponent mother1 = new PersonComponent(mother);
        PersonComponent aunt1 = new PersonComponent(aunt);
        grandmother1.add(mother1);
        grandmother1.add(aunt1);
        mother1.add(me);
        mother1.add(brother);
        aunt1.add(sister);
        grandmother1.print(grandmother1);

    }

}

输出:

非叶子节点{person=Person{name='姥姥', age=80}}
--------
非叶子节点{person=Person{name='妈妈', age=54}}
--------
Person{name='槐序', age=24}
--------
Person{name='哥哥', age=26}
--------
非叶子节点{person=Person{name='大姨', age=50}}
--------
Person{name='表姐', age=28}
--------

你可能感兴趣的:(组合模式)