My Composite in C#
My Composite in C#,My Composite in C#
//MyComposite
using System;
using System.Collections;
//----------------------------------Class FileElement
abstract class CFileElement
{
//Fields
protected string name;
public CFileElement(string name)
{
this.name=name;
}
public abstract void Add(CFileElement e);
public abstract void Remove(CFileElement e);
public abstract void Display();
};
//Class CFile
class CFile:CFileElement
{
public CFile(string name):base(name){}
public override void Add(CFileElement e)
{
Console.WriteLine("Can't add any files under a file!");
}
public override void Remove(CFileElement e)
{
Console.WriteLine("Can't remove any sub files!");
}
public override void Display()
{
Console.WriteLine(this.name);
}
};
//Class CFolder
class CFolder:CFileElement
{
private ArrayList children =new ArrayList();
public CFolder(string name):base(name){}
public override void Add(CFileElement e)
{
children.Add(e);
}
public override void Remove(CFileElement e)
{
children.Remove(e);
}
public override void Display()
{
Console.WriteLine(this.name);
foreach(CFileElement e in children)
{
e.Display();
}
}
};
//----------------------------------Class Element
//TestApp
class TestApp
{
public static void Main(string[] args)
{
CFolder root = new CFolder("Z:/");
CFolder afolder=new CFolder("Games/");
afolder.Add(new CFile("cstrike.exe"));
root.Add(afolder);
root.Display(); while(true); }};