程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 深刻分析設計形式中的組合形式運用及在C++中的完成

深刻分析設計形式中的組合形式運用及在C++中的完成

編輯:關於C++

深刻分析設計形式中的組合形式運用及在C++中的完成。本站提示廣大學習愛好者:(深刻分析設計形式中的組合形式運用及在C++中的完成)文章只能為提供參考,不一定能成為您想要的結果。以下是深刻分析設計形式中的組合形式運用及在C++中的完成正文


組合形式將對象組分解樹形構造以表現“部門-全體”的條理構造。C o m p o s i t e 使得用戶對單個對象和組合對象的應用具有分歧性。

形式圖:

實用場景:

  • 你想表現對象的部門-全體條理構造。
  • 你願望用戶疏忽組合對象與單個對象的分歧,用戶將同一地應用組合構造中的一切對象。

舉例:

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

輸入:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved