設計模式--第8篇(組合模式)

一,組合模式

組合模式:

  1. 又叫部分整體模式,創建了對象組的樹形結構,將對象組合成樹狀結構以表示“整體與部分”的層次關係;
  2. 依據樹形結構來組合對象,用來表示部分與整體的關係;
  3. 使用時對單個對象和組合對象的訪問具有一致性;

二,原理類圖

在這裏插入圖片描述
意圖: 將對象組合成樹形結構以表示“部分-整體”的層次結構。Composite使得用戶對單個對象和組合對象的使用具有一致性。
適用性:

  1. 你想表示對象的部分-整體層次結構。
  2. 你希望用戶忽略組合對象與單個對象的不同,用戶將統一地使用組合結構中的所有對象。

三,實例

component

package com.neei.component;

/**
 * @param
 * @Author: AaNeei
 * @Date: 2019/10/9  21:19
 * @Description: 遊學網
 * @throws:
 */
public abstract class OrganizationComponent {
    private String name;
    private String desc;

    protected void add(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    protected void remove(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public OrganizationComponent(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    protected abstract void print();
}

Composite

package com.neei.component;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @Author: AaNeei
 * @Date: 2019/10/9  21:22
 * @Description: 遊學網
 * @throws:
 */
public class University extends OrganizationComponent {

    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    public University(String name, String desc) {
        super(name, desc);
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }

    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    public String getDesc() {
        return super.getDesc();
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    protected void print() {
        System.out.println("--------" + getName() + "---------");
        for (OrganizationComponent component : organizationComponents) {
            component.print();
        }
    }
}

Composite

package com.neei.component;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @Author: AaNeei
 * @Date: 2019/10/9  21:27
 * @Description: 遊學網
 * @throws:
 */
public class College extends OrganizationComponent {
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    public College(String name, String desc) {
        super(name, desc);
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }

    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    public String getDesc() {
        return super.getDesc();
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    protected void print() {
        System.out.println("--------" + getName() + "---------");
        for (OrganizationComponent component : organizationComponents) {
            component.print();
        }
    }
}

leaf

package com.neei.component;

import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @Author: AaNeei
 * @Date: 2019/10/9  21:28
 * @Description: 遊學網
 * @throws:
 */
public class Department extends OrganizationComponent {

    public Department(String name, String desc) {
        super(name, desc);
    }

    @Override
    public String getDesc() {
        return super.getDesc();
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    protected void print() {
        System.out.println("--------" + getName() + "---------");
    }
}

調用

package com.neei.component;

/**
 * @param
 * @Author: AaNeei
 * @Date: 2019/10/9  21:30
 * @Description: 遊學網
 * @throws:
 */
public class Client {
    public static void main(String[] args) {
        OrganizationComponent university = new University("清華", "國際名校");
        OrganizationComponent college1 = new College("計算機", "都是大神");
        OrganizationComponent college2 = new College("機械", "都是大神");
        college1.add(new Department("java", "牛逼"));
        college1.add(new Department("C#", "牛逼"));
        college2.add(new Department("機器人", "牛逼"));
        college2.add(new Department("自動化", "牛逼"));
        university.add(college1);
        university.add(college2);
        university.print();

    }
}

四,源碼分析

JDK源碼中使用的組合模式,如HashMap

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章