- 组合模式:组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 “组合对象” 的含义。组合模式可以让客户端像修改配置文件一样简单的完成本来需要流程控制语句来完成的功能。
例子:
水果盘-
在水果盘(Plate)中有一些水果,如苹果(Apple)、香蕉(Banana)、梨子(Pear),当然大水果盘中还可以有小水果盘,现需要对盘中的水果进行遍历(吃),当然如果对一个水果盘执行“吃”方法,实际上就是吃其中的水果。使用组合模式设计类图并编程实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 组合模式
{
class Program
{
static void Main(string[] args)
{
Apple apple = new Apple("苹果");
Pear pear = new Pear("梨");
Banana banana = new Banana("香蕉");
BigDish b = new BigDish("大水果盘");
SmallDish smalldish = new SmallDish("小水果盘");
b.Add(apple);
b.Add(pear);
b.Add(banana);
smalldish.Add(apple);
smalldish.Add(pear);
smalldish.Add(banana);
b.Eat();
smalldish.Eat();
Console.ReadKey();
}
}
abstract class fruit
{
public abstract void Add(fruit f);
public abstract void Remove(fruit f);
public abstract fruit GetChild(int i);
public abstract void Eat();
}
class Apple : fruit
{
private string name;
public Apple(string name)
{
this.name = name;
}
public override void Add(fruit f)
{
// throw new NotImplementedException();
Console.WriteLine("不支持该方法");
}
public override void Remove(fruit f)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
}
public override fruit GetChild(int i)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
return null;
}
public override void Eat()
{
Console.WriteLine("吃{0}",name);
}
}
class Pear:fruit
{
private string name;
public Pear(string name)
{
this.name = name;
}
public override void Add(fruit f)
{
// throw new NotImplementedException();
Console.WriteLine("不支持该方法");
}
public override void Remove(fruit f)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
}
public override fruit GetChild(int i)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
return null;
}
public override void Eat()
{
Console.WriteLine("吃{0}", name);
}
}
class Banana:fruit
{
private string name;
public Banana(string name)
{
this.name=name;
}
public override void Add(fruit f)
{
// throw new NotImplementedException();
Console.WriteLine("不支持该方法");
}
public override void Remove(fruit f)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
}
public override fruit GetChild(int i)
{
Console.WriteLine("不支持该方法");
//throw new NotImplementedException();
return null;
}
public override void Eat()
{
Console.WriteLine("吃{0}", name);
}
}
class BigDish:fruit
{
private List<fruit> fruitList = new List<fruit>();
private string name;
public BigDish(string name)
{
this.name = name;
}
public override void Add(fruit f)
{
// throw new NotImplementedException();
fruitList.Add(f);
}
public override void Remove(fruit f)
{
fruitList.Remove(f);
//throw new NotImplementedException();
}
public override fruit GetChild(int i)
{
return fruitList[i];
}
public override void Eat()
{
Console.WriteLine("吃盘子里面的水果");
foreach(fruit a in fruitList)
{
a.Eat();
}
}
}
class SmallDish : fruit
{
private string name;
private List<fruit> fruitList = new List<fruit>();
public SmallDish(string name)
{
this.name = name;
}
public override void Add(fruit f)
{
// throw new NotImplementedException();
fruitList.Add(f);
}
public override void Remove(fruit f)
{
fruitList.Remove(f);
//throw new NotImplementedException();
}
public override fruit GetChild(int i)
{
return fruitList[i];
}
public override void Eat()
{
Console.WriteLine("吃盘子里面的水果");
foreach (fruit a in fruitList)
{
a.Eat();
}
}
}
}
如有错误之处还请指正!