浅析net简单工厂模式.docVIP

  • 1
  • 0
  • 约2.95千字
  • 约 8页
  • 2019-01-14 发布于广东
  • 举报
浅析net简单工厂模式.doc

浅析.net简单工厂模式 程时一门技术,更是一门艺术 简单工厂模式利用面向对象方式通过继承、封装、多 态把程序的耦合度降低,设计模式使得程序更加灵活,容 易修改,易于复用。 下面是服务器计算器代码: 代码如下: usingSystem; usingSystem. Collections. Generic; usingSystem. Linq; usingSystem. Text; usingSystem. Threading.T asks; namesp aceDesignMo del { ///It; s ummarygt; ///计算器 ///It;/summarygt; publicclas sCalculator //创建一个计算器的基类可 以接受两个参数,任何算法只需重写计算结果方法即可。 pri vatedouble_numberA; p rivatedoubl e_numberB; p ublicdouble NumberA g et {returnth is. _numbeTA ;} set {thi s. _nuHiberA二volue;} } pub licdoubleNu mberB { 百et {returnthis ?_numberB;} set {this? _nuniberB二vo lue;} } pub licvirtuaid oubleGetRes ult () { doubl eresult=0; r eturnresult ; } } ///It; su mmarygt; ///加法 ///It: /su mmarygt; p ublicclassA dd:Calculat or//每添加一种计算方式 只需添加一个计算类并重写基类方法即可 publicover ridedoubleG etResult () returnNum berA+Number B; ///11; s ummarygt; ///减法 ///It ;/s ummarygt; publicclass Sub:Calcula tor publi coverridedo ubleGetResu lt() retu rnNumberA+N umberB; ///It; summary gt; ///计算器工厂 ///It;/summ arygt; pub licclassCal culatorFact ory publi cstaticCalc ulatorGetRe suit (string oper) Cal culatorcalc u=null; swit ch (oper) { 〃.〃 ca se + : calcu =newAdd (); b reak; 〃 〃 case 一 : calcu=new Sub (); break ; } returncal cu; } } 复制代码代码如下: stati cvoidMain(s tring[]args ) { Console . WriteLine (〃请输入数字 A:〃); stringnum bera=Consol e. ReadLine (); Console . WriteLine (〃请输入运算符:〃); stringope r=Console? R eadLine (); Console. Wr iteLine (“请输入数字 B: 〃); s tringnumber b=Console? R eadLine (); Calculator c=Calculato rFactory. Ge tResult (ope r); c. Numb erA=Convert . ToDouble (n umbera); c ? NumberB二Co nvert? ToDou ble (numberb ); Console ? Writ eLine ( st ring ? Forni at (“ {0} {1} { 2}二{3}〃,num bera, oper, n umberb, c. Ge tResult ())); Console? Re adLine (); } 基本验证没加,学习练习的同学可以自己加上 28种设计模式后续更新 代码如下: usingSyst em; usingS ystem. Colle ctions. Gene ric; usingSy stem. Linq; u singSystem. Text; usin gSystem? Thr eading? Task s; namespace DesignModel ///It; summ arygt; ///计算器 ///It;/sum marygt; pu blicclassCa lculator

文档评论(0)

1亿VIP精品文档

相关文档