理解:整体与部分的层次关系,且树形关系中的所有对象都有相同的操作
代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DesignMode.Component
{
//职位
public abstract class Position
{
protected string _name;
public Position(string name)
{
_name = name;
}
public abstract void AddUnderPosition(Position post);
public abstract void RemoveUnderPosition(Position post);
public abstract string DisplayUnderPosition(int depth);
}
//管理职位
public class Manager : Position
{
private IList<Position> subs = new List<Position>();
public Manager(string name)
: base(name)
{ }
public override void AddUnderPosition(Position post)
{
subs.Add(post);
}
public override void RemoveUnderPosition(Position post)
{
subs.Remove(post);
}
public override string DisplayUnderPosition(int depth)
{
string dis = "";
dis += new String('-', depth) + _name + "\n";
foreach (Position p in subs)
{
dis += p.DisplayUnderPosition(depth + 2);
}
return dis;
}
}
//普通职员
public class Employee : Position
{
public Employee(string name)
: base(name)
{ }
public override void AddUnderPosition(Position post)
{
MessageBox.Show("不能添加下属职位");
}
public override void RemoveUnderPosition(Position post)
{
MessageBox.Show("不能移除下属职位");
}
public override string DisplayUnderPosition(int depth)
{
string dis = "";
dis += new String('-', depth) + _name + "\n";
return dis;
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
namespace DesignMode.Component
{
//职位
public abstract class Position
{
protected string _name;
public Position(string name)
{
_name = name;
}
public abstract void AddUnderPosition(Position post);
public abstract void RemoveUnderPosition(Position post);
public abstract string DisplayUnderPosition(int depth);
}
//管理职位
public class Manager : Position
{
private IList<Position> subs = new List<Position>();
public Manager(string name)
: base(name)
{ }
public override void AddUnderPosition(Position post)
{
subs.Add(post);
}
public override void RemoveUnderPosition(Position post)
{
subs.Remove(post);
}
public override string DisplayUnderPosition(int depth)
{
string dis = "";
dis += new String('-', depth) + _name + "\n";
foreach (Position p in subs)
{
dis += p.DisplayUnderPosition(depth + 2);
}
return dis;
}
}
//普通职员
public class Employee : Position
{
public Employee(string name)
: base(name)
{ }
public override void AddUnderPosition(Position post)
{
MessageBox.Show("不能添加下属职位");
}
public override void RemoveUnderPosition(Position post)
{
MessageBox.Show("不能移除下属职位");
}
public override string DisplayUnderPosition(int depth)
{
string dis = "";
dis += new String('-', depth) + _name + "\n";
return dis;
}
}
}
客户端代码:
private void btn_Component_Click(object sender, EventArgs e)
{
Manager chairman = new Manager("懂事长");
chairman.AddUnderPosition(new Employee("秘书"));
chairman.AddUnderPosition(new Employee("助理"));
Manager buss = new Manager("业务经理");
buss.AddUnderPosition(new Employee("销售员"));
Manager tech = new Manager("技术经理");
tech.AddUnderPosition(new Employee("程序员"));
chairman.AddUnderPosition(buss);
chairman.AddUnderPosition(tech);
string display = chairman.DisplayUnderPosition(1);
MessageBox.Show(display); }
{
Manager chairman = new Manager("懂事长");
chairman.AddUnderPosition(new Employee("秘书"));
chairman.AddUnderPosition(new Employee("助理"));
Manager buss = new Manager("业务经理");
buss.AddUnderPosition(new Employee("销售员"));
Manager tech = new Manager("技术经理");
tech.AddUnderPosition(new Employee("程序员"));
chairman.AddUnderPosition(buss);
chairman.AddUnderPosition(tech);
string display = chairman.DisplayUnderPosition(1);
MessageBox.Show(display); }