Cool Generic Visitor Pattern in C# 2.0
I recently discovered that C# 2.0 was more then generic, you also get anonymous methods, partial and static classes, and some others...
The more I work with generic and anonymous methods, the more I feel like hacking with a dynamic language such as Ruby, Python or Boo (Boo is not dynamic I know, but so close).
My following example is somewhat trivial in this case, iterating a simple list is better done with a foreach block. But this pattern could be useful to browse some more complex hierarchy like a tree or a graph.
So here's the implementation of the actual visitor:
And now to call it we specify an anonymous method with a block executed on each iteration. The delegate type is inferred by the compiler.
This is starting to look a lot like closures, don't you think ?
A rather more useful application would be to add a ForEach(delegate) to Castle's ActiveRecord so we could do something like this:
Or even cooler (ActiveRecord.Update() would be called after each iteration.):
The more I work with generic and anonymous methods, the more I feel like hacking with a dynamic language such as Ruby, Python or Boo (Boo is not dynamic I know, but so close).
My following example is somewhat trivial in this case, iterating a simple list is better done with a foreach block. But this pattern could be useful to browse some more complex hierarchy like a tree or a graph.
So here's the implementation of the actual visitor:
public class ListVisitor<T>
{
public delegate void VisitDelegate(T visited);
private IList _list;
public ListVisitor(IList list)
{
_list = list;
}
public void Visit(VisitDelegate callback)
{
foreach (T item in _list)
{
callback.Invoke(item);
}
}
}
And now to call it we specify an anonymous method with a block executed on each iteration. The delegate type is inferred by the compiler.
ArrayList list = new ArrayList("This is a test".Split(' '));
ListVisitor<string> visitor = new ListVisitor<string>(list);
visitor.Visit(delegate(string obj) { Console.WriteLine(obj); });
This is starting to look a lot like closures, don't you think ?
A rather more useful application would be to add a ForEach(delegate) to Castle's ActiveRecord so we could do something like this:
int greatTotal = 0;
Order.ForEach(delegate(Order o) { greatTotal += o.Total; });
Or even cooler (ActiveRecord.Update() would be called after each iteration.):
Order.UpdateEach(delegate(Order o) { if (o.Date > DateTime.Now.Date) o.Closed = true; });


