欢迎来到天天文库
浏览记录
ID:43423508
大小:148.00 KB
页数:47页
时间:2019-10-08
《代理模式( Proxy Pattern )》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、代理模式(ProxyPattern)没有代理的代码classPursuit{SchoolGirlmm;publicPursuit(SchoolGirlmm){this.mm=mm;}publicvoidGiveDolls(){Console.WriteLine(mm.Name+"送你洋娃娃");}publicvoidGiveFlowers(){Console.WriteLine(mm.Name+"送你鲜花");}publicvoidGiveChocolate(){Console.WriteLine(mm.Na
2、me+"送你巧克力");}}classSchoolGirl{privatestringname;publicstringName{get{returnname;}set{name=value;}}}staticvoidMain(string[]args){SchoolGirljiaojiao=newSchoolGirl();jiaojiao.Name="李娇娇";Pursuitzhuojiayi=newPursuit(jiaojiao);zhuojiayi.GiveDolls();zhuojiayi.Give
3、Flowers();zhuojiayi.GiveChocolate();Console.Read();}只有代理的代码classProxy{SchoolGirlmm;publicProxy(SchoolGirlmm){this.mm=mm;}publicvoidGiveDolls(){Console.WriteLine(mm.Name+"送你洋娃娃");}publicvoidGiveFlowers(){Console.WriteLine(mm.Name+"送你鲜花");}publicvoidGiveChoco
4、late(){Console.WriteLine(mm.Name+"送你巧克力");}}classSchoolGirl{privatestringname;publicstringName{get{returnname;}set{name=value;}}}staticvoidMain(string[]args){SchoolGirljiaojiao=newSchoolGirl();jiaojiao.Name="李娇娇";Proxydaili=newProxy(jiaojiao);daili.GiveDoll
5、s();daili.GiveFlowers();daili.GiveChocolate();Console.Read();}分析:礼物是Pursuit(追求者)买的、Proxy(代理)将Pursuit(追求者)买的礼物送给SchoolGirl(被追求者)。礼物实质是Pursuit(追求者)送的。Pursuit(追求者)和Proxy(代理)都有送礼物的行动综上所述:Pursuit(追求者)和Proxy(代理)应该实现相同的接口符合实际的代码classSchoolGirl{privatestringname;pu
6、blicstringName{get{returnname;}set{name=value;}}}interfaceGiveGift{voidGiveDolls();voidGiveFlowers();voidGiveChocolate();}classPursuit:GiveGift{SchoolGirlmm;publicPursuit(SchoolGirlmm){this.mm=mm;}publicvoidGiveDolls(){Console.WriteLine(mm.Name+"送你洋娃娃");}pu
7、blicvoidGiveFlowers(){Console.WriteLine(mm.Name+"送你鲜花");}publicvoidGiveChocolate(){Console.WriteLine(mm.Name+"送你巧克力");}}classProxy:GiveGift{Pursuitgg;publicProxy(SchoolGirlmm){gg=newPursuit(mm);}publicvoidGiveDolls(){gg.GiveDolls();}publicvoidGiveFlowers(){
8、gg.GiveFlowers();}publicvoidGiveChocolate(){gg.GiveChocolate();}}staticvoidMain(string[]args){SchoolGirljiaojiao=newSchoolGirl();jiaojiao.Name="李娇娇";Proxydaili=newProxy(jiaojiao);daili.GiveDolls();dail
此文档下载收益归作者所有