组合模式
1组合模式是一个树状结构,根节点有子节点,子节点又可以作为根节点,有它自己的子节点.
2 应用场景的话,比如一个公司.员工作为结点,那么 总经理包含多名副经理和助手,副经理包含多名部长,部长包含多名科长\副科长等
3 重点是,要组合的模型类里面,它含有一个类型为自己的list变量.一层套一层的感觉
public class YanghuiNumber
{
public int[,] position;
public int value;
public List<YanghuiNumber> subMembers ;
public YanghuiNumber(int value,int[,] position)
{
this.value = value;
this.position = position;
subMembers = new List<YanghuiNumber>();
}
public void Add(YanghuiNumber number)
{
subMembers.Add(number);
}
public void Des(YanghuiNumber number)
{
subMembers.Remove(number);
}
}
public class YanghuiTriangle
{
public static List<YanghuiNumber> GenerateYanghuiTriangle()
{
List<YanghuiNumber> yangHui = new List<YanghuiNumber>();
var yanghui1 = new YanghuiNumber(2, new int[3, 2]);
yanghui1.Add(new YanghuiNumber(1, new int[2,2]));
yanghui1.Add(new YanghuiNumber(1, new int[2,1]));
var yanghui2 = new YanghuiNumber(3, new int[4, 2]);
yanghui2.Add(new YanghuiNumber(1, new int[3,1]));
yanghui2.Add(new YanghuiNumber(2, new int[3,2]));
yangHui.Add(yanghui1);
yangHui.Add(yanghui2);
return yangHui;
}
}