我使用事件作爲遊戲模型的一部分,對於可擴展性和代碼「本地化」的緣故,我需要能夠否決大多數操作。有沒有實現「可選擇」事件的標準方法?
更清楚,幾乎具有副作用,每個方法採用這種形式:
public event TryingToDoSomethingHandler TryingToDoSomething;
public event SomethingHappenedHandler SomethingHappened;
/*
* Returning true indicates Something happened successfully.
*/
public bool DoSomething(...)
{
//Need a way to indicate "veto" here
TryingToDoSomething(...);
//Actual do it
SomethingHappened(...);
return true;
}
我想什麼是TryingToDoSomething(...),以便能夠表明一個註冊的事件處理程序的對象(通過返回false,修改out參數或其他)。因此,該代碼在道德上等同於:
/*
* Returning true indicates Something happened successfully.
*/
public bool DoSomethingImproved(...)
{
//Pretty sure multicast delegates don't work this way, but you get the idea
if(!TryingToDoSomething(...)) return false;
//Actual do it
SomethingHappened(...);
return true;
}
在C#/ .NET中是否存在可接受的或標準的方法?
我的Java(命名)根讓我再次陷入困境。這正是我所期待的。 – 2009-06-23 23:13:29