/** * 組合模式統一接口類 */ public interface Component { void operation(); } /** * 組合類 */ public class Composite implements Component { private String name; private List<Component> components = new ArrayList<>(); public Composite(String name) { this.name = name; } public void add(Component component) { components.add(component); } public void remove (Component component) { components.remove(component); } public Component get(int index) { return components.get(index); } @Override public void operation() { System.out.println(this.name); for (Component component : components) { component.operation(); } } } /** * 簡單類 */ public class Leaf implements Component { private String name; public Leaf(String name) { this.name = name; } @Override public void operation() { System.out.println(this.name); } public String getName() { return name; } }
/** * 應用與測試 */ public class Test { public static void main(String[] args) { Composite composite = new Composite("樹枝1"); Leaf leaf1 = new Leaf("樹枝1樹葉1"); Leaf leaf2 = new Leaf("樹枝1樹葉2"); Leaf leaf3 = new Leaf("樹枝1樹葉3"); composite.add(leaf1); composite.add(leaf2); composite.add(leaf3); Composite composite1 = new Composite("樹"); Leaf leaf4 = new Leaf("樹葉4"); Leaf leaf5 = new Leaf("樹葉5"); composite1.add(leaf4); composite1.add(leaf5); composite1.add(composite); composite1.operation(); } }
樹 樹葉4 樹葉5 樹枝1 樹枝1樹葉1 樹枝1樹葉2 樹枝1樹葉3
組合模式中的透明式以及安全式html
組合模式中的角色java
使用組合模式實現目錄和課程之間的關係。
/** * 通用的抽象類 */ public abstract class CatalogComponent { public void add (CatalogComponent catalogComponent) { throw new UnsupportedOperationException("不支持添加操做"); } public void remove (CatalogComponent catalogComponent) { throw new UnsupportedOperationException("不支持刪除操做"); } public String getName () { throw new UnsupportedOperationException("不支持獲取名稱操做"); } public Double getPrice () { throw new UnsupportedOperationException("不支持獲取價錢操做"); } public void print () { throw new UnsupportedOperationException("不支持打印操做"); } } /** * 目錄 */ public class CourseCatalog extends CatalogComponent { private List<CatalogComponent> itsms = new ArrayList<>(); private String name; private Integer level; public CourseCatalog(String name, Integer level) { this.name = name; this.level = level; } @Override public String getName() { return this.name; } @Override public void add(CatalogComponent catalogComponent) { this.itsms.add(catalogComponent); } @Override public void remove(CatalogComponent catalogComponent) { this.itsms.remove(catalogComponent); } @Override public void print() { System.out.println("> " + this.name); for (CatalogComponent catalogComponent : itsms) { if (this.level != null) { for (int i = 0; i < this.level; i ++) { System.out.print("--"); } } catalogComponent.print(); } } } /** * 具體的課程 */ public class Course extends CatalogComponent { private String name; private Double price; public Course(String name, Double price) { this.name = name; this.price = price; } @Override public String getName() { return this.name; } @Override public Double getPrice() { return this.price; } @Override public void print() { System.out.println("> Course Name:" + this.name + ": price: " + this.price); } }
/** * 測試與應用 */ public class Test { public static void main(String[] args) { CatalogComponent linuxCourse = new Course("Linux課程", 11D); CatalogComponent windowCourse = new Course("Window課程", 12D); CatalogComponent javaCourseCatalog = new CourseCatalog("Java課程目錄", 2); CatalogComponent mmallCourse1 = new Course("Java電商一期", 55D); CatalogComponent mmallCourse2 = new Course("Java電商二期", 66D); CatalogComponent designPattern = new Course("Java設計模式", 77D); javaCourseCatalog.add(mmallCourse1); javaCourseCatalog.add(mmallCourse2); javaCourseCatalog.add(designPattern); CatalogComponent mainCourseCatalog = new CourseCatalog("課程主目錄", 1); mainCourseCatalog.add(linuxCourse); mainCourseCatalog.add(windowCourse); mainCourseCatalog.add(javaCourseCatalog); mainCourseCatalog.print(); } }
組合模式和訪問者模式linux
慕課網設計模式精講
: https://coding.imooc.com/class/270.html 組合模式(詳解版)
: http://c.biancheng.net/view/1373.html 設計模式之組合模式
: https://www.cnblogs.com/snaildev/p/7647190.html