水果篮本质就是,由第三方发起的行为呼叫,被呼叫者需要相应这个呼叫,执行这个呼叫的具体行为操作...不同的被呼叫者有些够相应某些呼叫,有些则不能相应。
进一步说,水果篮希望达到的目的,就是先注册,后被呼叫的逻辑。其实一种呼叫就是触发某个事件,所以用简单的事件模式(Observer观察者模式)就可以实现.而C#的Delegate 是对事件模式的纯技术升华的高级产物。
于是问题就归结为:水果篮有削皮的事件,去核的事件...添加水果到水果篮的时候,只要让水果自己把自己的特色行为挂接到水果篮的对应的那个事件中就可以了,就这么简单....(当然也可以把这个挂接操作封装到一个专门的Control类来实现,这样水果就不用认识水果篮了,达到了降低关联性的目的)
从实用性来看,Delegate真是为了解决这个问题而诞生的,通过它,可以达到代码的高度可维护性和可扩展性...
delegate设计思想,本质就是:把动作行为和对象主体 完全的解偶...。也就是说delegate只关心动作本身,不关心动作的对象主体是什么。似乎有类似泛型的感觉...
说句题外话,敏捷软件开发中的那些原则是对如何达到代码可维护性及可扩展性的高度概括.而设计模式仅仅只是在遵循高度概括的具体实例而已。在考虑水果篮等类似的问题时,需要把握问题的本质,有些可以实现接口,有些不能定义接口。如果这里把不同的呼叫作为接口来定义,那么就陷入的为OO而OO的歧途上去了...... 封装成OO并不是绝对的....
具体实例代码稍后补上...
public abstract class Fruit
{
}
public class Apple : Fruit
{
private string m_ConcreteActionName = "苹果完成去皮";
public void Husk()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public class Pear : Fruit
{
private string m_ConcreteActionName = "梨完成去皮";
public void Husk()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public class Watermelon : Fruit
{
private string m_ConcreteActionName = "西瓜完成去子";
public void CleanSeeds()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public delegate void HustHandler();
public delegate void CleanSeedHandler();
public class FruitBasket
{
private ArrayList m_FruitList = new ArrayList();
public int AddFruit(Fruit aFruit)
{
if (null == aFruit)
return m_FruitList.Count;
if (m_FruitList.IndexOf(aFruit) < 0)
m_FruitList.Add(aFruit);
return m_FruitList.Count;
}
public event HustHandler Hust ;
public event CleanSeedHandler CleanSeed ;
public void DoHust()
{
if (null == Hust)
return ;
Hust();
}
public void DoCleanSeed()
{
if (null == CleanSeed)
return;
CleanSeed();
}
}
public class FruitBasketAssistant
{
public FruitBasket InitializeFruitBasket()
{
FruitBasket lFB = new FruitBasket();
Apple lApple = new Apple();
lFB.AddFruit(lApple);
lFB.Hust += new HustHandler(lApple.Husk);
Pear lPeer = new Pear();
lFB.AddFruit(lPeer);
lFB.Hust += new HustHandler(lPeer.Husk);
Watermelon lWatermelon = new Watermelon();
lFB.AddFruit(lWatermelon);
lFB.CleanSeed += new CleanSeedHandler(lWatermelon.CleanSeeds);
return lFB;
}
[STAThread]
static void Main(string[] args)
{
FruitBasketAssistant lassistant = new FruitBasketAssistant();
Console.WriteLine("开始装水果");
FruitBasket lFB = lassistant.InitializeFruitBasket();
Console.WriteLine("开始把水果篮中的水果削皮");
lFB.DoHust();
Console.WriteLine("开始把水果篮中的水果去子");
lFB.DoCleanSeed();
Console.ReadLine();
}
}
{
}
public class Apple : Fruit
{
private string m_ConcreteActionName = "苹果完成去皮";
public void Husk()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public class Pear : Fruit
{
private string m_ConcreteActionName = "梨完成去皮";
public void Husk()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public class Watermelon : Fruit
{
private string m_ConcreteActionName = "西瓜完成去子";
public void CleanSeeds()
{
Console.WriteLine(m_ConcreteActionName);
}
}
public delegate void HustHandler();
public delegate void CleanSeedHandler();
public class FruitBasket
{
private ArrayList m_FruitList = new ArrayList();
public int AddFruit(Fruit aFruit)
{
if (null == aFruit)
return m_FruitList.Count;
if (m_FruitList.IndexOf(aFruit) < 0)
m_FruitList.Add(aFruit);
return m_FruitList.Count;
}
public event HustHandler Hust ;
public event CleanSeedHandler CleanSeed ;
public void DoHust()
{
if (null == Hust)
return ;
Hust();
}
public void DoCleanSeed()
{
if (null == CleanSeed)
return;
CleanSeed();
}
}
public class FruitBasketAssistant
{
public FruitBasket InitializeFruitBasket()
{
FruitBasket lFB = new FruitBasket();
Apple lApple = new Apple();
lFB.AddFruit(lApple);
lFB.Hust += new HustHandler(lApple.Husk);
Pear lPeer = new Pear();
lFB.AddFruit(lPeer);
lFB.Hust += new HustHandler(lPeer.Husk);
Watermelon lWatermelon = new Watermelon();
lFB.AddFruit(lWatermelon);
lFB.CleanSeed += new CleanSeedHandler(lWatermelon.CleanSeeds);
return lFB;
}
[STAThread]
static void Main(string[] args)
{
FruitBasketAssistant lassistant = new FruitBasketAssistant();
Console.WriteLine("开始装水果");
FruitBasket lFB = lassistant.InitializeFruitBasket();
Console.WriteLine("开始把水果篮中的水果削皮");
lFB.DoHust();
Console.WriteLine("开始把水果篮中的水果去子");
lFB.DoCleanSeed();
Console.ReadLine();
}
}
请多多指教!