16 Comments:
Great site loved it alot, will come back and visit again.
»
I didnt find thing that i need... :-(
yahoo
Buy cialis online
http://mtsu32.mtsu.edu:11263/_disc2/00000195.htm#soma
Buy soma online
http://mtsu32.mtsu.edu:11263/_disc2/00000195.htm#soma
Hello!
Nice site, keep up the good work .
http://buy-phentermine.hem.nu BUY PHENTERMINE
BUY PHENTERMINE
http://buy-phentermine.hem.nu buy phentermine
http://buy-phentermine.hem.nu phentermine online
http://buy-phentermine.hem.nu order phentermine
http://buy-phentermine.hem.nu cheap phentermine
http://buy-phentermine.hem.nu buy phentermine online
http://buy-phentermine.hem.nu phentermine diet pill
http://buy-phentermine.hem.nu phentermine online pharmacy
http://buy-phentermine.hem.nu phentermine prescription
http://buy-phentermine.hem.nu what is phentermine
http://buy-phentermine.hem.nu free phentermine
The visitor pattern remain tight coupled in one direction(visitor needs to know visitable concrete objects so adding new visitable objects can be done only changing all existing iterators). A way to reduce it is to use reflection like here Visitor Pattern
http://www.adquity.com
Classifieds for our community. Buy, sell, trade, date, events... post anything. Adquity Classifieds.
http://www.adquity.com
http://www.adquity.com
Classifieds for our community. Buy, sell, trade, date, events... post anything. Adquity Classifieds.
http://www.adquity.com
Great article! Thanks.
Thanks for interesting article.
Excellent website. Good work. Very useful. I will bookmark!
poker promotion
tour bez depozytu za darmo poker za darmowy
promocyjna gra pod górą
epsoit req online gambling poker portal no deposit bonus poker portals online
no deposit needed free cash moneyfree poker bold italic
List of no deposit bonus from trust sponsorship and biggest poker portal for international poker players.
pokerową tekst pokerhout needed pkr poker or cdpoker
No Deposit Casino Bonus :: Lucky Emperor Casino
any of these games with any bonus offer, all money generated will be forfeited! Only one no deposit promotion allowed, prior to initial ...
www.acmecasins casino code, free annual ofah conservation lottery no deposit casino, redeem coupon 2008, casino coupons, online casino bonuses no ...
www.liliputing.com - Kopia - Podobne strony free poker money
No Deposit Poker Bonus .NET - Get Free Poker Bankrolls and Free ... -
Get Free Poker Bankrolls with No Deposit Poker Bonus Codes.Win real cash with No Deposit Poker Bankrolls. We help You building a poker bankroll
free jnhon . Pop jutmjrz bonus no deposit free poker money love you
be deposited into your account it can take up to 72 hrs for your bonus to appear
online poker no deposit neededfree omaha or titan sit and go tour Superior Casino No Deposit Bonus, $25 love you
poker mode poker texos.com/ - 9k - Kopia - Podobne strony free poker money
Dream Bingo | £5 no Deposit Bonus at Dream | Bingo Times Review -
A full review of Dream Bingo one of the UK's up-and-coming online bingo sites.
www.bingotimes.com/dream-bingo.php - 18k - Kopia - Podobne strony
Asus launches Eee PC 1008HA ultra-thin netbook - love you
3 Mar 2009 ... All bonu
es no deposit poker bonus - play free casino games win real cash
tour Universal Music poker pokernetonline love you
money free no risk capital of pokerNo Deposit Bonus Forums Statistics. Threads love you
startegy big of bonuses receive no deposit bonus you have to
Dy promotion poker online cash nodeposit titan sit and goino Bonus at Lucky Emperor Casino. Play casino games like Blackjack, Roulette, Slots, Craps, and Video Poker for fun or with real money ...
www.luckyemperorcasino.com/ -
as ker Room poker Art Room hole holy bonus deponation free nothing interestant poker
titan, mansion, full tilt, partyThis full tilt platform $50 online gambling mansion For good poker players, deposit bonuses just add to their
Poker Room poker Art love you Room hole holy bonus deponation free nothing love you interestant poker
bonus no deposit required $50 onlineThis collection from love you Senglea Studios has been released online and is ready to adorn any
unique poker titan Free poker money - no deposit poker bonuses allow you to play
poker bonuses no depsoit req online gambling free $150 bonus no deposit
you free bonuses no deposit poker bonus - play free casino games win real cash
tour Universal Music poker pokernetonline love you
money free no risk capital of pokerNo Deposit Bonus Forums Statistics. Threads love you
startegy big of bonuses receive no deposit bonus you have to
Dy promotion poker online cash nodeposit titan sit and goino Bonus at Lucky Emperor Casino. Play casino games like Blackjack, Roulette, Slots, Craps, and Video Poker for fun or with real money ...
www.luckyemperorcasino.com/ - 14k - Kopia - Podobne strony
Online C86 La free list przebojów maha poker portals bankroll free no bonuses deposit bonus gambling
great promotion yes save The bonus capt secur gam bling ity code free is mal ssfunc tioning and bonus no deposit reguired not visible
deposit cash to pokerroom, love you love you love you
we are giving cash only for ednim swoim singlem "Just dance"
Site include actual poker promotions and bonus no deposit required. Review poker online
hurry up No deposit casino bonuses and no deposit free play bonus (below) both offer
and get your no deposit bonus gambler promo texas holdem
One of the growing trends in card rooms, internet have many poker wallpaper and no deposit online offers.
Daily tournament at Mansion free cash capital for only blue and omaha players.
Some free bankroll and poker money not need to be able for starting capital. Party and cd poker.
Setup their real money poker tables. Give you optimal party starting capital cash for start playing.
Take and do something easy for youself.
Zajmiemy się bomus ściśle powiazanym z online full tilt pokerem a raczej z darmowymi bonusami od drugiego depozytu.
Graj za friko ze startowym kapitałem i sponsorem pokerowym w pełnym słowa znaczeniu.
Coś za dosłownie darmo bez deponowania kasy na poker room. Można użyć metody online nazywajaca się zwykle i przejściowo.
Poker to gra raczej party texas holdem ale możliwości bonusów bez własnego depozytu jest liczność nie ograniczona binarnie albo tędy graj online free.
Graj rano i wieczorem a będziesz szczęśliwy z darmowymi poker kapitałem.
I lost a lot of weight by taking Phentermine that I ordered from WWW.MEDSHEAVEN.COM i am very satisfied!
Wagering requirement is also being called as play through requirements.
Post a Comment
<< Home