前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA 设计模式 组合模式

JAVA 设计模式 组合模式

作者头像
静默虚空
发布2018-01-05 10:59:01
5570
发布2018-01-05 10:59:01
举报

用途

组合模式 (Component)

将对象组合成树形结构以表示“部分-整体”的层次结构。 组合模式使得用户对单个对象和组合对象的使用具有唯一性

组合模式是一种结构型模式

结构

图-组合模式结构图

Component : 组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理 Component 的子部件。

代码语言:txt
复制
abstract class Component {
 protected String name;
 
 public Component(String name) {
 this.name = name;
     }
 
 public abstract void Add(Component c);
 public abstract void Remove(Component c);
 public abstract void Display(int depth);
 }

Leaf : 表示叶节点对象。叶子节点没有子节点。

代码语言:txt
复制
class Leaf extends Component {
 
 public Leaf(String name) {
 super(name);
     }
 
     @Override
 public void Add(Component c) {
         System.out.println("Can not add to a leaf");
     }
 
     @Override
 public void Remove(Component c) {
         System.out.println("Can not remove from a leaf");
     }
 
     @Override
 public void Display(int depth) {
         String temp = "";
 for (int i = 0; i < depth; i++) 
             temp += '-';
         System.out.println(temp + name);
     }
 
 }

Composite : 定义枝节点行为,用来存储子部件,在 Component 接口中实现与子部件相关的操作。例如 Add 和 Remove。

代码语言:txt
复制
class Composite extends Component {
 
 private List<Component> children = new ArrayList<Component>();
 
 public Composite(String name) {
 super(name);
     }
 
     @Override
 public void Add(Component c) {
         children.add(c);
     }
 
     @Override
 public void Remove(Component c) {
         children.remove(c);
     }
 
     @Override
 public void Display(int depth) {
         String temp = "";
 for (int i = 0; i < depth; i++) 
             temp += '-';
         System.out.println(temp + name);
 
 for (Component c : children) {
             c.Display(depth + 2);
         }
     }
 
 }

Client : 通过 Component 接口操作结构中的对象。

代码语言:txt
复制
public class CompositePattern {
 
 public static void main(String[] args) {
         Composite root = new Composite("root");
         root.Add(new Leaf("Leaf A"));
         root.Add(new Leaf("Leaf B"));
 
         Composite compX = new Composite("Composite X");
         compX.Add(new Leaf("Leaf XA"));
         compX.Add(new Leaf("Leaf XB"));
         root.Add(compX);
 
         Composite compXY = new Composite("Composite XY");
         compXY.Add(new Leaf("Leaf XYA"));
         compXY.Add(new Leaf("Leaf XYB"));
         compX.Add(compXY);
 
         root.Display(1);
     }
 
 }

应用场景

1、想要表示对象的部分-整体层次结构。

2、想要客户端忽略组合对象与单个对象的差异,客户端将统一地使用组合结构中的所有对象。

关于分级数据结构的一个普遍性的例子是你每次使用电脑时所遇到的:文件系统

文件系统由目录和文件组成。每个目录都可以装内容。目录的内容可以是文件,也 可以是目录。

按照这种方式,计算机的文件系统就是以递归结构来组织的。如果你想要描述这样的数据结构,那么你可以使用组合模式。

要点

组合模式定义由 Leaf 对象和 Composite 对象组成的类结构;

它使得客户端变得简单;

它使得添加或删除子部件变得很容易。

推荐

本文属于 JAVA设计模式系列

参考资料

《大话设计模式》《HeadFirst设计模式》

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-01-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档