透明式的合成模式实现: 每个里都有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;
}










