• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > 深入剖析设计模式中的组合模式应用及在C++中的实现

深入剖析设计模式中的组合模式应用及在C++中的实现

作者:梦在天涯 字体:[增加 减小] 来源:互联网 时间:2017-05-28

梦在天涯 通过本文主要向大家介绍了设计模式c++,c++设计模式pdf,大话设计模式c++,c++设计模式视频教程,c++ qt设计模式等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

组合模式将对象组合成树形结构以表示“部分-整体”的层次结构。C o m p o s i t e 使得用户对单个对象和组合对象的使用具有一致性。

模式图:

201631193912941.jpg (571×257)

适用场景:

  • 你想表示对象的部分-整体层次结构。
  • 你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

举例:

namespace FactoryMethod_DesignPattern
{
  using System;
  using System.Collections;

  abstract class Component 
  {
    protected string strName;

    public Component(string name)
    {
      strName = name;
    }

    abstract public void Add(Component c);
  
    public abstract void DumpContents();
    
    // other operations for delete, get, etc.
  }

  class Composite : Component
  {
    private ArrayList ComponentList = new ArrayList();
    
    public Composite(string s) : base(s) {}

    override public void Add(Component c)
    {
      ComponentList.Add(c);
    }

    public override void DumpContents()
    {
      // First dump the name of this composite node
      Console.WriteLine("Node: {0}", strName);

      // Then loop through children, and get then to dump their contents
      foreach (Component c in ComponentList)
      {
        c.DumpContents();
      }
    }
  }

  class Leaf : Component
  {
    public Leaf(string s) : base(s) {}

    override public void Add(Component c)
    {
      Console.WriteLine("Cannot add to a leaf");
    }

    public override void DumpContents()
    {
      Console.WriteLine("Node: {0}", strName);
    }
  }

  /// <summary>
  ///  Summary description for Client.
  /// </summary>
  public class Client
  {
    Component SetupTree()
    {
      // here we have to create a tree structure, 
      // consisting of composites and leafs.   
      Composite root = new Composite("root-composite");
      Composite parentcomposite;
      Composite composite;
      Leaf leaf;

      parentcomposite = root;
      composite = new Composite("first level - first sibling - composite");
      parentcomposite.Add(composite);
      leaf = new Leaf("first level - second sibling - leaf");
      parentcomposite.Add(leaf);
      parentcomposite = composite; 
      composite = new Composite("second level - first sibling - composite");
      parentcomposite.Add(composite);
      composite = new Composite("second level - second sibling - composite");
      parentcomposite.Add(composite);

      // we will leaf the second level - first sibling empty, and start 
      // populating the second level - second sibling 
      parentcomposite = composite; 
      leaf = new Leaf("third level - first sibling - leaf");
      parentcomposite.Add(leaf);
      
      leaf = new Leaf("third level - second sibling - leaf");
      parentcomposite.Add(leaf);
      composite = new Composite("third level - third sibling - composite");
      parentcomposite.Add(composite);

      return root;
    }

    public static int Main(string[] args)
    {  
        Component component;
      Client c = new Client();
      component = c.SetupTree();

      component.DumpContents();
      return 0;
    }
  }
}

</div>


可以看出,Composite类型的对象可以包含其它Component类型的对象。换而言之,Composite类型对象可以含有其它的树枝(Composite)类型或树叶(Leaf)类型的对象。

合成模式的实现根据所实现接口的区别分为两种形式,分别称为安全模式和透明模式。合成模式可以不提供父对象的管理方法,但合成模式必须在合适的地方提供子对象的管理方法(诸如:add、remove、getChild等)。

透明方式

作为第一种选择,在Component里面声明所有的用来管理子类对象的方法,包括add()、remove(),以及getChild()方法。这样做的好处是所有的构件类都有相同的接口。在客户端看来,树叶类对象与合成类对象的区别起码在接口层次上消失了,客户端可以同等同的对待所有的对象。这就是透明形式的合成模式。

这个选择的缺点是不够安全,因为树叶类对象和合成类对象在本质上是有区别的。树叶类对象不可能有下一个层次的对象,因此add()、remove()以及getChild()方法没有意义,是在编译时期不会出错,而只会在运行时期才会出错。

安全方式

第二种选择是在Composite类里面声明所有的用来管理子类对象的方法。这样的做法是安全的做法,因为树叶类型的对象根本就没有管理子类对象的方法,因此,如果客户端对树叶类对象使用这些方法时,程序会在编译时期出错。

这个选择的缺点是不够透明,因为树叶类和合成类将具有不同的接口。

这两个形式各有优缺点,需要根据软件的具体情况做出取舍决定。

安全式的合成模式实现: 只有composite有Add ,remove,delete等方法.

