![在这里插入图片描述](https://img.php1.cn/3cd4a/1e618/c5a/d5d40da532c3a782.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTc3ODM4,size_16,color_FFFFFF,t_70)
介绍
组合模式 (Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单位的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建来对象组的树形结构。
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/011ac27956d007f0.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTc3ODM4,size_16,color_FFFFFF,t_70)
需求:
展示一个学校有多少个学校院系组成,一个学院有多少个学院,一个学院有多个系。
- 创建 OrganizationComponent 类 该类带有 OrganizationComponent 对象
@Data
@ToString
public abstract class OrganizationComponent {private String name;private String des;protected void add(OrganizationComponent component){throw new UnsupportedOperationException();}protected void remove(OrganizationComponent component){throw new UnsupportedOperationException();}protected abstract void print();public OrganizationComponent(String name, String des) {this.name = name;this.des = des;}
}
- 创建University 学校类
public class University extends OrganizationComponent{List<OrganizationComponent> list &#61; new ArrayList<>();public University(String name, String des) {super(name, des);}&#64;Overrideprotected void add(OrganizationComponent component) {list.add(component);}&#64;Overrideprotected void remove(OrganizationComponent component) {list.remove(component);}&#64;Overrideprotected void print() {System.out.println("----"&#43;getName()&#43;"---");list.stream().forEach( component -> {component.print();});}
}
- 创建学院类
public class College extends OrganizationComponent{List<OrganizationComponent> list &#61; new ArrayList<>();public College(String name, String des) {super(name, des);}&#64;Overrideprotected void add(OrganizationComponent component) {list.add(component);}&#64;Overrideprotected void remove(OrganizationComponent component) {list.remove(component);}&#64;Overrideprotected void print() {System.out.println("--------"&#43;getName()&#43;"--------");list.stream().forEach( component -> {component.print();});}
}
- 创建专业类
public class Department extends OrganizationComponent{public Department(String name, String des) {super(name, des);}&#64;Overrideprotected void print() {System.out.println("------------"&#43;getName()&#43;"------------");}
}
- Client
public class Client {public static void main(String[] args) {University university &#61; new University("清华", "985");College college &#61; new College("计算机学院", "计算机学院");College college2 &#61; new College("信息工程学院", "信息工程学院");college.add(new Department("计算机科学与技术","计算机科学与技术"));college.add(new Department("网络工程","网络工程"));college2.add(new Department("通信工程","通信工程"));college2.add(new Department("信息工程","通信工程"));university.add(college);university.add(college2);university.print();}
}
![在这里插入图片描述](https://img.php1.cn/3cd4a/189d8/978/7dbdf0f38ad53545.jpeg)
组合模式在JDK Map中使用
- Map 就是一个抽象的构件&#xff0c;类似于组合模式中的Component&#xff1b;
- HashMap是中间的构件&#xff0c;只不过在HashMap与Component之间多了一层AbstractMap&#xff0c;并且重写了Map中put()&#xff0c;putAll()等方法&#xff1b;
- Node是HashMap中的静态内部类&#xff0c;类似于Leaf叶子节点
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/7cccb7e4b6cb5cb8.webp?x-oss-process&#61;image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTc3ODM4,size_16,color_FFFFFF,t_70)
Map 接口 &#xff1a;顶级接口
public interface Map<K,V> {V put(K key, V value);void putAll(Map<? extends K, ? extends V> m);………………
}
AbstractMap 抽象类实现来Map接口 &#xff1a;接口实现&#xff0c;扩展
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());}}
HashMap &#xff1a;方法实现
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);}public void putAll(Map<? extends K, ? extends V> m) {putMapEntries(m, true);}
}
HashMap中的内部类 Node &#xff1a;他其实就是我们组合模式中所讲述的Leaf对象&#xff0c;它没有再组合任何的子节点&#xff0c;提供的也只有get方法和set方法
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 &#61; hash;this.key &#61; key;this.value &#61; value;this.next &#61; next;}public final K getKey() { return key; }public final V getValue() { return value; }public final String toString() { return key &#43; "&#61;" &#43; value; }public final int hashCode() {return Objects.hashCode(key) ^ Objects.hashCode(value);}public final V setValue(V newValue) {V oldValue &#61; value;value &#61; newValue;return oldValue;}public final boolean equals(Object o) {if (o &#61;&#61; this)return true;if (o instanceof Map.Entry) {Map.Entry<?,?> e &#61; (Map.Entry<?,?>)o;if (Objects.equals(key, e.getKey()) &&Objects.equals(value, e.getValue()))return true;}return false;}}
github Demo地址 : &#xff5e;&#xff5e;&#xff5e;传送门&#xff5e;&#xff5e;&#xff5e;
个人博客地址&#xff1a;http://blog.yanxiaolong.cn/