亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C++ > 正文

深入剖析設計模式中的組合模式應用及在C++中的實現

2020-05-23 14:07:35
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了設計模式中的組合模式應用及在C++中的實現,組合模式可以清晰地反映出遞歸構建樹狀的組合結構,需要的朋友可以參考下
 

組合模式將對象組合成樹形結構以表示“部分-整體”的層次結構。C o m p o s i t e 使得用戶對單個對象和組合對象的使用具有一致性。

模式圖:

設計模式,組合模式,C++

適用場景:

  • 你想表示對象的部分-整體層次結構。
  • 你希望用戶忽略組合對象與單個對象的不同,用戶將統一地使用組合結構中的所有對象。

舉例:

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;    }  }}


可以看出,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 ); }}

 透明式的合成模式實現: 每個里都有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"class Leaf : Component{ // Constructors public Leaf( string name ) : base( name ) {} // Methods public override void Add( Component c ) { Console.WriteLine("Cannot add to a leaf"); } public override void Remove( Component c ) { Console.WriteLine("Cannot remove from a leaf"); } 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 ); }}

實例

再看看一個完整些的例子:

#include <iostream> #include <string> #include <list> using namespace std;  class Component { protected:   string name; public:   Component(string name)     :name(name)   {  }   virtual void AddComponent(Component *component) {  }   virtual void RemoveComponent(Component *component) {  }   virtual void GetChild(int depth)  { } };  class Leaf: public Component { public:   Leaf(string name)     :Component(name)   {  }   void AddComponent(Component *component)   {     cout<<"Leaf can't add component"<<endl;   }   void RemoveComponent(Component *component)   {     cout<<"Leaf can't remove component"<<endl;   }   void GetChild(int depth)   {     string _tmpstring(depth, '-');     cout<<_tmpstring<<name<<endl;   } };  class Composite:public Component { private:   list<Component*> _componets;  public:   Composite(string name)     :Component(name)   { }   void AddComponent(Component *component)   {     _componets.push_back(component);   }   void RemoveComponent(Component *component)   {     _componets.remove(component);   }   void GetChild(int depth)   {     string tmpstring (depth, '-');     cout<<tmpstring<<name<<endl;     list<Component*>::iterator iter = _componets.begin();     for(; iter != _componets.end(); iter++)     {       (*iter)->GetChild(depth + 2);     }   } };  int main() {   Composite *root = new Composite("root");   Leaf *leaf1 = new Leaf("leaf1");   Leaf *leaf2 = new Leaf("leaf2");   root->AddComponent(leaf1);   root->AddComponent(leaf2);    Composite *lay2 = new Composite("layer2");   Leaf *leaf4 = new Leaf("leaf4");   lay2->AddComponent(leaf4);    Composite *lay1 = new Composite("layer1");   Leaf *leaf3 = new Leaf("leaf3");   lay1->AddComponent(leaf3);   lay1->AddComponent(lay2);    root->AddComponent(lay1);    root->GetChild(1);   cout<<endl;   lay1->GetChild(1);   cout<<endl;   lay2->GetChild(1);    delete root;   delete lay1;   delete lay2;   delete leaf1;   delete leaf2;   delete leaf3;   delete leaf4;   system("pause");   return 0; } 

輸出:

設計模式,組合模式,C++



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
最近2019年日本中文免费字幕| 红桃视频成人在线观看| 欧美激情视频免费观看| 欧美日韩精品国产| 欧美xxxx14xxxxx性爽| 日韩不卡中文字幕| 2019中文在线观看| 国产精品视频资源| 亚洲高清福利视频| 日本成熟性欧美| 91成人福利在线| 欧美激情xxxx性bbbb| 国产精品丝袜视频| 欧美日韩一区二区免费在线观看| 精品国产一区二区三区久久| 亚洲精品自在久久| 亚洲精品中文字幕女同| 97婷婷涩涩精品一区| 欧美www视频在线观看| 亚洲欧美日韩一区在线| 国产日韩欧美在线播放| 国产精品久久久久秋霞鲁丝| 91亚洲人电影| 国产成人福利夜色影视| 欧美在线一区二区视频| 日韩精品免费综合视频在线播放| 国产日本欧美一区二区三区| 亚洲精品视频中文字幕| 国产亚洲精品美女| 国产成人精品视频在线观看| 亚洲综合色av| 亚洲女人天堂色在线7777| 国产欧美精品日韩精品| 国产精品第2页| 国产精品99导航| 亚洲激情视频网站| 国产成人涩涩涩视频在线观看| 97视频在线观看免费| 午夜精品久久久久久久久久久久| 久久免费国产精品1| 日韩在线观看免费网站| 欧美成人免费在线观看| 国产999精品久久久| 91久久精品国产91久久| 亚洲一区二区三区毛片| 91香蕉国产在线观看| 亚洲精品国产电影| 亚洲视频欧洲视频| 亚洲第一视频在线观看| www.精品av.com| 国产又爽又黄的激情精品视频| 欧美性生交大片免网| 午夜精品国产精品大乳美女| 蜜月aⅴ免费一区二区三区| 这里只有精品视频| 欧美在线视频一二三| 久久精品福利视频| 成人久久久久爱| 欧美日本精品在线| 欧美大片免费看| 亚洲一区二区自拍| 国产精品视频在线观看| 国产精品va在线播放我和闺蜜| 麻豆精品精华液| 亚洲男人第一网站| 欧美在线视频网站| 日韩成人中文字幕| 欧美日韩一区二区免费在线观看| 91精品久久久久久久久不口人| 亚洲国产91精品在线观看| 日韩欧美成人精品| 77777少妇光屁股久久一区| 国产精品扒开腿爽爽爽视频| 伊人青青综合网站| 国产精品久久9| 91情侣偷在线精品国产| 疯狂做受xxxx高潮欧美日本| 自拍偷拍免费精品| 97在线免费观看| 91av视频在线观看| 亚洲成人网在线观看| 亚洲国产另类久久精品| 国产精品永久免费观看| 国产精品美女久久久久久免费| 亚洲精品xxx| 中文字幕欧美日韩va免费视频| 精品美女国产在线| 欧美性开放视频| 国产精品久久77777| 日韩美女在线播放| 成人精品aaaa网站| 成人h视频在线观看播放| 成人羞羞国产免费| 久久久免费观看视频| 国内成人精品视频| 精品中文字幕在线观看| 午夜精品视频网站| 欧美日韩第一视频| 亚洲成在人线av| 日本精品一区二区三区在线播放视频| 91久久精品日日躁夜夜躁国产| 日韩av网站电影| 92看片淫黄大片欧美看国产片| 国产精品日日做人人爱| 成人黄色免费网站在线观看| 亚洲欧美日韩精品| 欧美精品精品精品精品免费| 久久天天躁狠狠躁夜夜爽蜜月| 亚洲精品自拍偷拍| 国产一区二区在线播放| 欧美激情亚洲激情| 国产美女精品视频免费观看| yellow中文字幕久久| 成人网欧美在线视频| 日韩电影免费在线观看中文字幕| 欧美极品少妇与黑人| 日本一区二区三区四区视频| 欧美精品www| 亚洲国内高清视频| 午夜精品久久久久久久久久久久| 欧美在线国产精品| 国产色综合天天综合网| 中文字幕欧美国内| 国产精品网红直播| 久久人人爽亚洲精品天堂| 日韩电影免费观看在线| 久久免费视频在线观看| 成人国产精品免费视频| 8x拔播拔播x8国产精品| 亚洲第一福利网站| 国产精品欧美激情在线播放| 亚洲激情在线视频| 亚洲成人免费网站| 欧美国产日韩一区二区| 欧美黑人xxxⅹ高潮交| 欧美激情一级欧美精品| 国产美女主播一区| 亚洲人成在线免费观看| 91在线观看免费观看| 亚洲日本欧美日韩高观看| 国产精品欧美风情| 国产亚洲福利一区| 欧美成人免费网| 国产精品视频在线观看| 97香蕉超级碰碰久久免费的优势| 欧美日韩福利视频| 福利一区视频在线观看| 久久久视频在线| 欧美资源在线观看| 国产91露脸中文字幕在线| 欧美丰满少妇xxxxx做受| 欧美午夜视频一区二区| 中文字幕久精品免费视频| 国产日韩在线看| 一区二区av在线| 97超碰蝌蚪网人人做人人爽| 久久久亚洲影院| 国产精品扒开腿做爽爽爽男男| 亚洲人成网在线播放| 超碰精品一区二区三区乱码| 久久久久成人精品| 红桃视频成人在线观看| 亚洲精品网站在线播放gif| 中文字幕国产日韩|