以下示例性代码演示了安全式的合成模式代码:

// Composite pattern -- Structural example 
using System;
using System.Text;
using System.Collections;

// "Component"
abstract class Component
{
 // Fields
 protected string name;

 // Constructors
 public Component( string name )
 {
  this.name = name;
 }

 // Operation
 public abstract void Display( int depth );
}

// "Composite"
class Composite : Component
{
 // Fields
 private ArrayList children = new ArrayList();

 // Constructors
 public Composite( string name ) : base( name ) {}

 // Methods
 public void Add( Component component )
 {
  children.Add( component );
 }
 public void Remove( Component component )
 {
  children.Remove( component );
 }
 public override void Display( int depth )
 {
  Console.WriteLine( new String( '-', depth ) + name );

  // Display each of the node's children
  foreach( Component component in children )
   component.Display( depth + 2 );
 }
}

// "Leaf"
class Leaf : Component
{
 // Constructors
 public Leaf( string name ) : base( name ) {}

 // Methods
 public override void Display( int depth )
 {
  Console.WriteLine( new String( '-', depth ) + name );
 }
}

/// <summary>
/// Client test
/// </summary>
public class Client
{
 public static void Main( string[] args )
 {
  // Create a tree structure
  Composite root = new Composite( "root" );
  root.Add( new Leaf( "Leaf A" ));
  root.Add( new Leaf( "Leaf B" ));
  Composite comp = new Composite( "Composite X" );

  comp.Add( new Leaf( "Leaf XA" ) );
  comp.Add( new Leaf( "Leaf XB" ) );
  root.Add( comp );

  root.Add( new Leaf( "Leaf C" ));

  // Add and remove a leaf
  Leaf l = new Leaf( "Leaf D" );
  root.Add( l );
  root.Remove( l );

  // Recursively display nodes
  root.Display( 1 );
 }
}

</div>

 透明式的合成模式实现: 每个里都有add,remove等修改方法.
以下示例性代码演示了安全式的合成模式代码:

// Composite pattern -- Structural example 

using System;
using System.Text;
using System.Collections;

// "Component"
abstract class Component
{
 // Fields
 protected string name;

 // Constructors
 public Component( string name )
 { this.name = name; }

 // Methods
 abstract public void Add(Component c);
 abstract public void Remove( Component c );
 abstract public void Display( int depth );
}

// "Composite"
class Composite : Component
{
 // Fields
 private ArrayList children = new ArrayList();

 // Constructors
 public Composite( string name ) : base( name ) {}

 // Methods
 public override void Add( Component component )
 { children.Add( component ); }
 
 public override void Remove( Component component )
 { children.Remove( component ); }
 
 public override void Display( int depth )
 { 
  Console.WriteLine( new String( '-', depth ) + name );

  // Display each of the node's children
  foreach( Component component in children )
   component.Display( depth + 2 );
 }
}

// "Leaf"
c



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 深入理解C++之策略模式
  • C++设计模式编程中简单工厂与工厂方法模式的实例对比
  • 解析C++编程中如何使用设计模式中的状态模式结构
  • 详解C++设计模式编程中对访问者模式的运用
  • C++设计模式编程之Flyweight享元模式结构详解
  • 详解C++设计模式编程中责任链模式的应用
  • 深入剖析设计模式中的组合模式应用及在C++中的实现
  • 设计模式中的备忘录模式解析及相关C++实例应用
  • 深入解析设计模式中的适配器模式在C++中的运用
  • 全面解析设计模式中的建造者模式及相关C++实现

相关文章

  • 2017-05-28关于C++内存中字节对齐问题的详细介绍
  • 2017-05-28基于C语言实现的aes256加密算法示例
  • 2017-05-28linux C++ 获取文件绝对路径的实例代码
  • 2017-05-28C++设计模式编程中的观察者模式使用示例
  • 2017-05-28c++关键字mutable深入解析
  • 2017-05-28Linux下用Valgrind做检查(防止内存泄露)
  • 2017-05-28纯C语言:检索与周游广度深度遍历源码分享
  • 2017-05-28C++中十种内部排序算法的比较分析
  • 2017-05-28C++链表倒序实现方法
  • 2017-05-28c语言中十六进制转二进制显示的实现方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C++实现简单的希尔排序Shell Sort实例
    • c/c++实现获取域名的IP地址
    • C语言中settimeofday函数和gettimeofday函数的使用
    • 在C语言中调用C++做的动态链接库
    • c++中虚函数和纯虚函数的作用与区别
    • 使用C++制作简单的web服务器
    • C++中Cbitmap,HBitmap,Bitmap区别及联系
    • 浅谈C++的语句语法与强制数据类型转换
    • C++ 类的构造函数详解及实例
    • C语言单链表常见操作汇总

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有