/*
* User: Administrator
* Date: 2007-7-3 Time: 10:26
*/
using System;
using System.Collections;
namespace TreeViewVisitable
{
public interface IVisitable{
void Accept (Visitor v);
}
public abstract class Visitor{
public Visitor(){}
public abstract void VisitCompany(Company c);
public abstract void VisitDepartment(Department c);
public abstract void VisitEmployee(Employee e);
}
public class Company:IVisitable{
private string name;
private ArrayList myDepartments;
public Company(){
myDepartments=new ArrayList();
}
public ArrayList Departments{
get{return myDepartments;}
}
public string CompanyName{
get{return name;}
set{name=value;}
}
#region IVisitalbe 成员
public void Accept(Visitor v){
v.VisitCompany(this);
}
#endregion
}
public class Department:IVisitable{
private string name;
private ArrayList myEmployees;
private Employee dLeader;
public Department(){
myEmployees=new ArrayList();
}
public string DepartName{
get{return name;}
set{name=value;}
}
public Employee Leader
{
get{return dLeader;}
set{dLeader=value;}
}
public ArrayList Employee
{
get{return myEmployees;}
set{myEmployees=value;}
}
#region IVisitable 成员
public void Accept(Visitor v)
{
v.VisitDepartment(this);
}
#endregion
}
public class Employee:IVisitable
{
public Employee(){}
private string name;
public string Name{
get{return name;}
set{name=value;}
}
#region IVisitable 成员
public void Accept(Visitor v){
v.VisitEmployee(this);
}
#endregion
}
public class NameVisitor:Visitor
{
public NameVisitor(){}
private string name;
public string Name{get{return name;}}
public override void VisitCompany(Company c)
{
name=c.CompanyName;
}
public override void VisitDepartment(Department c)
{
name=c.DepartName;
//base.VisitDepartment(c);
}
public override void VisitEmployee(Employee e)
{
name=e.Name;
//base.VisitEmployee(e);
}
}
public class DetailVisitor:Visitor{
public DetailVisitor(){}
private string detail;
public string Detail
{
get{return detail;}
}
public override void VisitCompany(Company c)
{
detail=string.Empty;
detail+=c.CompanyName+" ";
detail+="共有"+c.Departments.Count.ToString()+"个部门";
//base.VisitCompany(c);
}
public override void VisitDepartment(Department c)
{
detail=string.Empty;
detail+=c.DepartName+" ";
detail+="共有"+c.Employee.Count+"个员工";
detail+="领导是"+c.Leader.Name;
//base.VisitDepartment(c);
}
public override void VisitEmployee(Employee e)
{
detail=string.Empty;
detail+="员工的姓名是"+e.Name;
//base.VisitEmployee(e);
}
}
}
private ArrayList dp=new ArrayList();
private NameVisitor nv=new NameVisitor();
private DetailVisitor dv=new DetailVisitor();
void Button1Click(object sender, EventArgs e)
{
try
{
Company c=new Company();
c.CompanyName="测试公司";
Department d=new Department();
d.DepartName="软件部";
Employee e1=new Employee();
e1.Name="佳艺";
d.Leader=e1;
d.Employee.Add(e);
Employee e2=new Employee();
e2.Name="e2";
d.Employee.Add(e2);
Employee e3=new Employee();
e3.Name="e3";
d.Employee.Add(e3);
c.Departments.Add(d);
dp.Add(c);
ShowCompanies();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ShowCompanies()
{
foreach(Company c in dp)
{
c.Accept(nv);
string name=nv.Name;
TreeNode n=treeView1.Nodes.Add(name);
n.Tag=c;
ShowDepartments(c,n);
}
}
private void ShowDepartments(Company c ,TreeNode on)
{
foreach(Department d in c.Departments){
d.Accept(nv);
string name=nv.Name;
TreeNode n=on.Nodes.Add(name);
n.Tag=d;
ShowEmployee(d,n);
}
}
private void ShowEmployee(Department d,TreeNode n)
{
foreach(Employee e in d.Employee){
d.Accept(nv);
string name=nv.Name;
TreeNode on=n.Nodes.Add(name);
on.Tag=e;
}
}
void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
{
IVisitable cde=(IVisitable)e.Node.Tag;
cde.Accept(dv);
MessageBox.Show(dv.Detail);
}
* User: Administrator
* Date: 2007-7-3 Time: 10:26
*/
using System;
using System.Collections;
namespace TreeViewVisitable
{
public interface IVisitable{
void Accept (Visitor v);
}
public abstract class Visitor{
public Visitor(){}
public abstract void VisitCompany(Company c);
public abstract void VisitDepartment(Department c);
public abstract void VisitEmployee(Employee e);
}
public class Company:IVisitable{
private string name;
private ArrayList myDepartments;
public Company(){
myDepartments=new ArrayList();
}
public ArrayList Departments{
get{return myDepartments;}
}
public string CompanyName{
get{return name;}
set{name=value;}
}
#region IVisitalbe 成员
public void Accept(Visitor v){
v.VisitCompany(this);
}
#endregion
}
public class Department:IVisitable{
private string name;
private ArrayList myEmployees;
private Employee dLeader;
public Department(){
myEmployees=new ArrayList();
}
public string DepartName{
get{return name;}
set{name=value;}
}
public Employee Leader
{
get{return dLeader;}
set{dLeader=value;}
}
public ArrayList Employee
{
get{return myEmployees;}
set{myEmployees=value;}
}
#region IVisitable 成员
public void Accept(Visitor v)
{
v.VisitDepartment(this);
}
#endregion
}
public class Employee:IVisitable
{
public Employee(){}
private string name;
public string Name{
get{return name;}
set{name=value;}
}
#region IVisitable 成员
public void Accept(Visitor v){
v.VisitEmployee(this);
}
#endregion
}
public class NameVisitor:Visitor
{
public NameVisitor(){}
private string name;
public string Name{get{return name;}}
public override void VisitCompany(Company c)
{
name=c.CompanyName;
}
public override void VisitDepartment(Department c)
{
name=c.DepartName;
//base.VisitDepartment(c);
}
public override void VisitEmployee(Employee e)
{
name=e.Name;
//base.VisitEmployee(e);
}
}
public class DetailVisitor:Visitor{
public DetailVisitor(){}
private string detail;
public string Detail
{
get{return detail;}
}
public override void VisitCompany(Company c)
{
detail=string.Empty;
detail+=c.CompanyName+" ";
detail+="共有"+c.Departments.Count.ToString()+"个部门";
//base.VisitCompany(c);
}
public override void VisitDepartment(Department c)
{
detail=string.Empty;
detail+=c.DepartName+" ";
detail+="共有"+c.Employee.Count+"个员工";
detail+="领导是"+c.Leader.Name;
//base.VisitDepartment(c);
}
public override void VisitEmployee(Employee e)
{
detail=string.Empty;
detail+="员工的姓名是"+e.Name;
//base.VisitEmployee(e);
}
}
}
private ArrayList dp=new ArrayList();
private NameVisitor nv=new NameVisitor();
private DetailVisitor dv=new DetailVisitor();
void Button1Click(object sender, EventArgs e)
{
try
{
Company c=new Company();
c.CompanyName="测试公司";
Department d=new Department();
d.DepartName="软件部";
Employee e1=new Employee();
e1.Name="佳艺";
d.Leader=e1;
d.Employee.Add(e);
Employee e2=new Employee();
e2.Name="e2";
d.Employee.Add(e2);
Employee e3=new Employee();
e3.Name="e3";
d.Employee.Add(e3);
c.Departments.Add(d);
dp.Add(c);
ShowCompanies();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void ShowCompanies()
{
foreach(Company c in dp)
{
c.Accept(nv);
string name=nv.Name;
TreeNode n=treeView1.Nodes.Add(name);
n.Tag=c;
ShowDepartments(c,n);
}
}
private void ShowDepartments(Company c ,TreeNode on)
{
foreach(Department d in c.Departments){
d.Accept(nv);
string name=nv.Name;
TreeNode n=on.Nodes.Add(name);
n.Tag=d;
ShowEmployee(d,n);
}
}
private void ShowEmployee(Department d,TreeNode n)
{
foreach(Employee e in d.Employee){
d.Accept(nv);
string name=nv.Name;
TreeNode on=n.Nodes.Add(name);
on.Tag=e;
}
}
void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
{
IVisitable cde=(IVisitable)e.Node.Tag;
cde.Accept(dv);
MessageBox.Show(dv.Detail);
}