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

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

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

2020-01-26 14:41:53
字體:
來源:轉載
供稿:網友

組合模式將對象組合成樹形結構以表示“部分-整體”的層次結構。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;    }  }}


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

輸出:

201631193949446.png (681×439)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲性视频网站| 97精品在线视频| 国产成人精品在线播放| 欧美性感美女h网站在线观看免费| 亚洲免费高清视频| 91精品国产综合久久久久久久久| 2019av中文字幕| 日本三级韩国三级久久| 亚洲欧美国产精品久久久久久久| 亚洲国产91精品在线观看| 国产精品wwwwww| 成人网中文字幕| 久久九九全国免费精品观看| 亚洲国产日韩欧美在线动漫| 精品久久久香蕉免费精品视频| 欧美黑人一区二区三区| 欧美大尺度激情区在线播放| 中文字幕日韩在线视频| 欧美一区二区色| 国产91免费看片| 日韩人体视频一二区| 久久久精品国产网站| 视频在线观看99| 欧美资源在线观看| 欧美成人中文字幕在线| 久久久久久久久久久国产| 亚洲精品久久久久国产| 国产精品女主播| 日韩电视剧在线观看免费网站| 午夜精品一区二区三区在线视频| 一道本无吗dⅴd在线播放一区| 国产精品午夜一区二区欲梦| 国产精品永久免费| 91久久久久久久久久久久久| 国产亚洲精品美女久久久久| 亚洲人a成www在线影院| 亚洲成人久久电影| 精品国产91久久久久久老师| 91久久久久久久久久久久久| 亚洲人成网站在线播| 国产精品成人在线| 精品视频久久久久久久| 日本久久中文字幕| 深夜福利国产精品| 成人午夜小视频| 久久精品国产69国产精品亚洲| 午夜精品久久久久久久久久久久| 久久视频免费在线播放| 国产91成人在在线播放| 国产成人精品日本亚洲专区61| 日韩在线观看成人| 欧美日韩亚洲视频| 午夜精品在线视频| 中文字幕在线精品| 伊人伊人伊人久久| 91a在线视频| 国产精品一区二区三区免费视频| 中文字幕久热精品视频在线| 美女福利精品视频| 久久全球大尺度高清视频| 国内精品久久久久影院优| 国产日产欧美a一级在线| 久久久午夜视频| 日韩精品中文字幕视频在线| 国产精品av免费在线观看| 一本色道久久88综合亚洲精品ⅰ| 日韩在线视频线视频免费网站| 国产成人一区二区三区小说| 亚洲高清福利视频| 国产日韩精品在线| 久久精品99国产精品酒店日本| 97精品一区二区视频在线观看| 亚洲国产成人精品久久| 欧美大码xxxx| 午夜精品一区二区三区在线视频| 97在线视频免费| 懂色av中文一区二区三区天美| 欧美午夜影院在线视频| 欧美精品18videosex性欧美| 日韩成人在线电影网| 精品国产依人香蕉在线精品| 亚洲成年人在线| 日本精品久久久| 亚洲人成网站999久久久综合| 国产精品香蕉在线观看| 日本乱人伦a精品| 国产一区二区三区直播精品电影| 国内免费久久久久久久久久久| 久久99久国产精品黄毛片入口| 91免费精品国偷自产在线| 欧美激情精品久久久久久| 日韩精品久久久久久福利| 欧美久久精品一级黑人c片| 国产在线观看91精品一区| 欧美极品欧美精品欧美视频| 亚洲欧美精品一区| 91免费欧美精品| 精品久久久久久久久中文字幕| 欧美日韩国产一区中文午夜| 日韩欧美国产一区二区| 国产精品99久久99久久久二8| 亚洲精品www久久久久久广东| 久久久久久久久91| 中文字幕精品网| 日韩欧中文字幕| 国产精品美女久久久久久免费| 91影视免费在线观看| 欧美自拍视频在线| 亚洲天堂av图片| 在线成人中文字幕| 亚洲japanese制服美女| 国产一区二区丝袜| 中文字幕一精品亚洲无线一区| 亚洲国产精品电影在线观看| 欧美日韩在线视频首页| 成人信息集中地欧美| 精品综合久久久久久97| 国产精品一区二区三区久久| 国产一区二区三区网站| 国产99在线|中文| 有码中文亚洲精品| 日本一区二区三区在线播放| 欧洲亚洲在线视频| 国产97在线|亚洲| 亚洲一区精品电影| 538国产精品一区二区在线| xvideos亚洲人网站| 国产啪精品视频| 国产丝袜一区二区| 国产精品久久9| 在线日韩日本国产亚洲| 视频在线一区二区| 国产精品国内视频| 亚洲人成网在线播放| 欧美日韩在线视频首页| 久久99精品久久久久久青青91| 欧美俄罗斯性视频| 国产啪精品视频| 国产成人免费91av在线| 欧美国产日本高清在线| 欧美日韩中文字幕在线视频| 亚洲精品一区在线观看香蕉| 97久久伊人激情网| 欧美视频在线免费| 日本一区二区三区四区视频| 亚洲国产精品成人va在线观看| 992tv成人免费影院| 91最新国产视频| 国产成人一区二| 日韩中文字幕国产精品| 日韩在线www| 成人xxxx视频| 中文字幕日韩av综合精品| 亚洲综合大片69999| 亚洲国产91精品在线观看| 欧美日韩国产色| 日韩在线高清视频| 日韩在线播放一区| 欧美大尺度电影在线观看| 91精品国产777在线观看| 久久国产视频网站| 久久视频在线看| 欧美午夜www高清视频| 成人在线视频网站|