HelloWorld开发者社区

www.helloworld.net - 开发者专属的技术社区

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

组合模式的介绍:物以类聚组合模式,结构型模式之一,组合模式比较简单,它将一组相似的对象看作一个对象处理,并根据一个树状结构来组合对象,然后提供一个统一的方法去访问相应的对象,以此忽略掉对象与对象集合之间的差别。

组合模式的定义:将对象表示成树形的层次结构,使得用户对单个对象和组合对象的使用具有一致性

组合模式的使用场景:表示对象的部分-整体层次结构时,从一个整体中能够独立出部分模块或者功能的场景

 

下面是组合模式的能用代码 

首先要有一个节点的抽象类,代码如下:

 1 /**
 2  * 透明组合模式
 3  */
 4 public abstract class Component {
 5     protected String name;
 6 
 7     public Component(String name){
 8         this.name = name;
 9     }
10 
11     //具体的逻辑由子类来实现
12     public abstract void doSomething();
13 
14     //添加一个节点
15     public abstract void addChild(Component child);
16 
17     //移除一个节点
18     public abstract void removeChild(Component child);
19 
20     //获取一个节点
21     public abstract Component getChild(int index);
22 
23 }

 

叶子节点:

 1 /**
 2  * 叶子节点
 3  */
 4 public class Leaf extends Component{
 5 
 6     public Leaf(String name) {
 7         super(name);
 8     }
 9 
10     @Override
11     public void doSomething() {
12         System.out.println(name);
13     }
14 
15     @Override
16     public void addChild(Component child) {
17         throw new UnsupportedOperationException("叶子节点没有子节点");
18     }
19 
20     @Override
21     public void removeChild(Component child) {
22         throw new UnsupportedOperationException("叶子节点没有子节点");
23     }
24 
25     @Override
26     public Component getChild(int index) {
27         throw new UnsupportedOperationException("叶子节点没有子节点");
28     }
29 }

 

具有叶子节点的节点:

 1 /**
 2  * 具有叶子节点的节点
 3  */
 4 public class Composite extends Component{
 5 
 6     private ArrayList<Component> components = new ArrayList<>();
 7 
 8     public Composite(String name){
 9         super(name);
10     }
11 
12     @Override
13     public void doSomething() {
14         System.out.println(name);
15         if(components != null){
16             for (Component child : components){
17                 child.doSomething();
18             }
19         }
20     }
21 
22     //
23     @Override
24     public void addChild(Component child) {
25         components.add(child);
26     }
27 
28     //
29     @Override
30     public void removeChild(Component child) {
31         components.remove(child);
32     }
33 
34     //
35     @Override
36     public Component getChild(int index) {
37         return components.get(index);
38     }
39 }

 

使用的时候可以构造一个根节点,支干节点和几个叶子节点:下面是客户端用法 

 1 /**
 2  * 客户端测试类
 3  */
 4 public class Client {
 5     public static void test(){
 6 
 7         //构造一个根节点
 8         Composite root = new Composite("root");
 9 
10         //构造两个枝干节点
11         Composite branch1 = new Composite("branch1");
12         Composite branch2 = new Composite("branch2");
13 
14         //构造两个叶子节点
15         Leaf leaf1 = new Leaf("left1");
16         Leaf leaf2 = new Leaf("left2");
17 
18         branch1.addChild(leaf1);
19         branch2.addChild(leaf2);
20 
21         root.addChild(branch1);
22         root.addChild(branch2);
23 
24 
25         root.doSomething();
26     }
27 }

 

组合模式在android源码中典型的应用注是View和ViewGroup,ViewGroup本身也是一个View,但是和View不同的是,ViewGroup里面可以包含其它子View以及ViewGroup

 

posted on 2017-04-25 20:49  HelloWorld开发者社区  阅读(192)  评论(0编辑  收藏  举报