业务需求
问题分析
基本介绍
原理图说明
代码实现
组织机构父类,可以是普通类、抽象类、接口
默认实现的方法作用在于,根据下边的节点是否为叶子节点,决定是否可以使用父类方法
@Data public abstract class OrganizationComponent { private String name; private String des; public OrganizationComponent(String name, String des) { super(); this.name = name; this.des = des; } protected void add(OrganizationComponent organizationComponent) { // 默认实现 throw new UnsupportedOperationException(); } protected void remove(OrganizationComponent organizationComponent) { // 默认实现 throw new UnsupportedOperationException(); } // 子类都需要实现 protected abstract void print(); }
定义大学类
public class University extends OrganizationComponent{ List<OrganizationComponent> list = new ArrayList<>(); public University(String name, String des) { super(name, des); } @Override protected void add(OrganizationComponent organizationComponent) { list.add(organizationComponent); } @Override protected void remove(OrganizationComponent organizationComponent) { list.remove(organizationComponent); } // 输出当前University所包含的学院 @Override protected void print() { System.out.println("============="+getName()+"==============="); for (OrganizationComponent o : list) { o.print(); } } }
定义学院类
public class College extends OrganizationComponent{ List<OrganizationComponent> list = new ArrayList<>(); public College(String name, String des) { super(name, des); } @Override protected void add(OrganizationComponent organizationComponent) { list.add(organizationComponent); } @Override protected void remove(OrganizationComponent organizationComponent) { list.remove(organizationComponent); } // 输出当前College所包含的学院 @Override protected void print() { System.out.println("============="+getName()+"==============="); for (OrganizationComponent o : list) { o.print(); } } }
定义具体系
public class Department extends OrganizationComponent{ public Department(String name, String des) { super(name, des); } // 叶子节点不具备增删,add和remove不再重写 @Override protected void print() { System.out.println(getName()); } }
客户端
public class Client { public static void main(String[] args) { // 创建学校 OrganizationComponent university = new University("清华大学", "中国名校"); // 创建学院 OrganizationComponent computerCollege = new College("计算机学院", "计算机学院"); OrganizationComponent infoEngineerCollege = new College("信息工程学院", "信息工程学院"); // 创建系 computerCollege.add(new Department("软件工程", "软件工程真不错")); computerCollege.add(new Department("网络工程", "网络工程也不错")); computerCollege.add(new Department("计算机科学与技术", "计算机科学与技术还可以")); infoEngineerCollege.add(new Department("通信工程", "通信工程不好学")); infoEngineerCollege.add(new Department("信息工程", "信息工程太难了")); // 学院添加到学校中 university.add(computerCollege); university.add(infoEngineerCollege); // 需要查看哪个级别,直接输出即可 university.print(); System.out.println("##################分割线####################"); computerCollege.print(); } }
Demo
public class Composite { public static void main(String[] args) { Map<Integer, String> hashMap = new HashMap<>(); // 存入叶子节点 hashMap.put(0, "水浒传"); Map<Integer, String> map = new HashMap<>(); map.put(1, "西游记"); map.put(2, "红楼梦"); // 存入节点 hashMap.putAll(map); System.out.println(hashMap); } }
源码片段
public interface Map<K, V> { ... V put(K var1, V var2); ... void putAll(Map<? extends K, ? extends V> var1);
public abstract class AbstractMap<K,V> implements Map<K,V> { ... public V put(K key, V value) { throw new UnsupportedOperationException(); } ... public void putAll(Map<? extends K, ? extends V> m) { for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) put(e.getKey(), e.getValue()); } }
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { ... public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } ... final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; ... return null; } ... static class Node<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Node<K,V> next; Node(int hash, K key, V value, Node<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } public final K getKey() { return key; } public final V getValue() { return value; } public final String toString() { return key + "=" + value; } public final int hashCode() { return Objects.hashCode(key) ^ Objects.hashCode(value); } public final V setValue(V newValue) { V oldValue = value; value = newValue; return oldValue; } public final boolean equals(Object o) { if (o == this) return true; if (o instanceof Map.Entry) { Map.Entry<?,?> e = (Map.Entry<?,?>)o; if (Objects.equals(key, e.getKey()) && Objects.equals(value, e.getValue())) return true; } return false; } } }
说明
注意实现和细节